reverting to origin

This commit is contained in:
Tilen Pintarič 2022-08-05 10:29:44 +02:00
parent 7188b00a9e
commit 824315deb7
11 changed files with 107 additions and 226 deletions

View File

@ -1,9 +0,0 @@
max_width = 80
hard_tabs = false
tab_spaces = 4
newline_style = "Unix"
wrap_comments = true
normalize_comments = true
merge_derives = true
force_explicit_abi = true
unstable_features = false

View File

@ -12,7 +12,7 @@ use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned};
use std::fmt;
use syn::{LitStr, spanned::Spanned};
use syn::{spanned::Spanned, LitStr};
mod error;
mod parser;
@ -22,32 +22,34 @@ mod parser;
pub fn parse_lit(input: TokenStream) -> TokenStream {
build_uuid(input.clone()).unwrap_or_else(|e| {
let msg = e.to_string();
let span =
match e {
Error::UuidParse(lit, error::Error(error::ErrorKind::Char {
character,
index,
})) => {
let mut bytes = character as u32;
let mut width = 0;
while bytes != 0 {
bytes >>= 4;
width += 1;
}
let mut s = proc_macro2::Literal::string("");
s.set_span(lit.span());
s.subspan(index..index + width - 1)
let span = match e {
Error::UuidParse(
lit,
error::Error(error::ErrorKind::Char { character, index }),
) => {
let mut bytes = character as u32;
let mut width = 0;
while bytes != 0 {
bytes >>= 4;
width += 1;
}
Error::UuidParse(lit, error::Error(
error::ErrorKind::GroupLength { index, len, .. },
)) => {
let mut s = proc_macro2::Literal::string("");
s.set_span(lit.span());
s.subspan(index..index + len)
}
_ => None,
let mut s = proc_macro2::Literal::string("");
s.set_span(lit.span());
s.subspan(index..index + width - 1)
}
.unwrap_or_else(|| TokenStream2::from(input).span());
Error::UuidParse(
lit,
error::Error(error::ErrorKind::GroupLength {
index, len, ..
}),
) => {
let mut s = proc_macro2::Literal::string("");
s.set_span(lit.span());
s.subspan(index..index + len)
}
_ => None,
}
.unwrap_or_else(|| TokenStream2::from(input).span());
TokenStream::from(quote_spanned! {span=>
compile_error!(#msg)

View File

@ -21,9 +21,7 @@ mod tests {
#[test]
fn test_arbitrary() {
let mut bytes = Unstructured::new(&[
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
]);
let mut bytes = Unstructured::new(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
let uuid = Uuid::arbitrary(&mut bytes).unwrap();

View File

@ -21,14 +21,9 @@ use serde::{
};
impl Serialize for Uuid {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
serializer.serialize_str(
self.hyphenated().encode_lower(&mut Uuid::encode_buffer()),
)
serializer.serialize_str(self.hyphenated().encode_lower(&mut Uuid::encode_buffer()))
} else {
serializer.serialize_bytes(self.as_bytes())
}
@ -36,45 +31,31 @@ impl Serialize for Uuid {
}
impl Serialize for Hyphenated {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}
impl Serialize for Simple {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}
impl Serialize for Urn {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}
impl Serialize for Braced {
fn serialize<S: Serializer>(
&self,
serializer: S,
) -> Result<S::Ok, S::Error> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
}
}
impl<'de> Deserialize<'de> for Uuid {
fn deserialize<D: Deserializer<'de>>(
deserializer: D,
) -> Result<Self, D::Error> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
fn de_error<E: de::Error>(e: Error) -> E {
E::custom(format_args!("UUID parsing failed: {}", e))
}
@ -85,24 +66,15 @@ impl<'de> Deserialize<'de> for Uuid {
impl<'vi> de::Visitor<'vi> for UuidVisitor {
type Value = Uuid;
fn expecting(
&self,
formatter: &mut fmt::Formatter<'_>,
) -> fmt::Result {
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "a UUID string")
}
fn visit_str<E: de::Error>(
self,
value: &str,
) -> Result<Uuid, E> {
fn visit_str<E: de::Error>(self, value: &str) -> Result<Uuid, E> {
value.parse::<Uuid>().map_err(de_error)
}
fn visit_bytes<E: de::Error>(
self,
value: &[u8],
) -> Result<Uuid, E> {
fn visit_bytes<E: de::Error>(self, value: &[u8]) -> Result<Uuid, E> {
Uuid::from_slice(value).map_err(de_error)
}
@ -141,17 +113,11 @@ impl<'de> Deserialize<'de> for Uuid {
impl<'vi> de::Visitor<'vi> for UuidBytesVisitor {
type Value = Uuid;
fn expecting(
&self,
formatter: &mut fmt::Formatter<'_>,
) -> fmt::Result {
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(formatter, "bytes")
}
fn visit_bytes<E: de::Error>(
self,
value: &[u8],
) -> Result<Uuid, E> {
fn visit_bytes<E: de::Error>(self, value: &[u8]) -> Result<Uuid, E> {
Uuid::from_slice(value).map_err(de_error)
}
}
@ -169,10 +135,7 @@ pub mod compact {
/// Serialize from a [`Uuid`] as a `[u8; 16]`
///
/// [`Uuid`]: ../../struct.Uuid.html
pub fn serialize<S>(
u: &crate::Uuid,
serializer: S,
) -> Result<S::Ok, S::Error>
pub fn serialize<S>(u: &crate::Uuid, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
@ -292,10 +255,7 @@ mod serde_tests {
let uuid_bytes = b"F9168C5E-CEB2-4F";
let u = Uuid::from_slice(uuid_bytes).unwrap();
serde_test::assert_de_tokens(
&u.readable(),
&[serde_test::Token::Bytes(uuid_bytes)],
);
serde_test::assert_de_tokens(&u.readable(), &[serde_test::Token::Bytes(uuid_bytes)]);
}
#[test]

View File

@ -140,12 +140,10 @@ impl Uuid {
}
const UPPER: [u8; 16] = [
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B',
b'C', b'D', b'E', b'F',
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E', b'F',
];
const LOWER: [u8; 16] = [
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'a', b'b',
b'c', b'd', b'e', b'f',
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'a', b'b', b'c', b'd', b'e', b'f',
];
#[inline]
@ -190,11 +188,7 @@ const fn format_hyphenated(src: &[u8; 16], upper: bool) -> [u8; 36] {
}
#[inline]
fn encode_simple<'b>(
src: &[u8; 16],
buffer: &'b mut [u8],
upper: bool,
) -> &'b mut str {
fn encode_simple<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
let buf = &mut buffer[..Simple::LENGTH];
let dst = buf.as_mut_ptr();
@ -207,11 +201,7 @@ fn encode_simple<'b>(
}
#[inline]
fn encode_hyphenated<'b>(
src: &[u8; 16],
buffer: &'b mut [u8],
upper: bool,
) -> &'b mut str {
fn encode_hyphenated<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
let buf = &mut buffer[..Hyphenated::LENGTH];
let dst = buf.as_mut_ptr();
@ -224,11 +214,7 @@ fn encode_hyphenated<'b>(
}
#[inline]
fn encode_braced<'b>(
src: &[u8; 16],
buffer: &'b mut [u8],
upper: bool,
) -> &'b mut str {
fn encode_braced<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
let buf = &mut buffer[..Braced::LENGTH];
buf[0] = b'{';
buf[Braced::LENGTH - 1] = b'}';
@ -244,11 +230,7 @@ fn encode_braced<'b>(
}
#[inline]
fn encode_urn<'b>(
src: &[u8; 16],
buffer: &'b mut [u8],
upper: bool,
) -> &'b mut str {
fn encode_urn<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
let buf = &mut buffer[..Urn::LENGTH];
buf[..9].copy_from_slice(b"urn:uuid:");

View File

@ -402,26 +402,26 @@ pub struct Uuid(Bytes);
impl Uuid {
/// UUID namespace for Domain Name System (DNS).
pub const NAMESPACE_DNS: Self = Uuid([
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0,
0x4f, 0xd4, 0x30, 0xc8,
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
0xc8,
]);
/// UUID namespace for ISO Object Identifiers (OIDs).
pub const NAMESPACE_OID: Self = Uuid([
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0,
0x4f, 0xd4, 0x30, 0xc8,
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
0xc8,
]);
/// UUID namespace for Uniform Resource Locators (URLs).
pub const NAMESPACE_URL: Self = Uuid([
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0,
0x4f, 0xd4, 0x30, 0xc8,
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
0xc8,
]);
/// UUID namespace for X.500 Distinguished Names (DNs).
pub const NAMESPACE_X500: Self = Uuid([
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0,
0x4f, 0xd4, 0x30, 0xc8,
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
0xc8,
]);
/// Returns the variant of the UUID structure.
@ -620,8 +620,7 @@ impl Uuid {
let d3 = (self.as_bytes()[6] as u16) | (self.as_bytes()[7] as u16) << 8;
let d4: &[u8; 8] =
convert::TryInto::try_into(&self.as_bytes()[8..16]).unwrap();
let d4: &[u8; 8] = convert::TryInto::try_into(&self.as_bytes()[8..16]).unwrap();
(d1, d2, d3, d4)
}
@ -805,9 +804,9 @@ impl Uuid {
/// ```
pub const fn to_bytes_le(&self) -> Bytes {
[
self.0[3], self.0[2], self.0[1], self.0[0], self.0[5], self.0[4],
self.0[7], self.0[6], self.0[8], self.0[9], self.0[10], self.0[11],
self.0[12], self.0[13], self.0[14], self.0[15],
self.0[3], self.0[2], self.0[1], self.0[0], self.0[5], self.0[4], self.0[7], self.0[6],
self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14],
self.0[15],
]
}
@ -891,15 +890,15 @@ mod tests {
pub const fn new() -> Uuid {
Uuid::from_bytes([
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAA, 0xB6, 0xBF, 0x32,
0x9B, 0xF3, 0x9F, 0xA1, 0xE4,
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAA, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
0xA1, 0xE4,
])
}
pub const fn new2() -> Uuid {
Uuid::from_bytes([
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAB, 0xB6, 0xBF, 0x32,
0x9B, 0xF3, 0x9F, 0xA1, 0xE4,
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAB, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
0xA1, 0xE4,
])
}
@ -993,9 +992,8 @@ mod tests {
fn test_nil() {
let nil = Uuid::nil();
let not_nil = new();
let from_bytes = Uuid::from_bytes([
4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87,
]);
let from_bytes =
Uuid::from_bytes([4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87]);
assert_eq!(from_bytes.get_version(), None);
@ -1031,8 +1029,7 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_get_version_v3() {
let uuid =
Uuid::new_v3(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
let uuid = Uuid::new_v3(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
assert_eq!(uuid.get_version().unwrap(), Version::Md5);
assert_eq!(uuid.get_version_num(), 3);
@ -1042,16 +1039,11 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_get_variant() {
let uuid1 = new();
let uuid2 =
Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
let uuid3 =
Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
let uuid4 =
Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap();
let uuid5 =
Uuid::parse_str("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap();
let uuid6 =
Uuid::parse_str("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap();
let uuid2 = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
let uuid3 = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
let uuid4 = Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap();
let uuid5 = Uuid::parse_str("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap();
let uuid6 = Uuid::parse_str("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap();
assert_eq!(uuid1.get_variant(), Variant::RFC4122);
assert_eq!(uuid2.get_variant(), Variant::RFC4122);
@ -1350,8 +1342,8 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_from_slice() {
let b = [
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3,
0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8,
];
let u = Uuid::from_slice(&b).unwrap();
@ -1364,8 +1356,8 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_from_bytes() {
let b = [
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3,
0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8,
];
let u = Uuid::from_bytes(b);
@ -1391,8 +1383,8 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_bytes_roundtrip() {
let b_in: crate::Bytes = [
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3,
0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8,
];
let u = Uuid::from_slice(&b_in).unwrap();
@ -1406,8 +1398,8 @@ mod tests {
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_bytes_le_roundtrip() {
let b = [
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3,
0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
0xd7, 0xd8,
];
let u1 = Uuid::from_bytes(b);

View File

@ -1,6 +1,6 @@
#[cfg(feature = "v3")]
pub(crate) fn hash(ns: &[u8], src: &[u8]) -> [u8; 16] {
use private_md_5::{Md5, Digest};
use private_md_5::{Digest, Md5};
let mut hasher = Md5::new();

View File

@ -142,10 +142,9 @@ const fn try_parse(input: &[u8]) -> Result<[u8; 16], InvalidUuid> {
// - `UUID` for a regular hyphenated UUID
(36, s)
| (38, [b'{', s @ .., b'}'])
| (
45,
[b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..],
) => parse_hyphenated(s),
| (45, [b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..]) => {
parse_hyphenated(s)
}
// Any other shaped input is immediately invalid
_ => Err(()),
};
@ -279,15 +278,10 @@ mod tests {
#[test]
fn test_parse_uuid_v4_valid() {
let from_hyphenated =
Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
let from_simple =
Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c8").unwrap();
let from_urn =
Uuid::parse_str("urn:uuid:67e55044-10b1-426f-9247-bb680e5fe0c8")
.unwrap();
let from_guid =
Uuid::parse_str("{67e55044-10b1-426f-9247-bb680e5fe0c8}").unwrap();
let from_hyphenated = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
let from_simple = Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c8").unwrap();
let from_urn = Uuid::parse_str("urn:uuid:67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
let from_guid = Uuid::parse_str("{67e55044-10b1-426f-9247-bb680e5fe0c8}").unwrap();
assert_eq!(from_hyphenated, from_simple);
assert_eq!(from_hyphenated, from_urn);
@ -298,13 +292,8 @@ mod tests {
assert!(Uuid::parse_str("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4").is_ok());
assert!(Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c8").is_ok());
assert!(Uuid::parse_str("01020304-1112-2122-3132-414243444546").is_ok());
assert!(Uuid::parse_str(
"urn:uuid:67e55044-10b1-426f-9247-bb680e5fe0c8"
)
.is_ok());
assert!(
Uuid::parse_str("{6d93bade-bd9f-4e13-8914-9474e1e3567b}").is_ok()
);
assert!(Uuid::parse_str("urn:uuid:67e55044-10b1-426f-9247-bb680e5fe0c8").is_ok());
assert!(Uuid::parse_str("{6d93bade-bd9f-4e13-8914-9474e1e3567b}").is_ok());
// Nil
let nil = Uuid::nil();
@ -527,9 +516,6 @@ mod tests {
#[test]
fn test_try_parse_ascii_non_utf8() {
assert!(Uuid::try_parse_ascii(
b"67e55044-10b1-426f-9247-bb680e5\0e0c8"
)
.is_err());
assert!(Uuid::try_parse_ascii(b"67e55044-10b1-426f-9247-bb680e5\0e0c8").is_err());
}
}

View File

@ -71,15 +71,10 @@ impl Timestamp {
/// If uniqueness and monotonicity is required, the user is responsible for
/// ensuring that the time value always increases between calls (including
/// between restarts of the process and device).
pub fn from_unix(
context: impl ClockSequence,
seconds: u64,
subsec_nanos: u32,
) -> Self {
pub fn from_unix(context: impl ClockSequence, seconds: u64, subsec_nanos: u32) -> Self {
let counter = context.generate_sequence(seconds, subsec_nanos);
let ticks = UUID_TICKS_BETWEEN_EPOCHS
+ seconds * 10_000_000
+ u64::from(subsec_nanos) / 100;
let ticks =
UUID_TICKS_BETWEEN_EPOCHS + seconds * 10_000_000 + u64::from(subsec_nanos) / 100;
Timestamp { ticks, counter }
}
@ -104,8 +99,7 @@ impl Timestamp {
pub const fn to_unix(&self) -> (u64, u32) {
(
(self.ticks - UUID_TICKS_BETWEEN_EPOCHS) / 10_000_000,
((self.ticks - UUID_TICKS_BETWEEN_EPOCHS) % 10_000_000) as u32
* 100,
((self.ticks - UUID_TICKS_BETWEEN_EPOCHS) % 10_000_000) as u32 * 100,
)
}
@ -202,8 +196,7 @@ impl Uuid {
pub const fn new_v1(ts: Timestamp, node_id: &[u8; 6]) -> Self {
let time_low = (ts.ticks & 0xFFFF_FFFF) as u32;
let time_mid = ((ts.ticks >> 32) & 0xFFFF) as u16;
let time_high_and_version =
(((ts.ticks >> 48) & 0x0FFF) as u16) | (1 << 12);
let time_high_and_version = (((ts.ticks >> 48) & 0x0FFF) as u16) | (1 << 12);
let mut d4 = [0; 8];
@ -244,8 +237,8 @@ impl Uuid {
| ((self.as_bytes()[2]) as u64) << 8
| (self.as_bytes()[3] as u64);
let counter: u16 = ((self.as_bytes()[8] & 0x3F) as u16) << 8
| (self.as_bytes()[9] as u16);
let counter: u16 =
((self.as_bytes()[8] & 0x3F) as u16) << 8 | (self.as_bytes()[9] as u16);
Some(Timestamp::from_rfc4122(ticks, counter))
}
@ -316,10 +309,7 @@ mod tests {
let node = [1, 2, 3, 4, 5, 6];
let context = Context::new(0);
let uuid = Uuid::new_v1(
Timestamp::from_unix(&context, time, time_fraction),
&node,
);
let uuid = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
assert_eq!(uuid.get_version(), Some(Version::Mac));
assert_eq!(uuid.get_variant(), Variant::RFC4122);
@ -333,8 +323,7 @@ mod tests {
assert_eq!(ts.0 - 0x01B2_1DD2_1381_4000, 14_968_545_358_129_460);
// Ensure parsing the same UUID produces the same timestamp
let parsed =
Uuid::parse_str("20616934-4ba2-11e7-8000-010203040506").unwrap();
let parsed = Uuid::parse_str("20616934-4ba2-11e7-8000-010203040506").unwrap();
assert_eq!(
uuid.get_timestamp().unwrap(),
@ -352,31 +341,19 @@ mod tests {
// This context will wrap
let context = Context::new((u16::MAX >> 2) - 1);
let uuid1 = Uuid::new_v1(
Timestamp::from_unix(&context, time, time_fraction),
&node,
);
let uuid1 = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
let time: u64 = 1_496_854_536;
let uuid2 = Uuid::new_v1(
Timestamp::from_unix(&context, time, time_fraction),
&node,
);
let uuid2 = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
assert_eq!(uuid1.get_timestamp().unwrap().to_rfc4122().1, 16382);
assert_eq!(uuid2.get_timestamp().unwrap().to_rfc4122().1, 0);
let time = 1_496_854_535;
let uuid3 = Uuid::new_v1(
Timestamp::from_unix(&context, time, time_fraction),
&node,
);
let uuid4 = Uuid::new_v1(
Timestamp::from_unix(&context, time, time_fraction),
&node,
);
let uuid3 = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
let uuid4 = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
assert_eq!(uuid3.get_timestamp().unwrap().to_rfc4122().1, 1);
assert_eq!(uuid4.get_timestamp().unwrap().to_rfc4122().1, 2);

View File

@ -41,10 +41,7 @@ mod tests {
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
use crate::{
Variant, Version,
std::string::ToString,
};
use crate::{std::string::ToString, Variant, Version};
static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
(

View File

@ -40,10 +40,7 @@ mod tests {
#[cfg(target_arch = "wasm32")]
use wasm_bindgen_test::*;
use crate::{
Variant, Version,
std::string::ToString,
};
use crate::{std::string::ToString, Variant, Version};
static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
(
@ -131,8 +128,7 @@ mod tests {
#[test]
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
fn test_get_version() {
let uuid =
Uuid::new_v5(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
let uuid = Uuid::new_v5(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
assert_eq!(uuid.get_version(), Some(Version::Sha1));
assert_eq!(uuid.get_version_num(), 5);