mirror of
https://github.com/uuid-rs/uuid.git
synced 2025-10-02 07:20:40 +00:00
stabilize UUIDv6-v8 support
This commit is contained in:
parent
e68b0108fa
commit
3df0aaa80d
11
benches/v7.rs
Normal file
11
benches/v7.rs
Normal file
@ -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());
|
||||||
|
}
|
@ -2,7 +2,7 @@
|
|||||||
//!
|
//!
|
||||||
//! If you enable the `v7` feature you can generate sortable UUIDs.
|
//! If you enable the `v7` feature you can generate sortable UUIDs.
|
||||||
|
|
||||||
#[cfg(all(uuid_unstable, feature = "v7"))]
|
#[cfg(feature = "v7")]
|
||||||
fn main() {
|
fn main() {
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
|
|
||||||
@ -13,5 +13,5 @@ fn main() {
|
|||||||
println!("{}", uuid);
|
println!("{}", uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(all(uuid_unstable, feature = "v7")))]
|
#[cfg(not(feature = "v7"))]
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
@ -95,7 +95,6 @@ impl Uuid {
|
|||||||
/// uuid.hyphenated().to_string(),
|
/// uuid.hyphenated().to_string(),
|
||||||
/// );
|
/// );
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
pub const fn max() -> Self {
|
pub const fn max() -> Self {
|
||||||
Uuid::from_bytes([0xFF; 16])
|
Uuid::from_bytes([0xFF; 16])
|
||||||
}
|
}
|
||||||
@ -600,7 +599,6 @@ impl Builder {
|
|||||||
/// Creates a `Builder` for a version 6 UUID using the supplied timestamp and node ID.
|
/// 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.
|
/// This method will encode the ticks, counter, and node ID in a sortable UUID.
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
pub const fn from_sorted_rfc4122_timestamp(
|
pub const fn from_sorted_rfc4122_timestamp(
|
||||||
ticks: u64,
|
ticks: u64,
|
||||||
counter: u16,
|
counter: u16,
|
||||||
@ -638,7 +636,6 @@ impl Builder {
|
|||||||
/// # Ok(())
|
/// # Ok(())
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
pub const fn from_unix_timestamp_millis(millis: u64, random_bytes: &[u8; 10]) -> Self {
|
pub const fn from_unix_timestamp_millis(millis: u64, random_bytes: &[u8; 10]) -> Self {
|
||||||
Builder(timestamp::encode_unix_timestamp_millis(
|
Builder(timestamp::encode_unix_timestamp_millis(
|
||||||
millis,
|
millis,
|
||||||
@ -650,7 +647,6 @@ impl Builder {
|
|||||||
///
|
///
|
||||||
/// This method won't interpret the given bytes in any way, except to set the appropriate
|
/// This method won't interpret the given bytes in any way, except to set the appropriate
|
||||||
/// bits for the UUID version and variant.
|
/// bits for the UUID version and variant.
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
pub const fn from_custom_bytes(custom_bytes: Bytes) -> Self {
|
pub const fn from_custom_bytes(custom_bytes: Bytes) -> Self {
|
||||||
Builder::from_bytes(custom_bytes)
|
Builder::from_bytes(custom_bytes)
|
||||||
.with_variant(Variant::RFC4122)
|
.with_variant(Variant::RFC4122)
|
||||||
|
18
src/lib.rs
18
src/lib.rs
@ -251,11 +251,11 @@ mod v3;
|
|||||||
mod v4;
|
mod v4;
|
||||||
#[cfg(feature = "v5")]
|
#[cfg(feature = "v5")]
|
||||||
mod v5;
|
mod v5;
|
||||||
#[cfg(all(uuid_unstable, feature = "v6"))]
|
#[cfg(feature = "v6")]
|
||||||
mod v6;
|
mod v6;
|
||||||
#[cfg(all(uuid_unstable, feature = "v7"))]
|
#[cfg(feature = "v7")]
|
||||||
mod v7;
|
mod v7;
|
||||||
#[cfg(all(uuid_unstable, feature = "v8"))]
|
#[cfg(feature = "v8")]
|
||||||
mod v8;
|
mod v8;
|
||||||
|
|
||||||
#[cfg(feature = "md5")]
|
#[cfg(feature = "md5")]
|
||||||
@ -312,16 +312,12 @@ pub enum Version {
|
|||||||
/// Version 5: SHA-1 hash.
|
/// Version 5: SHA-1 hash.
|
||||||
Sha1 = 5,
|
Sha1 = 5,
|
||||||
/// Version 6: Sortable Timestamp and node ID.
|
/// Version 6: Sortable Timestamp and node ID.
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
SortMac = 6,
|
SortMac = 6,
|
||||||
/// Version 7: Timestamp and random.
|
/// Version 7: Timestamp and random.
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
SortRand = 7,
|
SortRand = 7,
|
||||||
/// Version 8: Custom.
|
/// Version 8: Custom.
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
Custom = 8,
|
Custom = 8,
|
||||||
/// The "max" (all ones) UUID.
|
/// The "max" (all ones) UUID.
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
Max = 0xff,
|
Max = 0xff,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -575,13 +571,9 @@ impl Uuid {
|
|||||||
3 => Some(Version::Md5),
|
3 => Some(Version::Md5),
|
||||||
4 => Some(Version::Random),
|
4 => Some(Version::Random),
|
||||||
5 => Some(Version::Sha1),
|
5 => Some(Version::Sha1),
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
6 => Some(Version::SortMac),
|
6 => Some(Version::SortMac),
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
7 => Some(Version::SortRand),
|
7 => Some(Version::SortRand),
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
8 => Some(Version::Custom),
|
8 => Some(Version::Custom),
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
0xf => Some(Version::Max),
|
0xf => Some(Version::Max),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
@ -851,7 +843,6 @@ impl Uuid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Tests if the UUID is max (all ones).
|
/// Tests if the UUID is max (all ones).
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
pub const fn is_max(&self) -> bool {
|
pub const fn is_max(&self) -> bool {
|
||||||
self.as_u128() == u128::MAX
|
self.as_u128() == u128::MAX
|
||||||
}
|
}
|
||||||
@ -911,13 +902,11 @@ impl Uuid {
|
|||||||
|
|
||||||
Some(Timestamp::from_rfc4122(ticks, counter))
|
Some(Timestamp::from_rfc4122(ticks, counter))
|
||||||
}
|
}
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
Some(Version::SortMac) => {
|
Some(Version::SortMac) => {
|
||||||
let (ticks, counter) = timestamp::decode_sorted_rfc4122_timestamp(self);
|
let (ticks, counter) = timestamp::decode_sorted_rfc4122_timestamp(self);
|
||||||
|
|
||||||
Some(Timestamp::from_rfc4122(ticks, counter))
|
Some(Timestamp::from_rfc4122(ticks, counter))
|
||||||
}
|
}
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
Some(Version::SortRand) => {
|
Some(Version::SortRand) => {
|
||||||
let millis = timestamp::decode_unix_timestamp_millis(self);
|
let millis = timestamp::decode_unix_timestamp_millis(self);
|
||||||
|
|
||||||
@ -1184,7 +1173,6 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
#[cfg_attr(
|
#[cfg_attr(
|
||||||
all(
|
all(
|
||||||
target_arch = "wasm32",
|
target_arch = "wasm32",
|
||||||
|
@ -204,7 +204,6 @@ pub(crate) const fn decode_rfc4122_timestamp(uuid: &Uuid) -> (u64, u16) {
|
|||||||
(ticks, counter)
|
(ticks, counter)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
pub(crate) const fn encode_sorted_rfc4122_timestamp(
|
pub(crate) const fn encode_sorted_rfc4122_timestamp(
|
||||||
ticks: u64,
|
ticks: u64,
|
||||||
counter: u16,
|
counter: u16,
|
||||||
@ -228,7 +227,6 @@ pub(crate) const fn encode_sorted_rfc4122_timestamp(
|
|||||||
Uuid::from_fields(time_high, time_mid, time_low_and_version, &d4)
|
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) {
|
pub(crate) const fn decode_sorted_rfc4122_timestamp(uuid: &Uuid) -> (u64, u16) {
|
||||||
let bytes = uuid.as_bytes();
|
let bytes = uuid.as_bytes();
|
||||||
|
|
||||||
@ -246,7 +244,6 @@ pub(crate) const fn decode_sorted_rfc4122_timestamp(uuid: &Uuid) -> (u64, u16) {
|
|||||||
(ticks, counter)
|
(ticks, counter)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(uuid_unstable)]
|
|
||||||
pub(crate) const fn encode_unix_timestamp_millis(millis: u64, random_bytes: &[u8; 10]) -> Uuid {
|
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_high = ((millis >> 16) & 0xFFFF_FFFF) as u32;
|
||||||
let millis_low = (millis & 0xFFFF) as u16;
|
let millis_low = (millis & 0xFFFF) as u16;
|
||||||
@ -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)
|
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 {
|
pub(crate) const fn decode_unix_timestamp_millis(uuid: &Uuid) -> u64 {
|
||||||
let bytes = uuid.as_bytes();
|
let bytes = uuid.as_bytes();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user