chore(deps): uuidv7 stable (#1451)

This commit is contained in:
Conrad Ludgate 2023-12-16 19:21:04 +00:00 committed by GitHub
parent ec131c7c96
commit 7aeea1c050
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 9 additions and 30 deletions

7
Cargo.lock generated
View File

@ -141,6 +141,12 @@ dependencies = [
"num-traits", "num-traits",
] ]
[[package]]
name = "atomic"
version = "0.5.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c59bdb34bc650a32731b31bd8f0829cc15d24a708ee31559e0bb34f2bc320cba"
[[package]] [[package]]
name = "atomic-write-file" name = "atomic-write-file"
version = "0.1.2" version = "0.1.2"
@ -3758,6 +3764,7 @@ version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560" checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560"
dependencies = [ dependencies = [
"atomic",
"getrandom", "getrandom",
"serde", "serde",
] ]

View File

@ -39,7 +39,7 @@ semver = "1.0.20"
serde = { version = "1.0.193", features = ["derive"] } serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108" serde_json = "1.0.108"
tokio = { version = "1", features = ["full"] } tokio = { version = "1", features = ["full"] }
uuid = { version = "1.3", features = ["v4", "serde"] } uuid = { version = "1.3", features = ["v4", "v7", "serde"] }
whoami = "1.1.2" whoami = "1.1.2"
typed-builder = "0.18.0" typed-builder = "0.18.0"
pretty_assertions = "1.3.0" pretty_assertions = "1.3.0"

View File

@ -12,36 +12,8 @@ pub fn random_bytes<const N: usize>() -> [u8; N] {
ret ret
} }
// basically just ripped from the uuid crate. they have it as unstable, but we can use it fine.
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;
let random_and_version =
(random_bytes[0] as u16 | ((random_bytes[1] as u16) << 8) & 0x0FFF) | (0x7 << 12);
let mut d4 = [0; 8];
d4[0] = (random_bytes[2] & 0x3F) | 0x80;
d4[1] = random_bytes[3];
d4[2] = random_bytes[4];
d4[3] = random_bytes[5];
d4[4] = random_bytes[6];
d4[5] = random_bytes[7];
d4[6] = random_bytes[8];
d4[7] = random_bytes[9];
Uuid::from_fields(millis_high, millis_low, random_and_version, &d4)
}
pub fn uuid_v7() -> Uuid { pub fn uuid_v7() -> Uuid {
let bytes = random_bytes(); Uuid::now_v7()
let now: u64 = u64::try_from(
time::OffsetDateTime::now_utc().unix_timestamp_nanos() / 1_000_000,
)
.expect("Either you're in the past (1970) - or your in the far future (2554). Good for you");
encode_unix_timestamp_millis(now, &bytes)
} }
pub fn uuid_v4() -> String { pub fn uuid_v4() -> String {