#[repr(C)]pub struct UUID { /* private fields */ }Expand description
A uProtocol UUIDv7 message identifier (RFC 9562 layout).
Implementations§
Source§impl UUID
impl UUID
Sourcepub fn from_bytes(bytes: &[u8; 16]) -> Result<Self, UuidConversionError>
pub fn from_bytes(bytes: &[u8; 16]) -> Result<Self, UuidConversionError>
Sourcepub fn from_u64_pair(msb: u64, lsb: u64) -> Result<Self, UuidConversionError>
pub fn from_u64_pair(msb: u64, lsb: u64) -> Result<Self, UuidConversionError>
Sourcepub fn as_u64_pair(&self) -> (u64, u64)
pub fn as_u64_pair(&self) -> (u64, u64)
Gets this UUID as a pair of 64 bit integers.
§Returns
A tuple of the most significant and least significant 8 bytes,
i.e. the inverse of UUID::from_u64_pair.
Sourcepub fn build() -> UUID
pub fn build() -> UUID
Creates a new UUID that can be used for uProtocol messages.
§Panics
if the system clock is set to an instant before the UNIX Epoch.
§Examples
use std::time::SystemTime;
use up_rust::UUID;
let uuid = UUID::build();
assert!(uuid.time() <= SystemTime::now()
.duration_since(SystemTime::UNIX_EPOCH)
.expect("current system time is set to a point in time before UNIX Epoch")
.as_millis() as u64);Sourcepub fn to_hyphenated_string(&self) -> String
pub fn to_hyphenated_string(&self) -> String
Serializes this UUID to a hyphenated string as defined by RFC 4122, Section 3 using lower case characters.
§Examples
use up_rust::UUID;
// timestamp = 1, ver = 0b0111
let msb = 0x0000000000017000_u64;
// variant = 0b10, random = 0x0010101010101a1a
let lsb = 0x8010101010101a1a_u64;
let uuid = UUID::from_u64_pair(msb, lsb).unwrap();
assert_eq!(uuid.to_hyphenated_string(), "00000000-0001-7000-8010-101010101a1a");Sourcepub fn time(&self) -> u64
pub fn time(&self) -> u64
Returns the point in time that this UUID has been created at.
§Returns
The number of milliseconds since UNIX EPOCH.
§Examples
use up_rust::UUID;
// timestamp = 0x018D548EA8E0 (Monday, 29 January 2024, 9:30:52 AM GMT)
// ver = 0b0111
let msb = 0x018D548EA8E07000u64;
// variant = 0b10
let lsb = 0x8000000000000000u64;
let creation_time = UUID::from_u64_pair(msb, lsb).unwrap().time();
assert_eq!(creation_time, 0x018D548EA8E0_u64);Sourcepub fn get_time(&self) -> u64
👎Deprecated since 0.11.0: renamed to time
pub fn get_time(&self) -> u64
timeDeprecated alias for Self::time.
Trait Implementations§
Source§impl From<&UUID> for Vec<u8>
impl From<&UUID> for Vec<u8>
Source§fn from(value: &UUID) -> Self
fn from(value: &UUID) -> Self
Serializes this UUID to a byte vector.
§Returns
a byte vector containing the 16 bytes of this UUID in big-endian order.
§Examples
use up_rust::UUID;
// timestamp = 1, ver = 0b0111
let msb = 0x0000000000017000_u64;
// variant = 0b10, random = 0x0010101010101a1a
let lsb = 0x8010101010101a1a_u64;
let uuid = UUID::from_u64_pair(msb, lsb).unwrap();
let bytes: Vec<u8> = uuid.into();
assert_eq!(bytes, vec![
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x70, 0x00,
0x80, 0x10, 0x10, 0x10, 0x10, 0x10, 0x1a, 0x1a,
]);Source§impl FromStr for UUID
impl FromStr for UUID
Source§fn from_str(uuid_str: &str) -> Result<Self, Self::Err>
fn from_str(uuid_str: &str) -> Result<Self, Self::Err>
Parses a string into a UUID.
§Returns
a uProtocol UUID based on the bytes encoded in the string.
§Errors
Returns an error
- if the given string does not represent a UUID as defined by RFC 4122, Section 3, or
- if the bytes encoded in the string contain an invalid version and/or variant identifier.
§Examples
use up_rust::UUID;
// parsing a valid uProtocol UUID succeeds
let parsing_attempt = "00000000-0001-7000-8010-101010101a1A".parse::<UUID>();
assert!(parsing_attempt.is_ok());
// parsing an invalid UUID fails
assert!("a1a2a3a4-b1b2-c1c2-d1d2-d3d4d5d6d7d8"
.parse::<UUID>()
.is_err());
// parsing a string that is not a UUID fails
assert!("this-is-not-a-UUID"
.parse::<UUID>()
.is_err());Source§impl ProtobufMappable for UUID
impl ProtobufMappable for UUID
Source§fn parse_from_protobuf_bytes(proto: &[u8]) -> Result<Self, SerializationError>
fn parse_from_protobuf_bytes(proto: &[u8]) -> Result<Self, SerializationError>
Source§fn parse_from_packed_protobuf_bytes(
proto: &[u8],
) -> Result<Self, SerializationError>
fn parse_from_packed_protobuf_bytes( proto: &[u8], ) -> Result<Self, SerializationError>
Source§fn write_to_protobuf_bytes(&self) -> Result<Vec<u8>, SerializationError>
fn write_to_protobuf_bytes(&self) -> Result<Vec<u8>, SerializationError>
Source§fn write_to_packed_protobuf_bytes(&self) -> Result<Vec<u8>, SerializationError>
fn write_to_packed_protobuf_bytes(&self) -> Result<Vec<u8>, SerializationError>
Source§impl TryFrom<Vec<u8>> for UUID
impl TryFrom<Vec<u8>> for UUID
Source§fn try_from(value: Vec<u8>) -> Result<Self, Self::Error>
fn try_from(value: Vec<u8>) -> Result<Self, Self::Error>
Creates a UUID from a byte vector.
§Arguments
value - the byte vector
§Returns
a uProtocol UUID with the given timestamp and random values.
§Errors
Returns an error
- if the given vector does not have a length of 16 bytes, or
- if the given bytes contain an invalid version and/or variant identifier.
§Examples
use up_rust::UUID;
// timestamp = 1, ver = 0b0111, variant = 0b10
let bytes: Vec<u8> = vec![
0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x70, 0x00,
0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
];
let conversion_attempt = UUID::try_from(bytes);
assert!(conversion_attempt.is_ok());
let uuid = conversion_attempt.unwrap();
assert_eq!(uuid.time(), 0x1_u64);