8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
+ use std:: mem;
11
12
use rustc_data_structures:: stable_hasher;
13
+ use serialize;
14
+ use serialize:: opaque:: { EncodeResult , Encoder , Decoder } ;
12
15
13
- #[ derive( Eq , PartialEq , Ord , PartialOrd , Hash , Debug , Clone , Copy , RustcEncodable , RustcDecodable ) ]
16
+ #[ derive( Eq , PartialEq , Ord , PartialOrd , Hash , Debug , Clone , Copy ) ]
14
17
pub struct Fingerprint ( u64 , u64 ) ;
15
18
16
19
impl Fingerprint {
@@ -46,6 +49,21 @@ impl Fingerprint {
46
49
format ! ( "{:x}{:x}" , self . 0 , self . 1 )
47
50
}
48
51
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
+ }
49
67
}
50
68
51
69
impl :: std:: fmt:: Display for Fingerprint {
@@ -69,3 +87,19 @@ impl<CTX> stable_hasher::HashStable<CTX> for Fingerprint {
69
87
:: std:: hash:: Hash :: hash ( self , hasher) ;
70
88
}
71
89
}
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