Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Borsh serialization support #9

Merged
merged 1 commit into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
### Added

- Added support for the `uuid` crate's `bytemuck` feature.
- Added support for [Borsh](https://borsh.io/) serialization via the `uuid` crate's `borsh` feature.

## 0.6.0 - 2023-06-24

Expand Down Expand Up @@ -41,7 +42,7 @@ This project uses [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

### Added

- Added `PartialEq<T>` for `Vec<u8>` and `&[u8; 16]`.
- Added `PartialEq<T>` for `Vec<u8>` and `&[u8; 16]`.

### Changed

Expand Down
19 changes: 11 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ readme = "README.md"
rust-version = "1.67.1"

[features]
default = ["random"]
default = ["fast-rng"]
arbitrary = ["uuid/arbitrary", "arbitrary/derive"] # Add support for arbitrary types
random = ["uuid/v4"] # Create random ShortGuid IDs
fast-rng = ["uuid/fast-rng"] # Use a faster (but still sufficiently random) RNG
fast-rng = ["random", "uuid/fast-rng"] # Use a faster (but still sufficiently random) RNG
serde = ["dep:serde", "uuid/serde"] # Serialization and deserialization support
# zerocopy = ["dep:zerocopy", "uuid/zerocopy"] # Zerocopy support
bytemuck = ["dep:bytemuck", "uuid/bytemuck"] # Bytemuck support
borsh = ["dep:borsh", "dep:borsh-derive", "uuid/borsh"] # Borsh support

[[example]]
name = "shortguid"
Expand All @@ -31,15 +32,17 @@ required-features = ["serde"]

[dependencies]
arbitrary = { version = "1.3.2", optional = true }
base64 = "0.21.5"
bytemuck = { version = "1.14.0", optional = true, features = ["derive"] }
serde = { version = "1.0.193", optional = true }
uuid = "1.6.1"
zerocopy = { version = "0.7.32", optional = true, features = ["derive"] }
base64 = "0.22.1"
borsh = { version = "1.5.0", optional = true, features = ["derive"] }
borsh-derive = { version = "1.5.0", optional = true }
bytemuck = { version = "1.15.0", optional = true, features = ["derive"] }
serde = { version = "1.0.200", optional = true }
uuid = "1.8.0"
zerocopy = { version = "0.7.33", optional = true, features = ["derive"] }

[dev-dependencies]
hex = "0.4.3"
clap = "4.4.11"
clap = "4.5.4"
serde_test = "1.0.176"

[package.metadata.docs.rs]
Expand Down
44 changes: 44 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@
//! let random = ShortGuid::new_random();
//! assert_ne!(from_uuid, random);
//! ```
//!
//! # Create features
//!
//! Other crate features can also be useful beyond the version support:
//!
//! * `serde` - adds the ability to serialize and deserialize a UUID using
//! `serde`.
//! * `borsh` - adds the ability to serialize and deserialize a UUID using
//! `borsh`.
//! * `arbitrary` - adds an `Arbitrary` trait implementation to `Uuid` for
//! fuzzing.
//! * `random` - adds the ability to generate a random [`ShortGuid`]s.
//! * `fast-rng` - uses a faster algorithm for generating random [`ShortGuid`]s.
//! This feature requires more dependencies to compile, but is just as suitable for
//! [`ShortGuid`] as the default algorithm. Implies `random`.
//! * `bytemuck` - adds a `Pod` trait implementation to `Uuid` for byte manipulation

// only enables the `doc_cfg` feature when
// the `docsrs` configuration attribute is defined
Expand Down Expand Up @@ -56,6 +72,10 @@ use uuid::Uuid;
// feature = "zerocopy",
// derive(zerocopy::AsBytes, zerocopy::FromBytes, zerocopy::Unaligned)
// )]
#[cfg_attr(
feature = "borsh",
derive(borsh_derive::BorshDeserialize, borsh_derive::BorshSerialize)
)]
#[cfg_attr(
feature = "bytemuck",
derive(bytemuck::Zeroable, bytemuck::Pod, bytemuck::TransparentWrapper)
Expand Down Expand Up @@ -610,3 +630,27 @@ mod tests {
assert_eq!(id, slice);
}
}

#[cfg(all(test, feature = "borsh"))]
mod borsh_tests {
use super::*;
use std::string::ToString;

#[test]
fn test_serialize() {
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
let sg = ShortGuid::from_str(uuid_str).unwrap();
let sg_bytes = sg.as_bytes().to_vec();
let borsh_bytes = borsh::to_vec(&sg).unwrap();
assert_eq!(sg_bytes, borsh_bytes);
}

#[test]
fn test_deserialize() {
let uuid_str = "f9168c5e-ceb2-4faa-b6bf-329bf39fa1e4";
let sg = ShortGuid::from_str(uuid_str).unwrap();
let sg_bytes = sg.as_bytes().to_vec();
let deserialized = borsh::from_slice::<Uuid>(&sg_bytes).unwrap().to_string();
assert_eq!(uuid_str, deserialized);
}
}
Loading