Skip to content

Commit

Permalink
Add unstable borsh support
Browse files Browse the repository at this point in the history
  • Loading branch information
grovesNL committed Jun 16, 2023
1 parent 07052be commit c94dc37
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Continuous integration

env:
VERSION_FEATURES: "v1 v3 v4 v5 v6 v7 v8"
DEP_FEATURES: "slog serde arbitrary zerocopy"
DEP_FEATURES: "slog serde arbitrary borsh zerocopy"

on:
pull_request:
Expand Down
10 changes: 9 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ version = "1.3.4" # remember to update html_root_url in lib.rs
rustc-args = ["--cfg", "uuid_unstable"]
rustdoc-args = ["--cfg", "uuid_unstable"]
targets = ["x86_64-unknown-linux-gnu"]
features = ["serde", "arbitrary", "slog", "v1", "v3", "v4", "v5", "v6", "v7", "v8"]
features = ["serde", "arbitrary", "slog", "borsh", "v1", "v3", "v4", "v5", "v6", "v7", "v8"]

[package.metadata.playground]
features = ["serde", "v1", "v3", "v4", "v5", "v6", "v7", "v8"]
Expand Down Expand Up @@ -95,6 +95,14 @@ version = "1.1.3"
optional = true
version = "0.6"

# Public (unstable): Used in trait impls on `Uuid`
# Unstable: also need RUSTFLAGS="--cfg uuid_unstable" to work
# This feature may break between releases, or be removed entirely before
# stabilization.
[dependencies.borsh]
optional = true
version = "0.10.3"

# Private
# Don't depend on this optional feature directly: it may change at any time
# use the `rng` feature instead
Expand Down
2 changes: 2 additions & 0 deletions src/external.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#[cfg(feature = "arbitrary")]
pub(crate) mod arbitrary_support;
#[cfg(all(uuid_unstable, feature = "borsh"))]
pub(crate) mod borsh_support;
#[cfg(feature = "serde")]
pub(crate) mod serde_support;
#[cfg(feature = "slog")]
Expand Down
24 changes: 24 additions & 0 deletions src/external/borsh_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#[cfg(test)]
mod borsh_tests {
use crate::Uuid;
use std::string::ToString;
use borsh::{BorshDeserialize, BorshSerialize};

#[test]
fn test_serialize() {
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
let uuid = Uuid::parse_str(uuid_str).unwrap();
let uuid_bytes = uuid.as_bytes().to_vec();
let borsh_bytes = uuid.try_to_vec().unwrap();
assert_eq!(uuid_bytes, borsh_bytes);
}

#[test]
fn test_deserialize() {
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
let uuid = Uuid::parse_str(uuid_str).unwrap();
let uuid_bytes = uuid.as_bytes().to_vec();
let deserialized = Uuid::try_from_slice(&uuid_bytes).unwrap().to_string();
assert_eq!(uuid_str, deserialized);
}
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@
//! * `v8` - Version 8 UUIDs using user-defined data.
//! * `zerocopy` - adds support for zero-copy deserialization using the
//! `zerocopy` library.
//! * `borsh` - adds the ability to serialize and deserialize a UUID using
//! `borsh`.
//!
//! Unstable features may break between minor releases.
//!
Expand Down Expand Up @@ -435,6 +437,9 @@ pub enum Variant {
all(uuid_unstable, feature = "zerocopy"),
derive(AsBytes, FromBytes, Unaligned)
)]
#[cfg_attr(all(uuid_unstable, feature = "borsh"),
derive(borsh::BorshDeserialize, borsh::BorshSerialize)
)]
#[repr(transparent)]
pub struct Uuid(Bytes);

Expand Down

0 comments on commit c94dc37

Please sign in to comment.