mirror of
https://github.com/uuid-rs/uuid.git
synced 2025-10-02 23:34:50 +00:00
reverting to origin
This commit is contained in:
parent
7188b00a9e
commit
824315deb7
@ -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
|
|
@ -12,7 +12,7 @@ use proc_macro::TokenStream;
|
|||||||
use proc_macro2::TokenStream as TokenStream2;
|
use proc_macro2::TokenStream as TokenStream2;
|
||||||
use quote::{quote, quote_spanned};
|
use quote::{quote, quote_spanned};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use syn::{LitStr, spanned::Spanned};
|
use syn::{spanned::Spanned, LitStr};
|
||||||
|
|
||||||
mod error;
|
mod error;
|
||||||
mod parser;
|
mod parser;
|
||||||
@ -22,32 +22,34 @@ mod parser;
|
|||||||
pub fn parse_lit(input: TokenStream) -> TokenStream {
|
pub fn parse_lit(input: TokenStream) -> TokenStream {
|
||||||
build_uuid(input.clone()).unwrap_or_else(|e| {
|
build_uuid(input.clone()).unwrap_or_else(|e| {
|
||||||
let msg = e.to_string();
|
let msg = e.to_string();
|
||||||
let span =
|
let span = match e {
|
||||||
match e {
|
Error::UuidParse(
|
||||||
Error::UuidParse(lit, error::Error(error::ErrorKind::Char {
|
lit,
|
||||||
character,
|
error::Error(error::ErrorKind::Char { character, index }),
|
||||||
index,
|
) => {
|
||||||
})) => {
|
let mut bytes = character as u32;
|
||||||
let mut bytes = character as u32;
|
let mut width = 0;
|
||||||
let mut width = 0;
|
while bytes != 0 {
|
||||||
while bytes != 0 {
|
bytes >>= 4;
|
||||||
bytes >>= 4;
|
width += 1;
|
||||||
width += 1;
|
|
||||||
}
|
|
||||||
let mut s = proc_macro2::Literal::string("");
|
|
||||||
s.set_span(lit.span());
|
|
||||||
s.subspan(index..index + width - 1)
|
|
||||||
}
|
}
|
||||||
Error::UuidParse(lit, error::Error(
|
let mut s = proc_macro2::Literal::string("");
|
||||||
error::ErrorKind::GroupLength { index, len, .. },
|
s.set_span(lit.span());
|
||||||
)) => {
|
s.subspan(index..index + width - 1)
|
||||||
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());
|
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=>
|
TokenStream::from(quote_spanned! {span=>
|
||||||
compile_error!(#msg)
|
compile_error!(#msg)
|
||||||
|
4
src/external/arbitrary_support.rs
vendored
4
src/external/arbitrary_support.rs
vendored
@ -21,9 +21,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_arbitrary() {
|
fn test_arbitrary() {
|
||||||
let mut bytes = Unstructured::new(&[
|
let mut bytes = Unstructured::new(&[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]);
|
||||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
|
||||||
]);
|
|
||||||
|
|
||||||
let uuid = Uuid::arbitrary(&mut bytes).unwrap();
|
let uuid = Uuid::arbitrary(&mut bytes).unwrap();
|
||||||
|
|
||||||
|
68
src/external/serde_support.rs
vendored
68
src/external/serde_support.rs
vendored
@ -21,14 +21,9 @@ use serde::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
impl Serialize for Uuid {
|
impl Serialize for Uuid {
|
||||||
fn serialize<S: Serializer>(
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
&self,
|
|
||||||
serializer: S,
|
|
||||||
) -> Result<S::Ok, S::Error> {
|
|
||||||
if serializer.is_human_readable() {
|
if serializer.is_human_readable() {
|
||||||
serializer.serialize_str(
|
serializer.serialize_str(self.hyphenated().encode_lower(&mut Uuid::encode_buffer()))
|
||||||
self.hyphenated().encode_lower(&mut Uuid::encode_buffer()),
|
|
||||||
)
|
|
||||||
} else {
|
} else {
|
||||||
serializer.serialize_bytes(self.as_bytes())
|
serializer.serialize_bytes(self.as_bytes())
|
||||||
}
|
}
|
||||||
@ -36,45 +31,31 @@ impl Serialize for Uuid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for Hyphenated {
|
impl Serialize for Hyphenated {
|
||||||
fn serialize<S: Serializer>(
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
&self,
|
|
||||||
serializer: S,
|
|
||||||
) -> Result<S::Ok, S::Error> {
|
|
||||||
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
|
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for Simple {
|
impl Serialize for Simple {
|
||||||
fn serialize<S: Serializer>(
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
&self,
|
|
||||||
serializer: S,
|
|
||||||
) -> Result<S::Ok, S::Error> {
|
|
||||||
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
|
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for Urn {
|
impl Serialize for Urn {
|
||||||
fn serialize<S: Serializer>(
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
&self,
|
|
||||||
serializer: S,
|
|
||||||
) -> Result<S::Ok, S::Error> {
|
|
||||||
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
|
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Serialize for Braced {
|
impl Serialize for Braced {
|
||||||
fn serialize<S: Serializer>(
|
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
|
||||||
&self,
|
|
||||||
serializer: S,
|
|
||||||
) -> Result<S::Ok, S::Error> {
|
|
||||||
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
|
serializer.serialize_str(self.encode_lower(&mut Uuid::encode_buffer()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'de> Deserialize<'de> for Uuid {
|
impl<'de> Deserialize<'de> for Uuid {
|
||||||
fn deserialize<D: Deserializer<'de>>(
|
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
|
||||||
deserializer: D,
|
|
||||||
) -> Result<Self, D::Error> {
|
|
||||||
fn de_error<E: de::Error>(e: Error) -> E {
|
fn de_error<E: de::Error>(e: Error) -> E {
|
||||||
E::custom(format_args!("UUID parsing failed: {}", 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 {
|
impl<'vi> de::Visitor<'vi> for UuidVisitor {
|
||||||
type Value = Uuid;
|
type Value = Uuid;
|
||||||
|
|
||||||
fn expecting(
|
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
&self,
|
|
||||||
formatter: &mut fmt::Formatter<'_>,
|
|
||||||
) -> fmt::Result {
|
|
||||||
write!(formatter, "a UUID string")
|
write!(formatter, "a UUID string")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_str<E: de::Error>(
|
fn visit_str<E: de::Error>(self, value: &str) -> Result<Uuid, E> {
|
||||||
self,
|
|
||||||
value: &str,
|
|
||||||
) -> Result<Uuid, E> {
|
|
||||||
value.parse::<Uuid>().map_err(de_error)
|
value.parse::<Uuid>().map_err(de_error)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_bytes<E: de::Error>(
|
fn visit_bytes<E: de::Error>(self, value: &[u8]) -> Result<Uuid, E> {
|
||||||
self,
|
|
||||||
value: &[u8],
|
|
||||||
) -> Result<Uuid, E> {
|
|
||||||
Uuid::from_slice(value).map_err(de_error)
|
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 {
|
impl<'vi> de::Visitor<'vi> for UuidBytesVisitor {
|
||||||
type Value = Uuid;
|
type Value = Uuid;
|
||||||
|
|
||||||
fn expecting(
|
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
&self,
|
|
||||||
formatter: &mut fmt::Formatter<'_>,
|
|
||||||
) -> fmt::Result {
|
|
||||||
write!(formatter, "bytes")
|
write!(formatter, "bytes")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn visit_bytes<E: de::Error>(
|
fn visit_bytes<E: de::Error>(self, value: &[u8]) -> Result<Uuid, E> {
|
||||||
self,
|
|
||||||
value: &[u8],
|
|
||||||
) -> Result<Uuid, E> {
|
|
||||||
Uuid::from_slice(value).map_err(de_error)
|
Uuid::from_slice(value).map_err(de_error)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -169,10 +135,7 @@ pub mod compact {
|
|||||||
/// Serialize from a [`Uuid`] as a `[u8; 16]`
|
/// Serialize from a [`Uuid`] as a `[u8; 16]`
|
||||||
///
|
///
|
||||||
/// [`Uuid`]: ../../struct.Uuid.html
|
/// [`Uuid`]: ../../struct.Uuid.html
|
||||||
pub fn serialize<S>(
|
pub fn serialize<S>(u: &crate::Uuid, serializer: S) -> Result<S::Ok, S::Error>
|
||||||
u: &crate::Uuid,
|
|
||||||
serializer: S,
|
|
||||||
) -> Result<S::Ok, S::Error>
|
|
||||||
where
|
where
|
||||||
S: serde::Serializer,
|
S: serde::Serializer,
|
||||||
{
|
{
|
||||||
@ -292,10 +255,7 @@ mod serde_tests {
|
|||||||
let uuid_bytes = b"F9168C5E-CEB2-4F";
|
let uuid_bytes = b"F9168C5E-CEB2-4F";
|
||||||
let u = Uuid::from_slice(uuid_bytes).unwrap();
|
let u = Uuid::from_slice(uuid_bytes).unwrap();
|
||||||
|
|
||||||
serde_test::assert_de_tokens(
|
serde_test::assert_de_tokens(&u.readable(), &[serde_test::Token::Bytes(uuid_bytes)]);
|
||||||
&u.readable(),
|
|
||||||
&[serde_test::Token::Bytes(uuid_bytes)],
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
30
src/fmt.rs
30
src/fmt.rs
@ -140,12 +140,10 @@ impl Uuid {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const UPPER: [u8; 16] = [
|
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'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'C', b'D', b'E', b'F',
|
|
||||||
];
|
];
|
||||||
const LOWER: [u8; 16] = [
|
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'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'c', b'd', b'e', b'f',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
@ -190,11 +188,7 @@ const fn format_hyphenated(src: &[u8; 16], upper: bool) -> [u8; 36] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn encode_simple<'b>(
|
fn encode_simple<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
|
||||||
src: &[u8; 16],
|
|
||||||
buffer: &'b mut [u8],
|
|
||||||
upper: bool,
|
|
||||||
) -> &'b mut str {
|
|
||||||
let buf = &mut buffer[..Simple::LENGTH];
|
let buf = &mut buffer[..Simple::LENGTH];
|
||||||
let dst = buf.as_mut_ptr();
|
let dst = buf.as_mut_ptr();
|
||||||
|
|
||||||
@ -207,11 +201,7 @@ fn encode_simple<'b>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn encode_hyphenated<'b>(
|
fn encode_hyphenated<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
|
||||||
src: &[u8; 16],
|
|
||||||
buffer: &'b mut [u8],
|
|
||||||
upper: bool,
|
|
||||||
) -> &'b mut str {
|
|
||||||
let buf = &mut buffer[..Hyphenated::LENGTH];
|
let buf = &mut buffer[..Hyphenated::LENGTH];
|
||||||
let dst = buf.as_mut_ptr();
|
let dst = buf.as_mut_ptr();
|
||||||
|
|
||||||
@ -224,11 +214,7 @@ fn encode_hyphenated<'b>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn encode_braced<'b>(
|
fn encode_braced<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
|
||||||
src: &[u8; 16],
|
|
||||||
buffer: &'b mut [u8],
|
|
||||||
upper: bool,
|
|
||||||
) -> &'b mut str {
|
|
||||||
let buf = &mut buffer[..Braced::LENGTH];
|
let buf = &mut buffer[..Braced::LENGTH];
|
||||||
buf[0] = b'{';
|
buf[0] = b'{';
|
||||||
buf[Braced::LENGTH - 1] = b'}';
|
buf[Braced::LENGTH - 1] = b'}';
|
||||||
@ -244,11 +230,7 @@ fn encode_braced<'b>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn encode_urn<'b>(
|
fn encode_urn<'b>(src: &[u8; 16], buffer: &'b mut [u8], upper: bool) -> &'b mut str {
|
||||||
src: &[u8; 16],
|
|
||||||
buffer: &'b mut [u8],
|
|
||||||
upper: bool,
|
|
||||||
) -> &'b mut str {
|
|
||||||
let buf = &mut buffer[..Urn::LENGTH];
|
let buf = &mut buffer[..Urn::LENGTH];
|
||||||
buf[..9].copy_from_slice(b"urn:uuid:");
|
buf[..9].copy_from_slice(b"urn:uuid:");
|
||||||
|
|
||||||
|
72
src/lib.rs
72
src/lib.rs
@ -402,26 +402,26 @@ pub struct Uuid(Bytes);
|
|||||||
impl Uuid {
|
impl Uuid {
|
||||||
/// UUID namespace for Domain Name System (DNS).
|
/// UUID namespace for Domain Name System (DNS).
|
||||||
pub const NAMESPACE_DNS: Self = Uuid([
|
pub const NAMESPACE_DNS: Self = Uuid([
|
||||||
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0,
|
0x6b, 0xa7, 0xb8, 0x10, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
|
||||||
0x4f, 0xd4, 0x30, 0xc8,
|
0xc8,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/// UUID namespace for ISO Object Identifiers (OIDs).
|
/// UUID namespace for ISO Object Identifiers (OIDs).
|
||||||
pub const NAMESPACE_OID: Self = Uuid([
|
pub const NAMESPACE_OID: Self = Uuid([
|
||||||
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0,
|
0x6b, 0xa7, 0xb8, 0x12, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
|
||||||
0x4f, 0xd4, 0x30, 0xc8,
|
0xc8,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/// UUID namespace for Uniform Resource Locators (URLs).
|
/// UUID namespace for Uniform Resource Locators (URLs).
|
||||||
pub const NAMESPACE_URL: Self = Uuid([
|
pub const NAMESPACE_URL: Self = Uuid([
|
||||||
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0,
|
0x6b, 0xa7, 0xb8, 0x11, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
|
||||||
0x4f, 0xd4, 0x30, 0xc8,
|
0xc8,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/// UUID namespace for X.500 Distinguished Names (DNs).
|
/// UUID namespace for X.500 Distinguished Names (DNs).
|
||||||
pub const NAMESPACE_X500: Self = Uuid([
|
pub const NAMESPACE_X500: Self = Uuid([
|
||||||
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0,
|
0x6b, 0xa7, 0xb8, 0x14, 0x9d, 0xad, 0x11, 0xd1, 0x80, 0xb4, 0x00, 0xc0, 0x4f, 0xd4, 0x30,
|
||||||
0x4f, 0xd4, 0x30, 0xc8,
|
0xc8,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
/// Returns the variant of the UUID structure.
|
/// 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 d3 = (self.as_bytes()[6] as u16) | (self.as_bytes()[7] as u16) << 8;
|
||||||
|
|
||||||
let d4: &[u8; 8] =
|
let d4: &[u8; 8] = convert::TryInto::try_into(&self.as_bytes()[8..16]).unwrap();
|
||||||
convert::TryInto::try_into(&self.as_bytes()[8..16]).unwrap();
|
|
||||||
(d1, d2, d3, d4)
|
(d1, d2, d3, d4)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -805,9 +804,9 @@ impl Uuid {
|
|||||||
/// ```
|
/// ```
|
||||||
pub const fn to_bytes_le(&self) -> Bytes {
|
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[3], self.0[2], self.0[1], self.0[0], self.0[5], self.0[4], self.0[7], self.0[6],
|
||||||
self.0[7], self.0[6], self.0[8], self.0[9], self.0[10], self.0[11],
|
self.0[8], self.0[9], self.0[10], self.0[11], self.0[12], self.0[13], self.0[14],
|
||||||
self.0[12], self.0[13], self.0[14], self.0[15],
|
self.0[15],
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -891,15 +890,15 @@ mod tests {
|
|||||||
|
|
||||||
pub const fn new() -> Uuid {
|
pub const fn new() -> Uuid {
|
||||||
Uuid::from_bytes([
|
Uuid::from_bytes([
|
||||||
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAA, 0xB6, 0xBF, 0x32,
|
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAA, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
|
||||||
0x9B, 0xF3, 0x9F, 0xA1, 0xE4,
|
0xA1, 0xE4,
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
pub const fn new2() -> Uuid {
|
pub const fn new2() -> Uuid {
|
||||||
Uuid::from_bytes([
|
Uuid::from_bytes([
|
||||||
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAB, 0xB6, 0xBF, 0x32,
|
0xF9, 0x16, 0x8C, 0x5E, 0xCE, 0xB2, 0x4F, 0xAB, 0xB6, 0xBF, 0x32, 0x9B, 0xF3, 0x9F,
|
||||||
0x9B, 0xF3, 0x9F, 0xA1, 0xE4,
|
0xA1, 0xE4,
|
||||||
])
|
])
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -993,9 +992,8 @@ mod tests {
|
|||||||
fn test_nil() {
|
fn test_nil() {
|
||||||
let nil = Uuid::nil();
|
let nil = Uuid::nil();
|
||||||
let not_nil = new();
|
let not_nil = new();
|
||||||
let from_bytes = Uuid::from_bytes([
|
let from_bytes =
|
||||||
4, 54, 67, 12, 43, 2, 2, 76, 32, 50, 87, 5, 1, 33, 43, 87,
|
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);
|
assert_eq!(from_bytes.get_version(), None);
|
||||||
|
|
||||||
@ -1031,8 +1029,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_get_version_v3() {
|
fn test_get_version_v3() {
|
||||||
let uuid =
|
let uuid = Uuid::new_v3(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
|
||||||
Uuid::new_v3(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
|
|
||||||
|
|
||||||
assert_eq!(uuid.get_version().unwrap(), Version::Md5);
|
assert_eq!(uuid.get_version().unwrap(), Version::Md5);
|
||||||
assert_eq!(uuid.get_version_num(), 3);
|
assert_eq!(uuid.get_version_num(), 3);
|
||||||
@ -1042,16 +1039,11 @@ mod tests {
|
|||||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_get_variant() {
|
fn test_get_variant() {
|
||||||
let uuid1 = new();
|
let uuid1 = new();
|
||||||
let uuid2 =
|
let uuid2 = Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
|
||||||
Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap();
|
let uuid3 = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
|
||||||
let uuid3 =
|
let uuid4 = Uuid::parse_str("936DA01F9ABD4d9dC0C702AF85C822A8").unwrap();
|
||||||
Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
|
let uuid5 = Uuid::parse_str("F9168C5E-CEB2-4faa-D6BF-329BF39FA1E4").unwrap();
|
||||||
let uuid4 =
|
let uuid6 = Uuid::parse_str("f81d4fae-7dec-11d0-7765-00a0c91e6bf6").unwrap();
|
||||||
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!(uuid1.get_variant(), Variant::RFC4122);
|
||||||
assert_eq!(uuid2.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)]
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_from_slice() {
|
fn test_from_slice() {
|
||||||
let b = [
|
let b = [
|
||||||
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3,
|
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
|
||||||
0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
|
0xd7, 0xd8,
|
||||||
];
|
];
|
||||||
|
|
||||||
let u = Uuid::from_slice(&b).unwrap();
|
let u = Uuid::from_slice(&b).unwrap();
|
||||||
@ -1364,8 +1356,8 @@ mod tests {
|
|||||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_from_bytes() {
|
fn test_from_bytes() {
|
||||||
let b = [
|
let b = [
|
||||||
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3,
|
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
|
||||||
0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
|
0xd7, 0xd8,
|
||||||
];
|
];
|
||||||
|
|
||||||
let u = Uuid::from_bytes(b);
|
let u = Uuid::from_bytes(b);
|
||||||
@ -1391,8 +1383,8 @@ mod tests {
|
|||||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_bytes_roundtrip() {
|
fn test_bytes_roundtrip() {
|
||||||
let b_in: crate::Bytes = [
|
let b_in: crate::Bytes = [
|
||||||
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3,
|
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
|
||||||
0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
|
0xd7, 0xd8,
|
||||||
];
|
];
|
||||||
|
|
||||||
let u = Uuid::from_slice(&b_in).unwrap();
|
let u = Uuid::from_slice(&b_in).unwrap();
|
||||||
@ -1406,8 +1398,8 @@ mod tests {
|
|||||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_bytes_le_roundtrip() {
|
fn test_bytes_le_roundtrip() {
|
||||||
let b = [
|
let b = [
|
||||||
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3,
|
0xa1, 0xa2, 0xa3, 0xa4, 0xb1, 0xb2, 0xc1, 0xc2, 0xd1, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
|
||||||
0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
|
0xd7, 0xd8,
|
||||||
];
|
];
|
||||||
|
|
||||||
let u1 = Uuid::from_bytes(b);
|
let u1 = Uuid::from_bytes(b);
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
#[cfg(feature = "v3")]
|
#[cfg(feature = "v3")]
|
||||||
pub(crate) fn hash(ns: &[u8], src: &[u8]) -> [u8; 16] {
|
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();
|
let mut hasher = Md5::new();
|
||||||
|
|
||||||
|
@ -142,10 +142,9 @@ const fn try_parse(input: &[u8]) -> Result<[u8; 16], InvalidUuid> {
|
|||||||
// - `UUID` for a regular hyphenated UUID
|
// - `UUID` for a regular hyphenated UUID
|
||||||
(36, s)
|
(36, s)
|
||||||
| (38, [b'{', s @ .., b'}'])
|
| (38, [b'{', s @ .., b'}'])
|
||||||
| (
|
| (45, [b'u', b'r', b'n', b':', b'u', b'u', b'i', b'd', b':', s @ ..]) => {
|
||||||
45,
|
parse_hyphenated(s)
|
||||||
[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
|
// Any other shaped input is immediately invalid
|
||||||
_ => Err(()),
|
_ => Err(()),
|
||||||
};
|
};
|
||||||
@ -279,15 +278,10 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_parse_uuid_v4_valid() {
|
fn test_parse_uuid_v4_valid() {
|
||||||
let from_hyphenated =
|
let from_hyphenated = Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
|
||||||
Uuid::parse_str("67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
|
let from_simple = Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c8").unwrap();
|
||||||
let from_simple =
|
let from_urn = Uuid::parse_str("urn:uuid:67e55044-10b1-426f-9247-bb680e5fe0c8").unwrap();
|
||||||
Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c8").unwrap();
|
let from_guid = Uuid::parse_str("{67e55044-10b1-426f-9247-bb680e5fe0c8}").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_simple);
|
||||||
assert_eq!(from_hyphenated, from_urn);
|
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("F9168C5E-CEB2-4faa-B6BF-329BF39FA1E4").is_ok());
|
||||||
assert!(Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c8").is_ok());
|
assert!(Uuid::parse_str("67e5504410b1426f9247bb680e5fe0c8").is_ok());
|
||||||
assert!(Uuid::parse_str("01020304-1112-2122-3132-414243444546").is_ok());
|
assert!(Uuid::parse_str("01020304-1112-2122-3132-414243444546").is_ok());
|
||||||
assert!(Uuid::parse_str(
|
assert!(Uuid::parse_str("urn:uuid:67e55044-10b1-426f-9247-bb680e5fe0c8").is_ok());
|
||||||
"urn:uuid:67e55044-10b1-426f-9247-bb680e5fe0c8"
|
assert!(Uuid::parse_str("{6d93bade-bd9f-4e13-8914-9474e1e3567b}").is_ok());
|
||||||
)
|
|
||||||
.is_ok());
|
|
||||||
assert!(
|
|
||||||
Uuid::parse_str("{6d93bade-bd9f-4e13-8914-9474e1e3567b}").is_ok()
|
|
||||||
);
|
|
||||||
|
|
||||||
// Nil
|
// Nil
|
||||||
let nil = Uuid::nil();
|
let nil = Uuid::nil();
|
||||||
@ -527,9 +516,6 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_try_parse_ascii_non_utf8() {
|
fn test_try_parse_ascii_non_utf8() {
|
||||||
assert!(Uuid::try_parse_ascii(
|
assert!(Uuid::try_parse_ascii(b"67e55044-10b1-426f-9247-bb680e5\0e0c8").is_err());
|
||||||
b"67e55044-10b1-426f-9247-bb680e5\0e0c8"
|
|
||||||
)
|
|
||||||
.is_err());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
49
src/v1.rs
49
src/v1.rs
@ -71,15 +71,10 @@ impl Timestamp {
|
|||||||
/// If uniqueness and monotonicity is required, the user is responsible for
|
/// If uniqueness and monotonicity is required, the user is responsible for
|
||||||
/// ensuring that the time value always increases between calls (including
|
/// ensuring that the time value always increases between calls (including
|
||||||
/// between restarts of the process and device).
|
/// between restarts of the process and device).
|
||||||
pub fn from_unix(
|
pub fn from_unix(context: impl ClockSequence, seconds: u64, subsec_nanos: u32) -> Self {
|
||||||
context: impl ClockSequence,
|
|
||||||
seconds: u64,
|
|
||||||
subsec_nanos: u32,
|
|
||||||
) -> Self {
|
|
||||||
let counter = context.generate_sequence(seconds, subsec_nanos);
|
let counter = context.generate_sequence(seconds, subsec_nanos);
|
||||||
let ticks = UUID_TICKS_BETWEEN_EPOCHS
|
let ticks =
|
||||||
+ seconds * 10_000_000
|
UUID_TICKS_BETWEEN_EPOCHS + seconds * 10_000_000 + u64::from(subsec_nanos) / 100;
|
||||||
+ u64::from(subsec_nanos) / 100;
|
|
||||||
|
|
||||||
Timestamp { ticks, counter }
|
Timestamp { ticks, counter }
|
||||||
}
|
}
|
||||||
@ -104,8 +99,7 @@ impl Timestamp {
|
|||||||
pub const fn to_unix(&self) -> (u64, u32) {
|
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,
|
||||||
((self.ticks - UUID_TICKS_BETWEEN_EPOCHS) % 10_000_000) as u32
|
((self.ticks - UUID_TICKS_BETWEEN_EPOCHS) % 10_000_000) as u32 * 100,
|
||||||
* 100,
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -202,8 +196,7 @@ impl Uuid {
|
|||||||
pub const fn new_v1(ts: Timestamp, node_id: &[u8; 6]) -> Self {
|
pub const fn new_v1(ts: Timestamp, node_id: &[u8; 6]) -> Self {
|
||||||
let time_low = (ts.ticks & 0xFFFF_FFFF) as u32;
|
let time_low = (ts.ticks & 0xFFFF_FFFF) as u32;
|
||||||
let time_mid = ((ts.ticks >> 32) & 0xFFFF) as u16;
|
let time_mid = ((ts.ticks >> 32) & 0xFFFF) as u16;
|
||||||
let time_high_and_version =
|
let time_high_and_version = (((ts.ticks >> 48) & 0x0FFF) as u16) | (1 << 12);
|
||||||
(((ts.ticks >> 48) & 0x0FFF) as u16) | (1 << 12);
|
|
||||||
|
|
||||||
let mut d4 = [0; 8];
|
let mut d4 = [0; 8];
|
||||||
|
|
||||||
@ -244,8 +237,8 @@ impl Uuid {
|
|||||||
| ((self.as_bytes()[2]) as u64) << 8
|
| ((self.as_bytes()[2]) as u64) << 8
|
||||||
| (self.as_bytes()[3] as u64);
|
| (self.as_bytes()[3] as u64);
|
||||||
|
|
||||||
let counter: u16 = ((self.as_bytes()[8] & 0x3F) as u16) << 8
|
let counter: u16 =
|
||||||
| (self.as_bytes()[9] as u16);
|
((self.as_bytes()[8] & 0x3F) as u16) << 8 | (self.as_bytes()[9] as u16);
|
||||||
|
|
||||||
Some(Timestamp::from_rfc4122(ticks, counter))
|
Some(Timestamp::from_rfc4122(ticks, counter))
|
||||||
}
|
}
|
||||||
@ -316,10 +309,7 @@ mod tests {
|
|||||||
let node = [1, 2, 3, 4, 5, 6];
|
let node = [1, 2, 3, 4, 5, 6];
|
||||||
let context = Context::new(0);
|
let context = Context::new(0);
|
||||||
|
|
||||||
let uuid = Uuid::new_v1(
|
let uuid = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
|
||||||
Timestamp::from_unix(&context, time, time_fraction),
|
|
||||||
&node,
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(uuid.get_version(), Some(Version::Mac));
|
assert_eq!(uuid.get_version(), Some(Version::Mac));
|
||||||
assert_eq!(uuid.get_variant(), Variant::RFC4122);
|
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);
|
assert_eq!(ts.0 - 0x01B2_1DD2_1381_4000, 14_968_545_358_129_460);
|
||||||
|
|
||||||
// Ensure parsing the same UUID produces the same timestamp
|
// Ensure parsing the same UUID produces the same timestamp
|
||||||
let parsed =
|
let parsed = Uuid::parse_str("20616934-4ba2-11e7-8000-010203040506").unwrap();
|
||||||
Uuid::parse_str("20616934-4ba2-11e7-8000-010203040506").unwrap();
|
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
uuid.get_timestamp().unwrap(),
|
uuid.get_timestamp().unwrap(),
|
||||||
@ -352,31 +341,19 @@ mod tests {
|
|||||||
// This context will wrap
|
// This context will wrap
|
||||||
let context = Context::new((u16::MAX >> 2) - 1);
|
let context = Context::new((u16::MAX >> 2) - 1);
|
||||||
|
|
||||||
let uuid1 = Uuid::new_v1(
|
let uuid1 = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
|
||||||
Timestamp::from_unix(&context, time, time_fraction),
|
|
||||||
&node,
|
|
||||||
);
|
|
||||||
|
|
||||||
let time: u64 = 1_496_854_536;
|
let time: u64 = 1_496_854_536;
|
||||||
|
|
||||||
let uuid2 = Uuid::new_v1(
|
let uuid2 = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
|
||||||
Timestamp::from_unix(&context, time, time_fraction),
|
|
||||||
&node,
|
|
||||||
);
|
|
||||||
|
|
||||||
assert_eq!(uuid1.get_timestamp().unwrap().to_rfc4122().1, 16382);
|
assert_eq!(uuid1.get_timestamp().unwrap().to_rfc4122().1, 16382);
|
||||||
assert_eq!(uuid2.get_timestamp().unwrap().to_rfc4122().1, 0);
|
assert_eq!(uuid2.get_timestamp().unwrap().to_rfc4122().1, 0);
|
||||||
|
|
||||||
let time = 1_496_854_535;
|
let time = 1_496_854_535;
|
||||||
|
|
||||||
let uuid3 = Uuid::new_v1(
|
let uuid3 = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
|
||||||
Timestamp::from_unix(&context, time, time_fraction),
|
let uuid4 = Uuid::new_v1(Timestamp::from_unix(&context, time, time_fraction), &node);
|
||||||
&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!(uuid3.get_timestamp().unwrap().to_rfc4122().1, 1);
|
||||||
assert_eq!(uuid4.get_timestamp().unwrap().to_rfc4122().1, 2);
|
assert_eq!(uuid4.get_timestamp().unwrap().to_rfc4122().1, 2);
|
||||||
|
@ -41,10 +41,7 @@ mod tests {
|
|||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
use wasm_bindgen_test::*;
|
use wasm_bindgen_test::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{std::string::ToString, Variant, Version};
|
||||||
Variant, Version,
|
|
||||||
std::string::ToString,
|
|
||||||
};
|
|
||||||
|
|
||||||
static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
|
static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
|
||||||
(
|
(
|
||||||
|
@ -40,10 +40,7 @@ mod tests {
|
|||||||
#[cfg(target_arch = "wasm32")]
|
#[cfg(target_arch = "wasm32")]
|
||||||
use wasm_bindgen_test::*;
|
use wasm_bindgen_test::*;
|
||||||
|
|
||||||
use crate::{
|
use crate::{std::string::ToString, Variant, Version};
|
||||||
Variant, Version,
|
|
||||||
std::string::ToString,
|
|
||||||
};
|
|
||||||
|
|
||||||
static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
|
static FIXTURE: &'static [(&'static Uuid, &'static str, &'static str)] = &[
|
||||||
(
|
(
|
||||||
@ -131,8 +128,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test)]
|
||||||
fn test_get_version() {
|
fn test_get_version() {
|
||||||
let uuid =
|
let uuid = Uuid::new_v5(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
|
||||||
Uuid::new_v5(&Uuid::NAMESPACE_DNS, "rust-lang.org".as_bytes());
|
|
||||||
|
|
||||||
assert_eq!(uuid.get_version(), Some(Version::Sha1));
|
assert_eq!(uuid.get_version(), Some(Version::Sha1));
|
||||||
assert_eq!(uuid.get_version_num(), 5);
|
assert_eq!(uuid.get_version_num(), 5);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user