-
-
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.
- Allow DekuUpdate::update to have ctx - Add call_update, to call `update()` on self. - Add update_custom to call custom function
- Loading branch information
1 parent
1e96f86
commit a8f6daf
Showing
6 changed files
with
141 additions
and
19 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
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,68 @@ | ||
use deku::prelude::*; | ||
|
||
#[derive(Debug, DekuRead, DekuWrite, PartialEq)] | ||
pub struct Test { | ||
#[deku(call_update, update_ctx = "self.val.len() as u16, 0")] | ||
hdr: Hdr, | ||
|
||
#[deku(count = "hdr.length")] | ||
val: Vec<u8>, | ||
|
||
#[deku(call_update)] | ||
no_update_ctx: NoUpdateCtx, | ||
|
||
#[deku(update_custom = "Self::custom")] | ||
num: u8, | ||
|
||
#[deku(update_custom = "Self::other_custom")] | ||
other_num: (u8, u32), | ||
} | ||
|
||
impl Test { | ||
fn custom(num: &mut u8) -> Result<(), DekuError> { | ||
*num = 1; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn other_custom(num: &mut (u8, u32)) -> Result<(), DekuError> { | ||
*num = (0xf0, 0x0f); | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
#[derive(Debug, DekuRead, DekuWrite, PartialEq)] | ||
#[deku(update_ctx = "val_len: u16, _na: u8")] | ||
struct Hdr { | ||
#[deku(update = "val_len")] | ||
length: u8, | ||
} | ||
|
||
#[derive(Debug, DekuRead, DekuWrite, PartialEq)] | ||
struct NoUpdateCtx { | ||
#[deku(update = "0xff")] | ||
val: u8, | ||
} | ||
|
||
fn main() { | ||
let mut test = Test { | ||
hdr: Hdr { length: 2 }, | ||
val: vec![1, 2], | ||
no_update_ctx: NoUpdateCtx { val: 0 }, | ||
num: 0, | ||
other_num: (0, 0), | ||
}; | ||
|
||
test.val = vec![1, 2, 3]; | ||
test.update(()).unwrap(); | ||
|
||
let expected = Test { | ||
hdr: Hdr { length: 3 }, | ||
val: test.val.clone(), | ||
no_update_ctx: NoUpdateCtx { val: 0xff }, | ||
num: 1, | ||
other_num: (0xf0, 0x0f), | ||
}; | ||
assert_eq!(expected, test); | ||
} |
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
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