use crate::{error::*, timestamp, Bytes, Uuid, Variant, Version};
#[allow(missing_copy_implementations)]
#[derive(Debug)]
pub struct Builder(Uuid);
impl Uuid {
pub const fn nil() -> Self {
Uuid::from_bytes([0; 16])
}
#[cfg(uuid_unstable)]
pub const fn max() -> Self {
Uuid::from_bytes([0xFF; 16])
}
pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid {
Uuid::from_bytes([
(d1 >> 24) as u8,
(d1 >> 16) as u8,
(d1 >> 8) as u8,
d1 as u8,
(d2 >> 8) as u8,
d2 as u8,
(d3 >> 8) as u8,
d3 as u8,
d4[0],
d4[1],
d4[2],
d4[3],
d4[4],
d4[5],
d4[6],
d4[7],
])
}
pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Uuid {
Uuid::from_bytes([
d1 as u8,
(d1 >> 8) as u8,
(d1 >> 16) as u8,
(d1 >> 24) as u8,
(d2) as u8,
(d2 >> 8) as u8,
d3 as u8,
(d3 >> 8) as u8,
d4[0],
d4[1],
d4[2],
d4[3],
d4[4],
d4[5],
d4[6],
d4[7],
])
}
pub const fn from_u128(v: u128) -> Self {
Uuid::from_bytes([
(v >> 120) as u8,
(v >> 112) as u8,
(v >> 104) as u8,
(v >> 96) as u8,
(v >> 88) as u8,
(v >> 80) as u8,
(v >> 72) as u8,
(v >> 64) as u8,
(v >> 56) as u8,
(v >> 48) as u8,
(v >> 40) as u8,
(v >> 32) as u8,
(v >> 24) as u8,
(v >> 16) as u8,
(v >> 8) as u8,
v as u8,
])
}
pub const fn from_u128_le(v: u128) -> Self {
Uuid::from_bytes([
v as u8,
(v >> 8) as u8,
(v >> 16) as u8,
(v >> 24) as u8,
(v >> 32) as u8,
(v >> 40) as u8,
(v >> 48) as u8,
(v >> 56) as u8,
(v >> 64) as u8,
(v >> 72) as u8,
(v >> 80) as u8,
(v >> 88) as u8,
(v >> 96) as u8,
(v >> 104) as u8,
(v >> 112) as u8,
(v >> 120) as u8,
])
}
pub const fn from_u64_pair(high_bits: u64, low_bits: u64) -> Self {
Uuid::from_bytes([
(high_bits >> 56) as u8,
(high_bits >> 48) as u8,
(high_bits >> 40) as u8,
(high_bits >> 32) as u8,
(high_bits >> 24) as u8,
(high_bits >> 16) as u8,
(high_bits >> 8) as u8,
high_bits as u8,
(low_bits >> 56) as u8,
(low_bits >> 48) as u8,
(low_bits >> 40) as u8,
(low_bits >> 32) as u8,
(low_bits >> 24) as u8,
(low_bits >> 16) as u8,
(low_bits >> 8) as u8,
low_bits as u8,
])
}
pub fn from_slice(b: &[u8]) -> Result<Uuid, Error> {
if b.len() != 16 {
return Err(Error(ErrorKind::ByteLength { len: b.len() }));
}
let mut bytes: Bytes = [0; 16];
bytes.copy_from_slice(b);
Ok(Uuid::from_bytes(bytes))
}
pub fn from_slice_le(b: &[u8]) -> Result<Uuid, Error> {
if b.len() != 16 {
return Err(Error(ErrorKind::ByteLength { len: b.len() }));
}
let mut bytes: Bytes = [0; 16];
bytes.copy_from_slice(b);
Ok(Uuid::from_bytes_le(bytes))
}
pub const fn from_bytes(bytes: Bytes) -> Uuid {
Uuid(bytes)
}
pub const fn from_bytes_le(b: Bytes) -> Uuid {
Uuid([
b[3], b[2], b[1], b[0], b[5], b[4], b[7], b[6], b[8], b[9], b[10], b[11], b[12], b[13],
b[14], b[15],
])
}
pub fn from_bytes_ref(bytes: &Bytes) -> &Uuid {
unsafe { &*(bytes as *const Bytes as *const Uuid) }
}
}
impl Builder {
pub const fn from_bytes(b: Bytes) -> Self {
Builder(Uuid::from_bytes(b))
}
pub const fn from_bytes_le(b: Bytes) -> Self {
Builder(Uuid::from_bytes_le(b))
}
pub const fn from_rfc4122_timestamp(ticks: u64, counter: u16, node_id: &[u8; 6]) -> Self {
Builder(timestamp::encode_rfc4122_timestamp(ticks, counter, node_id))
}
pub const fn from_md5_bytes(md5_bytes: Bytes) -> Self {
Builder(Uuid::from_bytes(md5_bytes))
.with_variant(Variant::RFC4122)
.with_version(Version::Md5)
}
pub const fn from_random_bytes(random_bytes: Bytes) -> Self {
Builder(Uuid::from_bytes(random_bytes))
.with_variant(Variant::RFC4122)
.with_version(Version::Random)
}
pub const fn from_sha1_bytes(sha1_bytes: Bytes) -> Self {
Builder(Uuid::from_bytes(sha1_bytes))
.with_variant(Variant::RFC4122)
.with_version(Version::Sha1)
}
#[cfg(uuid_unstable)]
pub const fn from_sorted_rfc4122_timestamp(
ticks: u64,
counter: u16,
node_id: &[u8; 6],
) -> Self {
Builder(timestamp::encode_sorted_rfc4122_timestamp(
ticks, counter, node_id,
))
}
#[cfg(uuid_unstable)]
pub const fn from_unix_timestamp_millis(millis: u64, random_bytes: &[u8; 10]) -> Self {
Builder(timestamp::encode_unix_timestamp_millis(
millis,
random_bytes,
))
}
#[cfg(uuid_unstable)]
pub const fn from_custom_bytes(custom_bytes: Bytes) -> Self {
Builder::from_bytes(custom_bytes)
.with_variant(Variant::RFC4122)
.with_version(Version::Custom)
}
pub fn from_slice(b: &[u8]) -> Result<Self, Error> {
Ok(Builder(Uuid::from_slice(b)?))
}
pub fn from_slice_le(b: &[u8]) -> Result<Self, Error> {
Ok(Builder(Uuid::from_slice_le(b)?))
}
pub const fn from_fields(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self {
Builder(Uuid::from_fields(d1, d2, d3, d4))
}
pub const fn from_fields_le(d1: u32, d2: u16, d3: u16, d4: &[u8; 8]) -> Self {
Builder(Uuid::from_fields_le(d1, d2, d3, d4))
}
pub const fn from_u128(v: u128) -> Self {
Builder(Uuid::from_u128(v))
}
pub const fn from_u128_le(v: u128) -> Self {
Builder(Uuid::from_u128_le(v))
}
pub const fn nil() -> Self {
Builder(Uuid::nil())
}
pub fn set_variant(&mut self, v: Variant) -> &mut Self {
*self = Builder(self.0).with_variant(v);
self
}
pub const fn with_variant(mut self, v: Variant) -> Self {
let byte = (self.0).0[8];
(self.0).0[8] = match v {
Variant::NCS => byte & 0x7f,
Variant::RFC4122 => (byte & 0x3f) | 0x80,
Variant::Microsoft => (byte & 0x1f) | 0xc0,
Variant::Future => byte | 0xe0,
};
self
}
pub fn set_version(&mut self, v: Version) -> &mut Self {
*self = Builder(self.0).with_version(v);
self
}
pub const fn with_version(mut self, v: Version) -> Self {
(self.0).0[6] = ((self.0).0[6] & 0x0f) | ((v as u8) << 4);
self
}
pub const fn as_uuid(&self) -> &Uuid {
&self.0
}
pub const fn into_uuid(self) -> Uuid {
self.0
}
}