Skip to content

Commit

Permalink
Add borsh support
Browse files Browse the repository at this point in the history
  • Loading branch information
grovesNL committed Jun 15, 2023
1 parent 07052be commit a24c642
Show file tree
Hide file tree
Showing 5 changed files with 52 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
7 changes: 6 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 @@ -86,6 +86,11 @@ version = "2"
optional = true
version = "1.1.3"

# Public: Used in trait impls on `Uuid`
[dependencies.borsh]
optional = true
version = "0.10.3"

# Public (unstable): Used in `zerocopy` derive
# Unstable: also need RUSTFLAGS="--cfg uuid_unstable" to work
# This feature may break between releases, or be removed entirely before
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(feature = "borsh")]
pub(crate) mod borsh_support;
#[cfg(feature = "serde")]
pub(crate) mod serde_support;
#[cfg(feature = "slog")]
Expand Down
41 changes: 41 additions & 0 deletions src/external/borsh_support.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use crate::Uuid;
use std::io::{Read, Result, Write};
use borsh::{BorshDeserialize, BorshSerialize};

impl BorshSerialize for Uuid {
fn serialize<W: Write>(&self, writer: &mut W) -> Result<()> {
BorshSerialize::serialize(&self.as_bytes(), writer)
}
}

impl BorshDeserialize for Uuid {
fn deserialize_reader<R: Read>(reader: &mut R) -> Result<Self> {
Ok(Self::from_bytes(BorshDeserialize::deserialize_reader(
reader,
)?))
}
}

#[cfg(test)]
mod borsh_tests {
use super::*;

#[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() {
use std::string::ToString;
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);
}
}
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@
//! `serde`.
//! * `arbitrary` - adds an `Arbitrary` trait implementation to `Uuid` for
//! fuzzing.
//! * `borsh` - adds the ability to serialize and deserialize a UUID using
//! `borsh`.
//! * `fast-rng` - uses a faster algorithm for generating random UUIDs.
//! This feature requires more dependencies to compile, but is just as suitable for
//! UUIDs as the default algorithm.
Expand Down

0 comments on commit a24c642

Please sign in to comment.