Skip to content

Commit 2d40b0f

Browse files
Add support for bincode v2.0
1 parent cb19a46 commit 2d40b0f

File tree

4 files changed

+228
-5
lines changed

4 files changed

+228
-5
lines changed

Cargo.toml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ homepage = "https://github.com/uuid-rs/uuid"
2929
name = "uuid"
3030
readme = "README.md"
3131
repository = "https://github.com/uuid-rs/uuid"
32-
version = "1.16.0" # remember to update html_root_url in lib.rs
32+
version = "1.16.1" # remember to update html_root_url in lib.rs
3333
rust-version = "1.63.0"
3434

3535
[package.metadata.docs.rs]
3636
rustc-args = ["--cfg", "uuid_unstable"]
3737
rustdoc-args = ["--cfg", "uuid_unstable"]
3838
targets = ["x86_64-unknown-linux-gnu"]
39-
features = ["serde", "arbitrary", "slog", "borsh", "v1", "v3", "v4", "v5", "v6", "v7", "v8"]
39+
features = ["serde", "bincode", "arbitrary", "slog", "borsh", "v1", "v3", "v4", "v5", "v6", "v7", "v8"]
4040

4141
[package.metadata.playground]
42-
features = ["serde", "v1", "v3", "v4", "v5", "v6", "v7", "v8"]
42+
features = ["serde", "bincode", "v1", "v3", "v4", "v5", "v6", "v7", "v8"]
4343

4444
[lints.rust]
4545
unexpected_cfgs = { level = "allow", check-cfg = ['cfg(uuid_unstable)'] }
@@ -94,6 +94,12 @@ default-features = false
9494
optional = true
9595
version = "1.0.56"
9696

97+
# Public: Used in trait impls on `Uuid`
98+
[dependencies.bincode]
99+
default-features = false
100+
optional = true
101+
version = "2.0.1"
102+
97103
# Public: Used in trait impls on `Uuid`
98104
[dependencies.slog]
99105
optional = true
@@ -179,7 +185,7 @@ version = "0.3"
179185
optional = true
180186

181187
[dev-dependencies.bincode]
182-
version = "1.0"
188+
version = "2.0.1"
183189

184190
[dev-dependencies.serde_derive]
185191
version = "1.0.79"

src/external.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#[cfg(feature = "arbitrary")]
22
pub(crate) mod arbitrary_support;
3+
#[cfg(feature = "bincode")]
4+
pub(crate) mod bincode_support;
35
#[cfg(feature = "borsh")]
46
pub(crate) mod borsh_support;
57
#[cfg(feature = "serde")]

