Skip to content

Commit

Permalink
Merge main into feature/stabilize
Browse files Browse the repository at this point in the history
# Conflicts:
#	Cargo.toml
  • Loading branch information
sunsided committed May 10, 2024
2 parents de969ae + a214e60 commit 0aa8e1b
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 9 deletions.
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"]
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
serde = ["dep:serde", "uuid/serde"] # Serialization and deserialization support
# zerocopy = ["dep:zerocopy", "uuid/zerocopy"] # Zerocopy support
bytemuck = ["dep:bytemuck", "uuid/bytemuck"] # Bytemuck support
default = ["fast-rng"]
arbitrary = ["uuid/arbitrary", "arbitrary/derive"] # Add support for arbitrary types
random = ["uuid/v4"] # Create random ShortGuid IDs
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 @@ -32,14 +33,16 @@ required-features = ["serde"]
[dependencies]
arbitrary = { version = "1.3.2", optional = true }
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
43 changes: 43 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! # shortguid
//!
//! Provides short, URL-safe UUID representations.
//!
//! ```
Expand All @@ -10,6 +12,19 @@
//! 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`, enabled by default.
//! * `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 +71,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 +629,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);
}
}
7 changes: 7 additions & 0 deletions tea.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# https://tea.xyz/what-is-this-file
---
version: 1.0.0
codeOwners:
- '0xfB0cb2c716Ceb7A207C18c540c8EfdDf71B64689'
quorum: 1

0 comments on commit 0aa8e1b

Please sign in to comment.