Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Parcel for std::ffi::CString #22

Merged
merged 2 commits into from
Jul 15, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Implement Parcel for std::ffi::CString
anarelion committed Jul 3, 2020
commit 9049e07da3ae5badfcbb2d7d3fafaf46e929bc10
1 change: 1 addition & 0 deletions protocol/src/errors.rs
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@ error_chain! {
foreign_links {
Io(std::io::Error);
FromUtf8(std::string::FromUtf8Error);
FromFfi(std::ffi::NulError);
anarelion marked this conversation as resolved.
Show resolved Hide resolved
TryFromIntError(TryFromIntError);
CharTryFromError(CharTryFromError);

61 changes: 61 additions & 0 deletions protocol/src/types/cstring.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use std::ffi::CString;
use std::io::prelude::{Read, Write};
use {hint, Error, Parcel, Settings};

impl Parcel for CString {
const TYPE_NAME: &'static str = "CString";

fn read_field(
read: &mut dyn Read,
settings: &Settings,
_hints: &mut hint::Hints,
) -> Result<Self, Error> {
let mut result = Vec::new();
loop {
anarelion marked this conversation as resolved.
Show resolved Hide resolved
let c: u8 = Parcel::read(read, settings)?;
if c == 0x00 {
break;
anarelion marked this conversation as resolved.
Show resolved Hide resolved
}
result.push(c);
}
Ok(CString::new(result)?)
}

fn write_field(
&self,
write: &mut dyn Write,
settings: &Settings,
_hints: &mut hint::Hints,
) -> Result<(), Error> {
for c in self.clone().into_bytes() {
anarelion marked this conversation as resolved.
Show resolved Hide resolved
anarelion marked this conversation as resolved.
Show resolved Hide resolved
c.write(write, settings)?;
if c == 0x00 {
anarelion marked this conversation as resolved.
Show resolved Hide resolved
return Ok(());
}
}
0u8.write(write, settings)?;
Ok(())
}
}

#[cfg(test)]
mod test {
use {Parcel, Settings};
use std::io::Cursor;
use std::ffi::CString;

#[test]
fn can_read_cstring() {
let mut data = Cursor::new([0x41, 0x42, 0x43, 0]);
let read_back: CString = Parcel::read(&mut data, &Settings::default()).unwrap();
assert_eq!(read_back, CString::new("ABC").unwrap());
}

#[test]
fn can_write_cstring() {
let mut buffer = Cursor::new(Vec::new());

CString::new("ABC").unwrap().write(&mut buffer, &Settings::default()).unwrap();
assert_eq!(buffer.into_inner(), vec![0x41, 0x42, 0x43, 0]);
}
}
1 change: 1 addition & 0 deletions protocol/src/types/mod.rs
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@ mod array;
mod char;
/// Definitions for the `std::collections` module.
mod collections;
mod cstring;
mod marker;
mod numerics;
mod option;