diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index dabf4847..b75d64a2 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -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: diff --git a/Cargo.toml b/Cargo.toml index 8147edeb..4a87b0ea 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] @@ -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 diff --git a/src/external.rs b/src/external.rs index 219a9236..8ac12d0f 100644 --- a/src/external.rs +++ b/src/external.rs @@ -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")] diff --git a/src/external/borsh_support.rs b/src/external/borsh_support.rs new file mode 100644 index 00000000..3ff1b6bf --- /dev/null +++ b/src/external/borsh_support.rs @@ -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); + } +} diff --git a/src/lib.rs b/src/lib.rs index c4695040..2a98077d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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. //! @@ -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);