src/external/bincode_support.rs

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
// Copyright 2013-2014 The Rust Project Developers.
2+
// Copyright 2018 The Uuid Project Developers.
3+
//
4+
// See the COPYRIGHT file at the top-level directory of this distribution.
5+
//
6+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
7+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
8+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
9+
// option. This file may not be copied, modified, or distributed
10+
// except according to those terms.
11+
12+
use crate::{
13+
convert::TryFrom,
14+
fmt::{Braced, Hyphenated, Simple, Urn},
15+
non_nil::NonNilUuid,
16+
Uuid,
17+
};
18+
use bincode::{
19+
de::{BorrowDecoder, Decoder},
20+
enc::Encoder,
21+
error::{DecodeError, EncodeError},
22+
Decode, Encode,
23+
};
24+
use std::string::ToString;
25+
26+
impl Encode for Uuid {
27+
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
28+
Encode::encode(self.as_bytes(), encoder)
29+
}
30+
}
31+
32+
impl Encode for NonNilUuid {
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)
37+
}
38+
}
39+
40+
impl Encode for Hyphenated {
41+
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
42+
Encode::encode(self.as_uuid().as_bytes(), encoder)
43+
}
44+
}
45+
46+
impl Encode for Simple {
47+
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
48+
Encode::encode(self.as_uuid().as_bytes(), encoder)
49+
}
50+
}
51+
52+
impl Encode for Urn {
53+
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
54+
Encode::encode(self.as_uuid().as_bytes(), encoder)
55+
}
56+
}
57+
58+
impl Encode for Braced {
59+
fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
60+
Encode::encode(self.as_uuid().as_bytes(), encoder)
61+
}
62+
}
63+
64+
impl<Context> Decode<Context> for Uuid {
65+
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
66+
Ok(Uuid::from_bytes(Decode::decode(decoder)?))
67+
}
68+
}
69+
impl<'de, Context> bincode::BorrowDecode<'de, Context> for Uuid {
70+
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>(
71+
decoder: &mut D,
72+
) -> core::result::Result<Self, bincode::error::DecodeError> {
73+
Ok(Uuid::from_bytes(Decode::decode(decoder)?))
74+
}
75+
}
76+
77+
impl<Context> Decode<Context> for NonNilUuid {
78+
fn decode<D: Decoder<Context = Context>>(decoder: &mut D) -> Result<Self, DecodeError> {
79+
let uuid = Uuid::decode(decoder)?;
80+
81+
NonNilUuid::try_from(uuid).map_err(|e| DecodeError::OtherString(e.to_string()))
82+
}
83+
}
84+
impl<'de, Context> bincode::BorrowDecode<'de, Context> for NonNilUuid {
85+
fn borrow_decode<D: BorrowDecoder<'de, Context = Context>>(
86+
decoder: &mut D,
87+
) -> core::result::Result<Self, bincode::error::DecodeError> {
88+
let uuid = Uuid::decode(decoder)?;
89+
90+
NonNilUuid::try_from(uuid).map_err(|e| DecodeError::OtherString(e.to_string()))
91+
}
92+
}
93+
94+
#[cfg(test)]
95+
mod bincode_tests {
96+
use super::*;
97+
use bincode::config;
98+
99+
#[test]
100+
fn test_encode_readable_string() {
101+
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
102+
let u = Uuid::parse_str(uuid_str).unwrap();
103+
104+
bincode::encode_to_vec(&u, config::standard())
105+
.expect(&format!("Failed to encode {uuid_str}."));
106+
}
107+
108+
#[test]
109+
fn test_encode_hyphenated() {
110+
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
111+
let u = Uuid::parse_str(uuid_str).unwrap();
112+
113+
bincode::encode_to_vec(&u, config::standard())
114+
.expect(&format!("Failed to encode {uuid_str}."));
115+
}
116+
117+
#[test]
118+
fn test_encode_simple() {
119+
let uuid_str = "f9168c5eceb24faab6bf329bf39fa1e4";
120+
let u = Uuid::parse_str(uuid_str).unwrap();
121+
122+
bincode::encode_to_vec(&u, config::standard())
123+
.expect(&format!("Failed to encode {uuid_str}."));
124+
}
125+
126+
#[test]
127+
fn test_encode_urn() {
128+
let uuid_str = "urn:uuid:f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
129+
let u = Uuid::parse_str(uuid_str).unwrap();
130+
131+
bincode::encode_to_vec(&u, config::standard())
132+
.expect(&format!("Failed to encode {uuid_str}."));
133+
}
134+
135+
#[test]
136+
fn test_encode_braced() {
137+
let uuid_str = "{f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4}";
138+
let u = Uuid::parse_str(uuid_str).unwrap();
139+
140+
bincode::encode_to_vec(&u, config::standard())
141+
.expect(&format!("Failed to encode {uuid_str}."));
142+
}
143+
144+
#[test]
145+
fn test_encode_non_human_readable() {
146+
let uuid_bytes = b"F9168C5E-CEB2-4F";
147+
let u = Uuid::from_slice(uuid_bytes).unwrap();
148+
149+
bincode::encode_to_vec(&u, config::standard())
150+
.expect(&format!("{:?} failed to encode.", uuid_bytes));
151+
}
152+
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);
167+
}
168+
169+
#[test]
170+
fn test_decode_failure() {
171+
let bytes = "hello_world".as_bytes();
172+
let error = bincode::decode_from_slice::<Uuid, _>(bytes, config::standard())
173+
.expect_err(&format!("Should not have been able to decode {bytes:?}."));
174+
175+
match error {
176+
DecodeError::UnexpectedEnd { additional: 5 } => {}
177+
_ => panic!("Unexpected error"),
178+
}
179+
}
180+
181+
#[test]
182+
fn test_decode_non_nil_uuid() {
183+
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
184+
let uuid = Uuid::parse_str(uuid_str).unwrap();
185+
186+
let bytes = bincode::encode_to_vec(&uuid, config::standard())
187+
.expect(&format!("Failed to encode {uuid_str}."));
188+
189+
let (decoded_uuid, decoded_size) =
190+
bincode::decode_from_slice::<NonNilUuid, _>(&bytes, config::standard()).expect(
191+
&format!("Should have been able to decode {bytes:?} to NonNilUuid."),
192+
);
193+
194+
assert_eq!(uuid, decoded_uuid);
195+
assert_eq!(16, decoded_size);
196+
}
197+
198+
#[test]
199+
fn test_decode_nil_uuid() {
200+
let uuid = Uuid::nil();
201+
202+
let bytes = bincode::encode_to_vec(&uuid, config::standard())
203+
.expect(&format!("Failed to encode {}.", uuid.to_string()));
204+
205+
let error = bincode::decode_from_slice::<NonNilUuid, _>(&bytes, config::standard())
206+
.expect_err(&format!(
207+
"Should not have been able to decode {bytes:?} to NonNilUuid."
208+
));
209+
210+
match error {
211+
DecodeError::OtherString(s) if s == "the UUID is nil" => {}
212+
_ => panic!("Unexpected error"),
213+
}
214+
}
215+
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
#![doc(
213213
html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
214214
html_favicon_url = "https://www.rust-lang.org/favicon.ico",
215-
html_root_url = "https://docs.rs/uuid/1.16.0"
215+
html_root_url = "https://docs.rs/uuid/1.16.1"
216216
)]
217217

218218
#[cfg(any(feature = "std", test))]

0 commit comments

Comments
 (0)