Skip to content

Commit e34d40d

Browse files
committed
chore: reworked example
1 parent 8c6d749 commit e34d40d

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

tests/test_attributes/test_ctx.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,11 +419,11 @@ fn test_interior_mutability_for_context_read_until_with_ctx_hashmap() {
419419
#[derive(PartialEq, Debug, Clone)]
420420
struct A {
421421
#[deku(
422-
until_with_ctx = "|x:&(B,B),ctx:IndexContext| !ctx.fx.get()",
422+
until_with_ctx = "|x:&(N,B),ctx:IndexContext| !ctx.fx.get()",
423423
ctx = "IndexContext { idx: std::rc::Rc::new(std::cell::Cell::new(0)), n: 0, fx: std::rc::Rc::new(std::cell::Cell::new(false))}",
424424
writer_ctx = "IndexContext { idx: std::rc::Rc::new(std::cell::Cell::new(0)), n: items.len(), fx: std::rc::Rc::new(std::cell::Cell::new(false)) }"
425425
)]
426-
items: HashMap<B, B>,
426+
items: HashMap<N, B>,
427427
}
428428

429429
#[deku_derive(DekuRead, DekuWrite)]
@@ -448,11 +448,21 @@ fn test_interior_mutability_for_context_read_until_with_ctx_hashmap() {
448448
auto_fx: u8,
449449
}
450450

451+
#[deku_derive(DekuRead, DekuWrite)]
452+
#[derive(PartialEq, Debug, Clone, Eq, Hash)]
453+
#[deku(
454+
ctx = "ctx: IndexContext",
455+
ctx_default = "IndexContext{idx: std::rc::Rc::new(std::cell::Cell::new(0)), n: 0, fx: std::rc::Rc::new(std::cell::Cell::new(false))}"
456+
)] // this struct uses a context for serialization. For deserialization it also works with the default context.
457+
struct N {
458+
n: u8,
459+
}
460+
451461
let test_data = A {
452462
items: HashMap::from([
453-
(B { x: 8, y: 9 }, B { x: 8, y: 9 }),
454-
(B { x: 8, y: 9 }, B { x: 7, y: 9 }),
455-
(B { x: 8, y: 9 }, B { x: 6, y: 9 }),
463+
(N { n: 1 }, B { x: 8, y: 9 }),
464+
(N { n: 2 }, B { x: 7, y: 9 }),
465+
(N { n: 3 }, B { x: 6, y: 9 }),
456466
]),
457467
};
458468

0 commit comments

Comments
 (0)