feat: Add ability to construct a SegmentId from raw bytes (#24)

This allows a `SegmentId` to be constructed from a `[u8; 16]` byte array.  

It also adds a `impl Default for SegementId`, which defaults to all nulls
This commit is contained in:
Eric Ridge
2025-01-23 08:42:50 -05:00
committed by Stu Hood
parent 16da31cf06
commit 1b88bb61f9

View File

@@ -21,6 +21,14 @@ use uuid::Uuid;
#[derive(Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SegmentId(Uuid);
impl Default for SegmentId {
fn default() -> Self {
Self(Uuid::from_bytes(uuid::Bytes::from([
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
])))
}
}
#[cfg(test)]
static AUTO_INC_COUNTER: Lazy<atomic::AtomicUsize> = Lazy::new(atomic::AtomicUsize::default);
@@ -84,6 +92,10 @@ impl SegmentId {
pub fn from_uuid_string(uuid_string: &str) -> Result<SegmentId, SegmentIdParseError> {
FromStr::from_str(uuid_string)
}
pub fn from_bytes(uuid_bytes: [u8; 16]) -> SegmentId {
SegmentId(Uuid::from_bytes(uuid::Bytes::from(uuid_bytes)))
}
}
/// Error type used when parsing a `SegmentId` from a string fails.