Skip to content

Commit

Permalink
stabilize UUIDv6-v8 support
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Nov 16, 2023
1 parent e68b010 commit 3df0aaa
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 25 deletions.
11 changes: 11 additions & 0 deletions benches/v7.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#![cfg(all(feature = "v7", feature = "std"))]
#![feature(test)]
extern crate test;

use test::Bencher;
use uuid::Uuid;

#[bench]
fn now_v7(b: &mut Bencher) {
b.iter(|| Uuid::now_v7());
}
4 changes: 2 additions & 2 deletions examples/sortable_uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//!
//! If you enable the `v7` feature you can generate sortable UUIDs.

#[cfg(all(uuid_unstable, feature = "v7"))]
#[cfg(feature = "v7")]
fn main() {
use uuid::Uuid;

Expand All @@ -13,5 +13,5 @@ fn main() {
println!("{}", uuid);
}

#[cfg(not(all(uuid_unstable, feature = "v7")))]
#[cfg(not(feature = "v7"))]
fn main() {}
4 changes: 0 additions & 4 deletions src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ impl Uuid {
/// uuid.hyphenated().to_string(),
/// );
/// ```
#[cfg(uuid_unstable)]
pub const fn max() -> Self {
Uuid::from_bytes([0xFF; 16])
}
Expand Down Expand Up @@ -600,7 +599,6 @@ impl Builder {
/// Creates a `Builder` for a version 6 UUID using the supplied timestamp and node ID.
///
/// This method will encode the ticks, counter, and node ID in a sortable UUID.
#[cfg(uuid_unstable)]
pub const fn from_sorted_rfc4122_timestamp(
ticks: u64,
counter: u16,
Expand Down Expand Up @@ -638,7 +636,6 @@ impl Builder {
/// # Ok(())
/// # }
/// ```
#[cfg(uuid_unstable)]
pub const fn from_unix_timestamp_millis(millis: u64, random_bytes: &[u8; 10]) -> Self {
Builder(timestamp::encode_unix_timestamp_millis(
millis,
Expand All @@ -650,7 +647,6 @@ impl Builder {
///
/// This method won't interpret the given bytes in any way, except to set the appropriate
/// bits for the UUID version and variant.
#[cfg(uuid_unstable)]
pub const fn from_custom_bytes(custom_bytes: Bytes) -> Self {
Builder::from_bytes(custom_bytes)
.with_variant(Variant::RFC4122)
Expand Down
18 changes: 3 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,11 @@ mod v3;
mod v4;
#[cfg(feature = "v5")]
mod v5;
#[cfg(all(uuid_unstable, feature = "v6"))]
#[cfg(feature = "v6")]
mod v6;
#[cfg(all(uuid_unstable, feature = "v7"))]
#[cfg(feature = "v7")]
mod v7;
#[cfg(all(uuid_unstable, feature = "v8"))]
#[cfg(feature = "v8")]
mod v8;

#[cfg(feature = "md5")]
Expand Down Expand Up @@ -312,16 +312,12 @@ pub enum Version {
/// Version 5: SHA-1 hash.
Sha1 = 5,
/// Version 6: Sortable Timestamp and node ID.
#[cfg(uuid_unstable)]
SortMac = 6,
/// Version 7: Timestamp and random.
#[cfg(uuid_unstable)]
SortRand = 7,
/// Version 8: Custom.
#[cfg(uuid_unstable)]
Custom = 8,
/// The "max" (all ones) UUID.
#[cfg(uuid_unstable)]
Max = 0xff,
}

Expand Down Expand Up @@ -575,13 +571,9 @@ impl Uuid {
3 => Some(Version::Md5),
4 => Some(Version::Random),
5 => Some(Version::Sha1),
#[cfg(uuid_unstable)]
6 => Some(Version::SortMac),
#[cfg(uuid_unstable)]
7 => Some(Version::SortRand),
#[cfg(uuid_unstable)]
8 => Some(Version::Custom),
#[cfg(uuid_unstable)]
0xf => Some(Version::Max),
_ => None,
}
Expand Down Expand Up @@ -851,7 +843,6 @@ impl Uuid {
}

/// Tests if the UUID is max (all ones).
#[cfg(uuid_unstable)]
pub const fn is_max(&self) -> bool {
self.as_u128() == u128::MAX
}
Expand Down Expand Up @@ -911,13 +902,11 @@ impl Uuid {

Some(Timestamp::from_rfc4122(ticks, counter))
}
#[cfg(uuid_unstable)]
Some(Version::SortMac) => {
let (ticks, counter) = timestamp::decode_sorted_rfc4122_timestamp(self);

Some(Timestamp::from_rfc4122(ticks, counter))
}
#[cfg(uuid_unstable)]
Some(Version::SortRand) => {
let millis = timestamp::decode_unix_timestamp_millis(self);

Expand Down Expand Up @@ -1184,7 +1173,6 @@ mod tests {
}

#[test]
#[cfg(uuid_unstable)]
#[cfg_attr(
all(
target_arch = "wasm32",
Expand Down
4 changes: 0 additions & 4 deletions src/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,6 @@ pub(crate) const fn decode_rfc4122_timestamp(uuid: &Uuid) -> (u64, u16) {
(ticks, counter)
}

#[cfg(uuid_unstable)]
pub(crate) const fn encode_sorted_rfc4122_timestamp(
ticks: u64,
counter: u16,
Expand All @@ -228,7 +227,6 @@ pub(crate) const fn encode_sorted_rfc4122_timestamp(
Uuid::from_fields(time_high, time_mid, time_low_and_version, &d4)
}

#[cfg(uuid_unstable)]
pub(crate) const fn decode_sorted_rfc4122_timestamp(uuid: &Uuid) -> (u64, u16) {
let bytes = uuid.as_bytes();

Expand All @@ -246,7 +244,6 @@ pub(crate) const fn decode_sorted_rfc4122_timestamp(uuid: &Uuid) -> (u64, u16) {
(ticks, counter)
}

#[cfg(uuid_unstable)]
pub(crate) const fn encode_unix_timestamp_millis(millis: u64, random_bytes: &[u8; 10]) -> Uuid {
let millis_high = ((millis >> 16) & 0xFFFF_FFFF) as u32;
let millis_low = (millis & 0xFFFF) as u16;
Expand All @@ -268,7 +265,6 @@ pub(crate) const fn encode_unix_timestamp_millis(millis: u64, random_bytes: &[u8
Uuid::from_fields(millis_high, millis_low, random_and_version, &d4)
}

#[cfg(uuid_unstable)]
pub(crate) const fn decode_unix_timestamp_millis(uuid: &Uuid) -> u64 {
let bytes = uuid.as_bytes();

Expand Down

0 comments on commit 3df0aaa

Please sign in to comment.