@@ -12,9 +12,19 @@ impl PageID {
1212 pub fn to_bytes ( & self ) -> [ u8 ; 36 ] {
1313 let mut res = [ 0u8 ; 36 ] ;
1414 res[ ..32 ] . copy_from_slice ( & self . 0 . raw ( ) ) ;
15- res[ 32 ..] . copy_from_slice ( & self . 1 . to_le_bytes ( ) ) ;
15+ res[ 32 ..] . copy_from_slice ( & self . 1 . to_be_bytes ( ) ) ;
1616 res
1717 }
18+
19+ pub fn from_bytes ( data : [ u8 ; 36 ] ) -> Self {
20+ let mut slot_bytes = [ 0u8 ; 32 ] ;
21+ slot_bytes. copy_from_slice ( & data[ ..32 ] ) ;
22+ let slot_id = SlotID :: from_bytes ( slot_bytes) ;
23+ let mut index_bytes = [ 0u8 ; 4 ] ;
24+ index_bytes. copy_from_slice ( & data[ 32 ..] ) ;
25+ let index = u32:: from_be_bytes ( index_bytes) ;
26+ Self ( slot_id, index)
27+ }
1828}
1929
2030#[ derive( Debug , Clone , serde:: Serialize ) ]
@@ -369,3 +379,30 @@ impl<HS: HostStorageLocking + Send + Sync> Storage<HS> {
369379 Ok ( res)
370380 }
371381}
382+
383+ #[ cfg( test) ]
384+ mod tests {
385+ use super :: * ;
386+
387+ #[ test]
388+ fn pages_sorted_correctly_1_byte ( ) {
389+ let left = PageID ( SlotID :: from_bytes ( [ 1u8 ; 32 ] ) , 5 ) ;
390+ let right = PageID ( SlotID :: from_bytes ( [ 1u8 ; 32 ] ) , 10 ) ;
391+ assert ! ( left < right) ;
392+ assert ! ( left. to_bytes( ) < right. to_bytes( ) ) ;
393+
394+ assert ! ( right > left) ;
395+ assert ! ( right. to_bytes( ) > left. to_bytes( ) ) ;
396+ }
397+
398+ #[ test]
399+ fn pages_sorted_correctly_2_byte ( ) {
400+ let left = PageID ( SlotID :: from_bytes ( [ 1u8 ; 32 ] ) , 5 ) ;
401+ let right = PageID ( SlotID :: from_bytes ( [ 1u8 ; 32 ] ) , 1024 ) ;
402+ assert ! ( left < right) ;
403+ assert ! ( left. to_bytes( ) < right. to_bytes( ) ) ;
404+
405+ assert ! ( right > left) ;
406+ assert ! ( right. to_bytes( ) > left. to_bytes( ) ) ;
407+ }
408+ }
0 commit comments