You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+41Lines changed: 41 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,8 +13,49 @@ We have two main structures available:
13
13
* Message - Owned parsed values
14
14
* MessageRef - Message parsed by reference and bound to the lifetime of the readers source
15
15
16
+
For convenience, a derive macro is available to implement `Packable` and `Unpackable` for the types. These implementations will allow the types to be sent and received from `MessagePacker` and `MessageUnpacker` implementations, such as `CursorPacker`.
17
+
16
18
## Example
17
19
20
+
```rust
21
+
usemsgpacker::prelude::*;
22
+
23
+
#[derive(MsgPacker, Debug, Clone, PartialEq, Eq)]
24
+
pubstructFoo {
25
+
val:u64,
26
+
text:String,
27
+
flag:bool,
28
+
bar:Bar,
29
+
}
30
+
31
+
#[derive(MsgPacker, Debug, Clone, PartialEq, Eq)]
32
+
pubstructBar {
33
+
arr: [u8; 32],
34
+
}
35
+
36
+
letbar=Bar { arr: [0xff; 32] };
37
+
letfoo=Foo {
38
+
val:15,
39
+
text:String::from("Hello, world!"),
40
+
flag:true,
41
+
bar,
42
+
};
43
+
44
+
// Create a new bytes buffer
45
+
letmutbuffer:Vec<u8> =vec![];
46
+
47
+
// Pack the message into the buffer
48
+
CursorPacker::new(&mutbuffer).pack(foo.clone()).expect("failed to pack `Foo`");
49
+
50
+
// Unpack the message from the buffer
51
+
letfoo_p=CursorPacker::new(&buffer).unpack::<Foo>().expect("failed to unpack `Foo`");
52
+
53
+
// Assert the unpacked message is exactly the same as the original
0 commit comments