88// option. This file may not be copied, modified, or distributed
99// except according to those terms.
1010
11+ use std:: mem;
1112use rustc_data_structures:: stable_hasher;
13+ use serialize;
14+ use serialize:: opaque:: { EncodeResult , Encoder , Decoder } ;
1215
13- #[ derive( Eq , PartialEq , Ord , PartialOrd , Hash , Debug , Clone , Copy , RustcEncodable , RustcDecodable ) ]
16+ #[ derive( Eq , PartialEq , Ord , PartialOrd , Hash , Debug , Clone , Copy ) ]
1417pub struct Fingerprint ( u64 , u64 ) ;
1518
1619impl Fingerprint {
@@ -46,6 +49,21 @@ impl Fingerprint {
4649 format ! ( "{:x}{:x}" , self . 0 , self . 1 )
4750 }
4851
52+ pub fn encode_opaque ( & self , encoder : & mut Encoder ) -> EncodeResult {
53+ let bytes: [ u8 ; 16 ] = unsafe { mem:: transmute ( [ self . 0 . to_le ( ) , self . 1 . to_le ( ) ] ) } ;
54+
55+ encoder. emit_raw_bytes ( & bytes)
56+ }
57+
58+ pub fn decode_opaque < ' a > ( decoder : & mut Decoder < ' a > ) -> Result < Fingerprint , String > {
59+ let mut bytes = [ 0 ; 16 ] ;
60+
61+ decoder. read_raw_bytes ( & mut bytes) ?;
62+
63+ let [ l, r] : [ u64 ; 2 ] = unsafe { mem:: transmute ( bytes) } ;
64+
65+ Ok ( Fingerprint ( u64:: from_le ( l) , u64:: from_le ( r) ) )
66+ }
4967}
5068
5169impl :: std:: fmt:: Display for Fingerprint {
@@ -69,3 +87,19 @@ impl<CTX> stable_hasher::HashStable<CTX> for Fingerprint {
6987 :: std:: hash:: Hash :: hash ( self , hasher) ;
7088 }
7189}
90+
91+ impl serialize:: UseSpecializedEncodable for Fingerprint { }
92+
93+ impl serialize:: UseSpecializedDecodable for Fingerprint { }
94+
95+ impl < ' a > serialize:: SpecializedEncoder < Fingerprint > for serialize:: opaque:: Encoder < ' a > {
96+ fn specialized_encode ( & mut self , f : & Fingerprint ) -> Result < ( ) , Self :: Error > {
97+ f. encode_opaque ( self )
98+ }
99+ }
100+
101+ impl < ' a > serialize:: SpecializedDecoder < Fingerprint > for serialize:: opaque:: Decoder < ' a > {
102+ fn specialized_decode ( & mut self ) -> Result < Fingerprint , Self :: Error > {
103+ Fingerprint :: decode_opaque ( self )
104+ }
105+ }
0 commit comments