Merge pull request #686 from pod2co/borsh

Add `borsh` support
This commit is contained in:
Ashley Mannix 2023-06-27 09:21:13 +10:00 committed by GitHub
commit 952f75fb1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 41 additions and 2 deletions

View File

@ -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:

View File

@ -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

View File

@ -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")]

24
src/external/borsh_support.rs vendored Normal file
View File

@ -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);
}
}

View File

@ -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);