-
-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ByteSize impl for CString (#497)
* Add ByteSize impl for CString * Use the new dynamic bytes attribute to apply to CString, which in many protocols you already have the size of the CString, so might as well use it
- Loading branch information
1 parent
cbad152
commit 35b1d44
Showing
2 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use deku::prelude::*; | ||
|
||
use std::ffi::CString; | ||
|
||
#[test] | ||
fn test_cstring_no_ctx() { | ||
#[derive(PartialEq, Debug, DekuRead, DekuWrite)] | ||
pub struct Data { | ||
s: CString, | ||
} | ||
|
||
let bytes = &[b't', b'e', b's', b't', b'\0']; | ||
let (_, d) = Data::from_bytes((bytes, 0)).unwrap(); | ||
assert_eq!(d.s, CString::new("test").unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_cstring_bytes() { | ||
#[derive(PartialEq, Debug, DekuRead, DekuWrite)] | ||
pub struct Data { | ||
len: u8, | ||
#[deku(bytes = "*len as usize")] | ||
s: CString, | ||
} | ||
|
||
let bytes = &[0x05, b't', b'e', b's', b't', b'\0']; | ||
let (_, d) = Data::from_bytes((bytes, 0)).unwrap(); | ||
assert_eq!(d.s, CString::new("test").unwrap()); | ||
} |