Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use std::fmt::{LowerHex, UpperHex};
use std::{
fmt::{Binary, Display},
u128,

Check warning on line 7 in src/id.rs

View workflow job for this annotation

GitHub Actions / clippy

importing legacy numeric constants

warning: importing legacy numeric constants --> src/id.rs:7:5 | 7 | u128, | ^^^^ | = help: remove this import = note: then `u128::<CONST>` will resolve to the respective associated constant = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#legacy_numeric_constants = note: `#[warn(clippy::legacy_numeric_constants)]` on by default
};

#[derive(Debug, Eq, Clone, Copy)]
Expand All @@ -26,12 +26,6 @@
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 {
Expand All @@ -40,6 +34,14 @@
}
}

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
Expand Down Expand Up @@ -96,7 +98,7 @@
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);
}

Expand Down
Loading