pub struct Uuid(/* private fields */);net only.Expand description
A UUID.
This type provides type-safe UUIDs with RFC 4122 validation.
It uses the newtype pattern with #[repr(transparent)] for zero-cost abstraction.
§Invariants
- Exactly 16 bytes (128 bits)
§Examples
use bare_types::net::Uuid;
// Create a UUID from bytes
let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
// Access the string representation
assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
// Check if it's nil
assert!(!uuid.is_nil());
// Get the bytes
assert_eq!(uuid.bytes(), [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
// Parse from string
let uuid: Uuid = "00112233-4455-6677-8899-aabbccddeeff".parse()?;Implementations§
Source§impl Uuid
impl Uuid
Sourcepub const fn new(bytes: [u8; 16]) -> Self
pub const fn new(bytes: [u8; 16]) -> Self
Creates a new UUID from an array of 16 bytes.
§Examples
use bare_types::net::Uuid;
let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, UuidError>
pub fn from_bytes(bytes: &[u8]) -> Result<Self, UuidError>
Creates a new UUID from a slice of bytes.
§Errors
Returns UuidError if the slice is not exactly 16 bytes.
§Examples
use bare_types::net::Uuid;
let bytes = vec![0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff];
let uuid = Uuid::from_bytes(&bytes)?;
assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");Sourcepub fn from_str(s: &str) -> Result<Self, UuidError>
pub fn from_str(s: &str) -> Result<Self, UuidError>
Creates a new UUID from a string.
§Errors
Returns UuidError if string is not in a valid format.
§Panics
Panics if a hex character is invalid (this should never happen due to prior validation).
§Examples
use bare_types::net::Uuid;
let uuid = Uuid::from_str("00112233-4455-6677-8899-aabbccddeeff")?;
assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");Sourcepub const fn nil() -> Self
pub const fn nil() -> Self
Returns the nil UUID.
The nil UUID is 00000000-0000-0000-0000-000000000000.
§Examples
use bare_types::net::Uuid;
let uuid = Uuid::nil();
assert!(uuid.is_nil());
assert_eq!(uuid.as_str(), "00000000-0000-0000-0000-000000000000");Sourcepub fn as_str(&self) -> String
pub fn as_str(&self) -> String
Returns the UUID as a string in standard format.
§Examples
use bare_types::net::Uuid;
let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");Sourcepub fn as_str_upper(&self) -> String
pub fn as_str_upper(&self) -> String
Returns the UUID as a string in uppercase format.
§Examples
use bare_types::net::Uuid;
let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
assert_eq!(uuid.as_str_upper(), "00112233-4455-6677-8899-AABBCCDDEEFF");Sourcepub const fn as_inner(&self) -> &[u8; 16]
pub const fn as_inner(&self) -> &[u8; 16]
Returns a reference to the underlying array of 16 bytes.
§Examples
use bare_types::net::Uuid;
let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
let bytes: &[u8; 16] = uuid.as_inner();
assert_eq!(bytes, &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);Sourcepub const fn into_inner(self) -> [u8; 16]
pub const fn into_inner(self) -> [u8; 16]
Consumes this UUID and returns the underlying array of 16 bytes.
§Examples
use bare_types::net::Uuid;
let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
let bytes = uuid.into_inner();
assert_eq!(bytes, [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);Sourcepub const fn bytes(&self) -> [u8; 16]
pub const fn bytes(&self) -> [u8; 16]
Returns the bytes of the UUID.
§Examples
use bare_types::net::Uuid;
let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
assert_eq!(uuid.bytes(), [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);Sourcepub fn is_nil(&self) -> bool
pub fn is_nil(&self) -> bool
Returns true if this is a nil UUID.
The nil UUID is 00000000-0000-0000-0000-000000000000.
§Examples
use bare_types::net::Uuid;
assert!(Uuid::nil().is_nil());Sourcepub fn version(&self) -> Option<u8>
pub fn version(&self) -> Option<u8>
Returns the version of the UUID.
Returns None if the UUID is not a valid RFC 4122 UUID.
§Examples
use bare_types::net::Uuid;
let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
assert_eq!(uuid.version(), None);Sourcepub const fn variant(&self) -> Option<UuidVariant>
pub const fn variant(&self) -> Option<UuidVariant>
Returns the variant of the UUID.
Returns None if the UUID is not a valid RFC 4122 UUID.
§Examples
use bare_types::net::{Uuid, UuidVariant};
let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
assert_eq!(uuid.variant(), Some(UuidVariant::Rfc4122));Trait Implementations§
Source§impl<'a> Arbitrary<'a> for Uuid
Available on crate feature arbitrary only.
impl<'a> Arbitrary<'a> for Uuid
arbitrary only.Source§fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
fn arbitrary(u: &mut Unstructured<'a>) -> Result<Self>
Self from the given unstructured data. Read moreSource§fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
fn arbitrary_take_rest(u: Unstructured<'a>) -> Result<Self, Error>
Self from the entirety of the given
unstructured data. Read moreSource§fn size_hint(depth: usize) -> (usize, Option<usize>)
fn size_hint(depth: usize) -> (usize, Option<usize>)
Unstructured this type
needs to construct itself. Read moreSource§fn try_size_hint(
depth: usize,
) -> Result<(usize, Option<usize>), MaxRecursionReached>
fn try_size_hint( depth: usize, ) -> Result<(usize, Option<usize>), MaxRecursionReached>
Unstructured this type
needs to construct itself. Read more