From ada435ef1d2c7327c0899b6829adb92a107ea853 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anton=20=C3=96sterberg?= Date: Fri, 10 Jan 2025 14:16:51 +0100 Subject: [PATCH] reafactor: Replace from_bytes for Flake Remove function from_bytes for struct Flake in favor of more conventional "From" trait. --- src/id.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/id.rs b/src/id.rs index 9a0f4a0..f99d677 100644 --- a/src/id.rs +++ b/src/id.rs @@ -26,12 +26,6 @@ impl Flake { self.0.to_le_bytes() } - /// Creates a flake id from an array of 16 bytes. Endianness of the byte array is assumed to be - /// little endianess. - pub fn from_bytes(bytes: [u8; 16]) -> Flake { - Flake::new(u128::from_le_bytes(bytes)) - } - /// Returns a timestamp in form of number of **milliseconds** since UNIX epoch time /// (1st of January 1970 UTC). pub fn timestamp(&self) -> u64 { @@ -40,6 +34,14 @@ impl Flake { } } +impl From<[u8; 16]> for Flake { + /// Creates a flake id from an array of 16 bytes. Endianness of the byte array is assumed to be + /// little endianess. + fn from(value: [u8; 16]) -> Self { + Flake::new(u128::from_le_bytes(value)) + } +} + impl PartialEq for Flake { fn eq(&self, other: &Self) -> bool { self.0 == other.0 @@ -96,7 +98,7 @@ mod tests { fn test_byte_repr() { let id0 = Flake::new(29866156537351941961353716432896); let bytes = id0.bytes(); - let id1 = Flake::from_bytes(bytes); + let id1: Flake = bytes.into(); assert_eq!(id0, id1); }