@@ -19,7 +19,7 @@ use bincode::{
19
19
de:: { BorrowDecoder , Decoder } ,
20
20
enc:: Encoder ,
21
21
error:: { DecodeError , EncodeError } ,
22
- Decode , Encode ,
22
+ BorrowDecode , Decode , Encode ,
23
23
} ;
24
24
use std:: string:: ToString ;
25
25
@@ -31,33 +31,31 @@ impl Encode for Uuid {
31
31
32
32
impl Encode for NonNilUuid {
33
33
fn encode < E : Encoder > ( & self , encoder : & mut E ) -> Result < ( ) , EncodeError > {
34
- let uuid = Uuid :: from ( * self ) ;
35
-
36
- Encode :: encode ( uuid. as_bytes ( ) , encoder)
34
+ self . get ( ) . encode ( encoder)
37
35
}
38
36
}
39
37
40
38
impl Encode for Hyphenated {
41
39
fn encode < E : Encoder > ( & self , encoder : & mut E ) -> Result < ( ) , EncodeError > {
42
- Encode :: encode ( self . as_uuid ( ) . as_bytes ( ) , encoder)
40
+ self . as_uuid ( ) . encode ( encoder)
43
41
}
44
42
}
45
43
46
44
impl Encode for Simple {
47
45
fn encode < E : Encoder > ( & self , encoder : & mut E ) -> Result < ( ) , EncodeError > {
48
- Encode :: encode ( self . as_uuid ( ) . as_bytes ( ) , encoder)
46
+ self . as_uuid ( ) . encode ( encoder)
49
47
}
50
48
}
51
49
52
50
impl Encode for Urn {
53
51
fn encode < E : Encoder > ( & self , encoder : & mut E ) -> Result < ( ) , EncodeError > {
54
- Encode :: encode ( self . as_uuid ( ) . as_bytes ( ) , encoder)
52
+ self . as_uuid ( ) . encode ( encoder)
55
53
}
56
54
}
57
55
58
56
impl Encode for Braced {
59
57
fn encode < E : Encoder > ( & self , encoder : & mut E ) -> Result < ( ) , EncodeError > {
60
- Encode :: encode ( self . as_uuid ( ) . as_bytes ( ) , encoder)
58
+ self . as_uuid ( ) . encode ( encoder)
61
59
}
62
60
}
63
61
@@ -66,11 +64,11 @@ impl<Context> Decode<Context> for Uuid {
66
64
Ok ( Uuid :: from_bytes ( Decode :: decode ( decoder) ?) )
67
65
}
68
66
}
69
- impl < ' de , Context > bincode :: BorrowDecode < ' de , Context > for Uuid {
67
+ impl < ' de , Context > BorrowDecode < ' de , Context > for Uuid {
70
68
fn borrow_decode < D : BorrowDecoder < ' de , Context = Context > > (
71
69
decoder : & mut D ,
72
- ) -> core :: result :: Result < Self , bincode :: error :: DecodeError > {
73
- Ok ( Uuid :: from_bytes ( Decode :: decode ( decoder) ?) )
70
+ ) -> Result < Self , DecodeError > {
71
+ Ok ( Uuid :: from_bytes ( BorrowDecode :: borrow_decode ( decoder) ?) )
74
72
}
75
73
}
76
74
@@ -81,11 +79,11 @@ impl<Context> Decode<Context> for NonNilUuid {
81
79
NonNilUuid :: try_from ( uuid) . map_err ( |e| DecodeError :: OtherString ( e. to_string ( ) ) )
82
80
}
83
81
}
84
- impl < ' de , Context > bincode :: BorrowDecode < ' de , Context > for NonNilUuid {
82
+ impl < ' de , Context > BorrowDecode < ' de , Context > for NonNilUuid {
85
83
fn borrow_decode < D : BorrowDecoder < ' de , Context = Context > > (
86
84
decoder : & mut D ,
87
- ) -> core :: result :: Result < Self , bincode :: error :: DecodeError > {
88
- let uuid = Uuid :: decode ( decoder) ?;
85
+ ) -> Result < Self , DecodeError > {
86
+ let uuid = Uuid :: borrow_decode ( decoder) ?;
89
87
90
88
NonNilUuid :: try_from ( uuid) . map_err ( |e| DecodeError :: OtherString ( e. to_string ( ) ) )
91
89
}
@@ -99,71 +97,79 @@ mod bincode_tests {
99
97
#[ test]
100
98
fn test_encode_readable_string ( ) {
101
99
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4" ;
102
- let u = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
100
+ let uuid = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
103
101
104
- bincode:: encode_to_vec ( & u , config:: standard ( ) )
102
+ let bytes = bincode:: encode_to_vec ( & uuid , config:: standard ( ) )
105
103
. expect ( & format ! ( "Failed to encode {uuid_str}." ) ) ;
104
+ let ( decoded_uuid, _) = bincode:: decode_from_slice :: < Uuid , _ > ( & bytes, config:: standard ( ) )
105
+ . expect ( & format ! ( "Failed to decode {bytes:?}." ) ) ;
106
+
107
+ assert_eq ! ( uuid, decoded_uuid) ;
106
108
}
107
109
108
110
#[ test]
109
111
fn test_encode_hyphenated ( ) {
110
112
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4" ;
111
- let u = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
113
+ let uuid = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
112
114
113
- bincode:: encode_to_vec ( & u , config:: standard ( ) )
115
+ let bytes = bincode:: encode_to_vec ( & uuid , config:: standard ( ) )
114
116
. expect ( & format ! ( "Failed to encode {uuid_str}." ) ) ;
117
+ let ( decoded_uuid, _) = bincode:: decode_from_slice :: < Uuid , _ > ( & bytes, config:: standard ( ) )
118
+ . expect ( & format ! ( "Failed to decode {bytes:?}." ) ) ;
119
+
120
+ assert_eq ! ( uuid, decoded_uuid) ;
115
121
}
116
122
117
123
#[ test]
118
124
fn test_encode_simple ( ) {
119
125
let uuid_str = "f9168c5eceb24faab6bf329bf39fa1e4" ;
120
- let u = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
126
+ let uuid = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
121
127
122
- bincode:: encode_to_vec ( & u , config:: standard ( ) )
128
+ let bytes = bincode:: encode_to_vec ( & uuid , config:: standard ( ) )
123
129
. expect ( & format ! ( "Failed to encode {uuid_str}." ) ) ;
130
+ let ( decoded_uuid, _) = bincode:: decode_from_slice :: < Uuid , _ > ( & bytes, config:: standard ( ) )
131
+ . expect ( & format ! ( "Failed to decode {bytes:?}." ) ) ;
132
+
133
+ assert_eq ! ( uuid, decoded_uuid) ;
124
134
}
125
135
126
136
#[ test]
127
137
fn test_encode_urn ( ) {
128
138
let uuid_str = "urn:uuid:f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4" ;
129
- let u = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
139
+ let uuid = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
130
140
131
- bincode:: encode_to_vec ( & u , config:: standard ( ) )
141
+ let bytes = bincode:: encode_to_vec ( & uuid , config:: standard ( ) )
132
142
. expect ( & format ! ( "Failed to encode {uuid_str}." ) ) ;
143
+ let ( decoded_uuid, _) = bincode:: decode_from_slice :: < Uuid , _ > ( & bytes, config:: standard ( ) )
144
+ . expect ( & format ! ( "Failed to decode {bytes:?}." ) ) ;
145
+
146
+ assert_eq ! ( uuid, decoded_uuid) ;
133
147
}
134
148
135
149
#[ test]
136
150
fn test_encode_braced ( ) {
137
151
let uuid_str = "{f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4}" ;
138
- let u = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
152
+ let uuid = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
139
153
140
- bincode:: encode_to_vec ( & u , config:: standard ( ) )
154
+ let bytes = bincode:: encode_to_vec ( & uuid , config:: standard ( ) )
141
155
. expect ( & format ! ( "Failed to encode {uuid_str}." ) ) ;
156
+ let ( decoded_uuid, _) = bincode:: decode_from_slice :: < Uuid , _ > ( & bytes, config:: standard ( ) )
157
+ . expect ( & format ! ( "Failed to decode {bytes:?}." ) ) ;
158
+
159
+ assert_eq ! ( uuid, decoded_uuid) ;
142
160
}
143
161
144
162
#[ test]
145
163
fn test_encode_non_human_readable ( ) {
146
164
let uuid_bytes = b"F9168C5E-CEB2-4F" ;
147
- let u = Uuid :: from_slice ( uuid_bytes) . unwrap ( ) ;
165
+ let uuid = Uuid :: from_slice ( uuid_bytes) . unwrap ( ) ;
148
166
149
- bincode:: encode_to_vec ( & u , config:: standard ( ) )
167
+ let bytes = bincode:: encode_to_vec ( & uuid , config:: standard ( ) )
150
168
. expect ( & format ! ( "{:?} failed to encode." , uuid_bytes) ) ;
151
- }
169
+ let ( decoded_uuid, _) = bincode:: decode_from_slice :: < Uuid , _ > ( & bytes, config:: standard ( ) )
170
+ . expect ( & format ! ( "Failed to decode {bytes:?}." ) ) ;
152
171
153
- #[ test]
154
- fn test_decode ( ) {
155
- let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4" ;
156
- let u = Uuid :: parse_str ( uuid_str) . unwrap ( ) ;
157
-
158
- let bytes = bincode:: encode_to_vec ( & u, config:: standard ( ) )
159
- . expect ( & format ! ( "Failed to encode {uuid_str}." ) ) ;
160
-
161
- let ( decoded_uuid, decoded_size) =
162
- bincode:: decode_from_slice :: < Uuid , _ > ( & bytes, config:: standard ( ) )
163
- . expect ( & format ! ( "Failed to decode {bytes:?}." ) ) ;
164
-
165
- assert_eq ! ( u, decoded_uuid) ;
166
- assert_eq ! ( 16 , decoded_size) ;
172
+ assert_eq ! ( uuid, decoded_uuid) ;
167
173
}
168
174
169
175
#[ test]
0 commit comments