Crates.io | timeflaketiny-rs |
lib.rs | timeflaketiny-rs |
version | 0.2.3 |
source | src |
created_at | 2022-08-04 04:56:26.4039 |
updated_at | 2022-08-04 06:40:33.815987 |
description | TimeflakeTiny is 64-bit sized timebased unique, roughly-ordered and compatible with sqlite. Inspired by original Timeflake that is 128-bit sized. |
homepage | |
repository | https://github.com/pmnxis/timeflaketiny-rs |
max_upload_size | |
id | 638539 |
size | 19,709 |
Timeflake Tiny is a 64-bit sized timebased unique, roughly-ordered and compatible with sqlite. Inspired by original library timeflake-rs that is 128-bit sized.
use TimeflakeTiny;
fn main() {
let time = SystemTime::now().duration_since(UNIX_EPOCH).unwrap();
// Generate from current time and random generated value.
println!("{}", TimeflakeTiny::random().unwrap());
// Generate from specific time and some value.
println!("{}", TimeflakeTiny::from_values(time, Some(0)).unwrap());
// When seconds parameter is `None`, the module fill with random automatically.
println!("{}", TimeflakeTiny::from_values(time, None).unwrap());
let tiny = TimeflakeTiny::from_values(Duration::from_millis(SOME_TIME), Some(SOME_RAND)).unwrap();
let huge = Timeflake::from_values(Duration::from_millis(SOME_TIME), Some(SOME_RAND as u128)).unwrap();
// Would be same uuid between timeflake and timeflake tiny
// when both value is less than U16 MAX.
assert_eq!(huge.get_uuid(), tiny.get_uuid());
// The tiny module support the original type conversion.
let huge_from_tiny = TimeflakeTiny::to_timeflake(&tiny).unwrap();
let reverted = TimeflakeTiny::from_timeflake(&huge_from_tiny).unwrap();
assert_eq!(huge.get_uuid(), huge_from_tiny.get_uuid());
// TimeflakeTiny -> Timeflake -> TimeflakeTiny
// Should be same value at above case.
assert_eq!(tiny.get_uuid(), reverted.get_uuid());
}