pub struct UUIDLike(/* private fields */);Expand description
A wrapper for 128-bit values that may or may not be valid TNIDs.
This type provides a way to work with 128-bit UUID-like values without the strict
validation that Tnid requires. Unlike Tnid, which
only accepts values that conform to the TNID specification (correct UUIDv8 version/variant
bits and valid name encoding), UUIDLike accepts any 128-bit value.
This makes UUIDLike useful for:
- Inspecting potentially invalid TNIDs to understand why they don’t parse
- Converting between different UUID representations (u128, hex strings) without validation
- Working with UUIDs from external systems that may not be TNIDs
- Debugging and troubleshooting TNID-related issues
§Examples
Basic usage:
use tnid::UUIDLike;
// Create from any 128-bit value
let uuid_like = UUIDLike::new(0x12345678_1234_1234_1234_123456789abc);
// Convert to different representations
let as_u128 = uuid_like.as_u128();
let as_string = uuid_like.to_uuid_string_cased(false);Inspecting potentially invalid TNIDs:
use tnid::{UUIDLike, Tnid, TnidName, NameStr};
struct User;
impl TnidName for User {
const ID_NAME: NameStr<'static> = NameStr::new_const("user");
}
// Parse a UUID string that might not be a valid TNID
let uuid_str = "cab1952a-f09d-86d9-928e-96ea03dc6af3";
let uuid_like = UUIDLike::parse_uuid_string(uuid_str).unwrap();
// Try to convert to TNID - this performs validation
match Tnid::<User>::from_u128(uuid_like.as_u128()) {
Some(tnid) => println!("Valid TNID: {}", tnid),
None => println!("Not a valid TNID (wrong version/variant/name)"),
}Implementations§
Source§impl UUIDLike
impl UUIDLike
Sourcepub fn as_u128(&self) -> u128
pub fn as_u128(&self) -> u128
Returns the raw 128-bit value.
§Examples
use tnid::UUIDLike;
let uuid_like = UUIDLike::new(0x12345678_1234_1234_1234_123456789abc);
assert_eq!(uuid_like.as_u128(), 0x12345678_1234_1234_1234_123456789abc);Sourcepub fn new(id: u128) -> Self
pub fn new(id: u128) -> Self
Creates a new UUIDLike from a 128-bit value.
Accepts any u128 value without validation.
§Examples
use tnid::UUIDLike;
let uuid_like = UUIDLike::new(0x12345678_1234_1234_1234_123456789abc);
assert_eq!(uuid_like.as_u128(), 0x12345678_1234_1234_1234_123456789abc);Sourcepub fn to_uuid_string_cased(&self, uppercase: bool) -> String
pub fn to_uuid_string_cased(&self, uppercase: bool) -> String
Converts to UUID hex string format with specified case.
Produces the standard UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
§Parameters
uppercase: Iftrue, uses uppercase hex digits (A-F). Iffalse, uses lowercase (a-f).
§Examples
use tnid::UUIDLike;
let uuid_like = UUIDLike::new(0xCAB1952A_F09D_86D9_928E_96EA03DC6AF3);
let lowercase = uuid_like.to_uuid_string_cased(false);
assert_eq!(lowercase, "cab1952a-f09d-86d9-928e-96ea03dc6af3");
let uppercase = uuid_like.to_uuid_string_cased(true);
assert_eq!(uppercase, "CAB1952A-F09D-86D9-928E-96EA03DC6AF3");Sourcepub fn parse_uuid_string(uuid_string: &str) -> Option<Self>
pub fn parse_uuid_string(uuid_string: &str) -> Option<Self>
Parses a UUID hex string into a UUIDLike.
Accepts the standard UUID format: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
Accepts both uppercase and lowercase hex digits. Validates format but not TNID-specific requirements.
Returns None if the string is not a valid UUID hex string.
§Examples
use tnid::UUIDLike;
// Parse lowercase
let uuid = UUIDLike::parse_uuid_string("cab1952a-f09d-86d9-928e-96ea03dc6af3");
assert!(uuid.is_some());
// Parse uppercase
let uuid = UUIDLike::parse_uuid_string("CAB1952A-F09D-86D9-928E-96EA03DC6AF3");
assert!(uuid.is_some());
// Parse mixed case
let uuid = UUIDLike::parse_uuid_string("CaB1952a-F09D-86d9-928E-96ea03dc6af3");
assert!(uuid.is_some());
// Invalid format
assert!(UUIDLike::parse_uuid_string("not-a-uuid").is_none());