timeflaketiny-rs

TimeflakeTiny is 64-bit sized timebased unique, roughly-ordered and compatible with sqlite. Inspired by original Timeflake that is 128-bit sized.


Keywords
timeflake
License
MIT

Documentation

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());
}