@@ -57,7 +57,9 @@ impl<T, C: Container> Message<T, C> {
5757}
5858
5959// Instructions for serialization of `Message`.
60- // Intended to swap out the constraint on `C` for `C: Bytesable`.
60+ //
61+ // Serialization of each field is meant to be `u64` aligned, so that each has tha ability
62+ // to be decoded using safe transmutation, e.g. `bytemuck`.
6163impl < T , C > crate :: communication:: Bytesable for Message < T , C >
6264where
6365 T : Serialize + for < ' a > Deserialize < ' a > ,
@@ -69,24 +71,29 @@ where
6971 let from: usize = slice. read_u64 :: < byteorder:: LittleEndian > ( ) . unwrap ( ) . try_into ( ) . unwrap ( ) ;
7072 let seq: usize = slice. read_u64 :: < byteorder:: LittleEndian > ( ) . unwrap ( ) . try_into ( ) . unwrap ( ) ;
7173 let time: T = :: bincode:: deserialize_from ( & mut slice) . expect ( "bincode::deserialize() failed" ) ;
72- let bytes_read = bytes. len ( ) - slice. len ( ) ;
74+ let time_size = :: bincode:: serialized_size ( & time) . expect ( "bincode::serialized_size() failed" ) as usize ;
75+ // We expect to find the `data` payload at `8 + 8 + round_up(time_size)`;
76+ let bytes_read = 8 + 8 + ( ( time_size + 7 ) & !7 ) ;
77+ // let bytes_read = bytes.len() - slice.len();
7378 bytes. extract_to ( bytes_read) ;
7479 let data: C = ContainerBytes :: from_bytes ( bytes) ;
7580 Self { time, data, from, seq }
7681 }
7782
7883 fn length_in_bytes ( & self ) -> usize {
84+ let time_size = :: bincode:: serialized_size ( & self . time ) . expect ( "bincode::serialized_size() failed" ) as usize ;
7985 // 16 comes from the two `u64` fields: `from` and `seq`.
80- 16 +
81- :: bincode:: serialized_size ( & self . time ) . expect ( "bincode::serialized_size() failed" ) as usize +
82- self . data . length_in_bytes ( )
86+ 16 + ( ( time_size + 7 ) & !7 ) + self . data . length_in_bytes ( )
8387 }
8488
8589 fn into_bytes < W : :: std:: io:: Write > ( & self , writer : & mut W ) {
8690 use byteorder:: WriteBytesExt ;
8791 writer. write_u64 :: < byteorder:: LittleEndian > ( self . from . try_into ( ) . unwrap ( ) ) . unwrap ( ) ;
8892 writer. write_u64 :: < byteorder:: LittleEndian > ( self . seq . try_into ( ) . unwrap ( ) ) . unwrap ( ) ;
8993 :: bincode:: serialize_into ( & mut * writer, & self . time ) . expect ( "bincode::serialize_into() failed" ) ;
94+ let time_size = :: bincode:: serialized_size ( & self . time ) . expect ( "bincode::serialized_size() failed" ) as usize ;
95+ let time_slop = ( ( time_size + 7 ) & !7 ) - time_size;
96+ writer. write ( & [ 0u8 ; 8 ] [ ..time_slop] ) . unwrap ( ) ;
9097 self . data . into_bytes ( & mut * writer) ;
9198 }
9299}
0 commit comments