14
14
// ## Run
15
15
//
16
16
// ```
17
- // cargo run --example sv2_frame --features with_serde
17
+ // cargo run --example sv2_frame
18
18
// ```
19
19
20
- #[ cfg( feature = "with_serde" ) ]
21
- use binary_sv2:: { GetSize , Serialize } ;
22
- #[ cfg( feature = "with_serde" ) ]
20
+ use binary_sv2:: { binary_codec_sv2, Serialize } ;
23
21
use framing_sv2:: framing:: Sv2Frame ;
24
22
25
23
// Example message type (e.g., SetupConnection)
26
- #[ cfg( feature = "with_serde" ) ]
27
24
const MSG_TYPE : u8 = 1 ;
28
25
// Example extension type (e.g., a standard Sv2 message)
29
- #[ cfg( feature = "with_serde" ) ]
30
26
const EXT_TYPE : u16 = 0x0001 ;
31
27
32
- #[ cfg( feature = "with_serde" ) ]
33
28
#[ derive( Serialize , Debug ) ]
34
- struct CustomMessage {
29
+ pub struct CustomMessage {
35
30
pub data : u32 ,
36
31
}
37
32
38
- // Implemented to help determine the size of the message when framing.
39
- #[ cfg( feature = "with_serde" ) ]
40
- impl GetSize for CustomMessage {
41
- fn get_size ( & self ) -> usize {
42
- 4 // `data` is `u32`, which is 4 bytes
43
- }
44
- }
45
-
46
- #[ cfg( feature = "with_serde" ) ]
47
33
fn main ( ) {
48
34
// Create the message payload
49
35
let message = CustomMessage { data : 42 } ;
@@ -67,11 +53,7 @@ fn main() {
67
53
// Deserialize the frame from bytes back into an Sv2Frame
68
54
let mut deserialized_frame = Sv2Frame :: < CustomMessage , Vec < u8 > > :: from_bytes ( serialized_frame)
69
55
. expect ( "Failed to deserialize frame" ) ;
56
+
70
57
assert_eq ! ( deserialized_frame. encoded_length( ) , 10 ) ; // 6 header bytes + 4 payload bytes
71
58
assert_eq ! ( deserialized_frame. payload( ) , [ 42 , 0 , 0 , 0 ] ) ;
72
59
}
73
-
74
- #[ cfg( not( feature = "with_serde" ) ) ]
75
- fn main ( ) {
76
- eprintln ! ( "Serde feature not enabled. Skipping example." ) ;
77
- }
0 commit comments