pub struct Uuid(/* private fields */);
Expand description
A simple UUID (v4) implementation.
Rather than using the uuid
crate, tycho has it’s own implementation of uuids
for better handling between serde and elements.
Implementations§
Source§impl Uuid
impl Uuid
Sourcepub fn hex(&self) -> String
pub fn hex(&self) -> String
Get the hex representation of the uuid (no hyphens)
use tycho::Uuid;
let uuid = Uuid::from_numeric(62911151285467627956781226334379231326);
assert_eq!(uuid.hex(), "2f543f3c06594e9233b0c8a85c2ac85e");
Sourcepub fn string(&self) -> String
pub fn string(&self) -> String
Get the formated hex representation of the uuid (with hyphens)
use tycho::Uuid;
let uuid = Uuid::from_numeric(62911151285467627956781226334379231326);
assert_eq!(uuid.string(), "2f543f3c-0659-4e92-33b0-c8a85c2ac85e");
Sourcepub fn numeric(&self) -> u128
pub fn numeric(&self) -> u128
Get the numerical representation of the uuid as a unsigned 128-bit number.
use tycho::Uuid;
let uuid = Uuid::from_string("2f543f3c-0659-4e92-33b0-c8a85c2ac85e").unwrap();
assert_eq!(uuid.numeric(), 62911151285467627956781226334379231326);
Sourcepub fn bytes(&self) -> Vec<u8> ⓘ
pub fn bytes(&self) -> Vec<u8> ⓘ
Get the bytes representation of a uuid.
use tycho::Uuid;
let uuid = Uuid::from_string("2f543f3c-0659-4e92-33b0-c8a85c2ac85e").unwrap();
assert_eq!(uuid.bytes(), vec![47, 84, 63, 60, 6, 89, 78, 146, 51, 176, 200, 168, 92, 42, 200, 94]);
Sourcepub fn slice(&self) -> [u8; 16]
pub fn slice(&self) -> [u8; 16]
Get the slice of the bytes representation of a uuid.
use tycho::Uuid;
let uuid = Uuid::from_string("2f543f3c-0659-4e92-33b0-c8a85c2ac85e").unwrap();
assert_eq!(uuid.slice(), [47, 84, 63, 60, 6, 89, 78, 146, 51, 176, 200, 168, 92, 42, 200, 94]);
Sourcepub fn is_nil(&self) -> bool
pub fn is_nil(&self) -> bool
Check if the uuid is nil, a value of 0.
use tycho::Uuid;
let uuid = Uuid::nil();
assert_eq!(uuid.is_nil(), true);
Sourcepub fn version(&self) -> u8
pub fn version(&self) -> u8
Get the version number of a uuid as an u8.
use tycho::Uuid;
let uuid = Uuid::from_numeric(62911151285467627956781226334379231326);
assert_eq!(uuid.version(), 4);
Sourcepub fn from_numeric(x: u128) -> Self
pub fn from_numeric(x: u128) -> Self
Create an uuid from a numerical value.
use tycho::Uuid;
let uuid = Uuid::from_numeric(62911151285467627956781226334379231326);
assert_eq!(uuid.hex(), "2f543f3c06594e9233b0c8a85c2ac85e");
Sourcepub fn from_hex(x: &str) -> Option<Self>
pub fn from_hex(x: &str) -> Option<Self>
Create a uuid from an unformatted 32 length hex string.
Returns none on failure.
use tycho::Uuid;
let uuid = Uuid::from_hex("2f543f3c06594e9233b0c8a85c2ac85e").unwrap();
assert_eq!(uuid.hex(), "2f543f3c06594e9233b0c8a85c2ac85e");
Sourcepub fn from_string(x: &str) -> Option<Self>
pub fn from_string(x: &str) -> Option<Self>
Create a uuid from an formatted 32 length hex string.
Returns none on failure.
use tycho::Uuid;
let uuid = Uuid::from_string("2f543f3c-0659-4e92-33b0-c8a85c2ac85e").unwrap();
assert_eq!(uuid.hex(), "2f543f3c06594e9233b0c8a85c2ac85e");
Sourcepub fn from_string_lossy(x: &str) -> Self
pub fn from_string_lossy(x: &str) -> Self
Try to create a uuid from any string.
This function strips all non-hex characters and trims length of string.
If it fails, it returns a nil uuid.
use tycho::Uuid;
let uuid = Uuid::from_string_lossy("2<f54>??z3f3c-0659-[4]e92_33b0-c8a85opolc2ac85e@bbc.co.uk");
assert_eq!(uuid.hex(), "2f543f3c06594e9233b0c8a85c2ac85e");
Sourcepub fn from_slice(x: [u8; 16]) -> Self
pub fn from_slice(x: [u8; 16]) -> Self
Create a uuid from a slice of length 16.
use tycho::Uuid;
let uuid = Uuid::from_slice([47, 84, 63, 60, 6, 89, 78, 146, 51, 176, 200, 168, 92, 42, 200, 94]);
assert_eq!(uuid.string(), "2f543f3c-0659-4e92-33b0-c8a85c2ac85e");
Sourcepub fn from_bytes(x: &[u8]) -> Self
pub fn from_bytes(x: &[u8]) -> Self
Create a uuid from any size vec of u8.
use tycho::Uuid;
let uuid = Uuid::from_bytes(&vec![47, 84, 63, 60, 6, 89, 78, 146, 51, 176, 200, 168, 92, 42, 200, 94]);
assert_eq!(uuid.string(), "2f543f3c-0659-4e92-33b0-c8a85c2ac85e");