1个不稳定版本
0.2.3 | 2022年8月4日 |
---|---|
0.2.2 |
|
0.2.1 |
|
0.2.0 |
|
#1212 在 数据库接口
8KB
103 行
TimeflakeTiny-rs
Timeflake Tiny是一种基于时间的64位大小唯一标识符,大致排序且与sqlite兼容。受128位大小的原始库timeflake-rs的启发。
示例代码
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());
}
依赖关系
~580KB