@@ -66,6 +66,8 @@ use std::rand::Rng;
66
66
use std:: cmp:: Eq ;
67
67
use std:: cast:: { transmute, transmute_copy} ;
68
68
69
+ use serialize:: { Encoder , Encodable , Decoder , Decodable } ;
70
+
69
71
/// A 128-bit (16 byte) buffer containing the ID
70
72
pub type UuidBytes = [ u8 , ..16 ] ;
71
73
@@ -486,6 +488,21 @@ impl TotalEq for Uuid {
486
488
}
487
489
}
488
490
491
+ // FIXME #9845: Test these more thoroughly
492
+ impl < T : Encoder > Encodable < T > for Uuid {
493
+ /// Encode a UUID as a hypenated string
494
+ fn encode ( & self , e : & mut T ) {
495
+ e. emit_str ( self . to_hyphenated_str ( ) ) ;
496
+ }
497
+ }
498
+
499
+ impl < T : Decoder > Decodable < T > for Uuid {
500
+ /// Decode a UUID from a string
501
+ fn decode ( d : & mut T ) -> Uuid {
502
+ from_str ( d. read_str ( ) ) . unwrap ( )
503
+ }
504
+ }
505
+
489
506
/// Generates a random instance of UUID (V4 conformant)
490
507
impl rand:: Rand for Uuid {
491
508
#[ inline]
@@ -770,6 +787,20 @@ mod test {
770
787
assert ! ( ub. len( ) == 16 ) ;
771
788
assert ! ( ! ub. iter( ) . all( |& b| b == 0 ) ) ;
772
789
}
790
+
791
+ #[ test]
792
+ fn test_serialize_round_trip ( ) {
793
+ use std;
794
+ use ebml;
795
+ use serialize:: { Encodable , Decodable } ;
796
+
797
+ let u = Uuid :: new_v4 ( ) ;
798
+ let bytes = do std:: io:: with_bytes_writer |wr| {
799
+ u. encode ( & mut ebml:: writer:: Encoder ( wr) ) ;
800
+ } ;
801
+ let u2 = Decodable :: decode ( & mut ebml:: reader:: Decoder ( ebml:: reader:: Doc ( @bytes) ) ) ;
802
+ assert_eq ! ( u, u2) ;
803
+ }
773
804
}
774
805
775
806
#[ cfg( test) ]
0 commit comments