timeflaketiny-rs

Crates.iotimeflaketiny-rs
lib.rstimeflaketiny-rs
version0.2.3
sourcesrc
created_at2022-08-04 04:56:26.4039
updated_at2022-08-04 06:40:33.815987
descriptionTimeflakeTiny is 64-bit sized timebased unique, roughly-ordered and compatible with sqlite. Inspired by original Timeflake that is 128-bit sized.
homepage
repositoryhttps://github.com/pmnxis/timeflaketiny-rs
max_upload_size
id638539
size19,709
Jinwoo Park (pmnxis)

documentation

README

TimeflakeTiny-rs

Build Status crates.io License

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.

Example code

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());
}
Commit count: 48

cargo fmt