Skip to content

Commit 621c89a

Browse files
committed
Cleanup
1 parent ce92df8 commit 621c89a

File tree

1 file changed

+47
-41
lines changed

1 file changed

+47
-41
lines changed

src/external/bincode_support.rs

Lines changed: 47 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use bincode::{
1919
de::{BorrowDecoder, Decoder},
2020
enc::Encoder,
2121
error::{DecodeError, EncodeError},
22-
Decode, Encode,
22+
BorrowDecode, Decode, Encode,
2323
};
2424
use std::string::ToString;
2525

@@ -31,33 +31,31 @@ impl Encode for Uuid {
3131

3232
impl Encode for NonNilUuid {
3333
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)
3735
}
3836
}
3937

4038
impl Encode for Hyphenated {
4139
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)
4341
}
4442
}
4543

4644
impl Encode for Simple {
4745
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)
4947
}
5048
}
5149

5250
impl Encode for Urn {
5351
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)
5553
}
5654
}
5755

5856
impl Encode for Braced {
5957
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)
6159
}
6260
}
6361

@@ -66,11 +64,11 @@ impl<Context> Decode<Context> for Uuid {
6664
Ok(Uuid::from_bytes(Decode::decode(decoder)?))
6765
}
6866
}
69-
impl<'de, Context> bincode::BorrowDecode<'de, Context> for Uuid {
67+
impl<'de, Context> BorrowDecode<'de, Context> for Uuid {
7068
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>(
7169
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)?))
7472
}
7573
}
7674

@@ -81,11 +79,11 @@ impl<Context> Decode<Context> for NonNilUuid {
8179
NonNilUuid::try_from(uuid).map_err(|e| DecodeError::OtherString(e.to_string()))
8280
}
8381
}
84-
impl<'de, Context> bincode::BorrowDecode<'de, Context> for NonNilUuid {
82+
impl<'de, Context> BorrowDecode<'de, Context> for NonNilUuid {
8583
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>(
8684
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)?;
8987

9088
NonNilUuid::try_from(uuid).map_err(|e| DecodeError::OtherString(e.to_string()))
9189
}
@@ -99,71 +97,79 @@ mod bincode_tests {
9997
#[test]
10098
fn test_encode_readable_string() {
10199
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();
103101

104-
bincode::encode_to_vec(&u, config::standard())
102+
let bytes = bincode::encode_to_vec(&uuid, config::standard())
105103
.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);
106108
}
107109

108110
#[test]
109111
fn test_encode_hyphenated() {
110112
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();
112114

113-
bincode::encode_to_vec(&u, config::standard())
115+
let bytes = bincode::encode_to_vec(&uuid, config::standard())
114116
.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);
115121
}
116122

117123
#[test]
118124
fn test_encode_simple() {
119125
let uuid_str = "f9168c5eceb24faab6bf329bf39fa1e4";
120-
let u = Uuid::parse_str(uuid_str).unwrap();
126+
let uuid = Uuid::parse_str(uuid_str).unwrap();
121127

122-
bincode::encode_to_vec(&u, config::standard())
128+
let bytes = bincode::encode_to_vec(&uuid, config::standard())
123129
.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);
124134
}
125135

126136
#[test]
127137
fn test_encode_urn() {
128138
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();
130140

131-
bincode::encode_to_vec(&u, config::standard())
141+
let bytes = bincode::encode_to_vec(&uuid, config::standard())
132142
.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);
133147
}
134148

135149
#[test]
136150
fn test_encode_braced() {
137151
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();
139153

140-
bincode::encode_to_vec(&u, config::standard())
154+
let bytes = bincode::encode_to_vec(&uuid, config::standard())
141155
.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);
142160
}
143161

144162
#[test]
145163
fn test_encode_non_human_readable() {
146164
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();
148166

149-
bincode::encode_to_vec(&u, config::standard())
167+
let bytes = bincode::encode_to_vec(&uuid, config::standard())
150168
.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:?}."));
152171

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);
167173
}
168174

169175
#[test]

0 commit comments

Comments
 (0)