snowflaked

Crates.iosnowflaked
lib.rssnowflaked
version1.0.3
sourcesrc
created_at2022-06-13 17:15:15.381476
updated_at2024-02-24 17:12:14.858929
descriptionA crate for creating and working with snowflake ids
homepage
repositoryhttps://github.com/MrGunflame/snowflaked-rs
max_upload_size
id605213
size53,986
0xc0001a2040 (MrGunflame)

documentation

https://docs.rs/snowflaked

README

Snowflaked

Crates.io Docs.rs

A crate for creating and working with snowflake ids.

Usage

Add snowflaked to your Cargo.toml:

snowflaked = "1.0.0"

This crate provides APIs for generating new snowflake ids and defining custom snowflake types.

Snowflake Generation

Use the Generator type to create new snowflake ids:

use snowflaked::Generator;

let mut generator = Generator::new(0);
let id: u64 = generator.generate();

Or use the thread-safe sync::Generator type (requires the optional sync feature):

use snowflaked::sync::Generator;

static GENERATOR: Generator = Generator::new(0);

fn generate_id() -> u64 {
    GENERATOR.generate()
}

Using custom snowflake types

Custom snowflake types can be defined with the Snowflake trait. This trait is currently implemented for u64 and i64 and can be used to define your custom types:

use snowflaked::Snowflake;

struct UserId(u64);

impl Snowflake for UserId {
    fn from_parts(timestamp: u64, instance: u64, sequence: u64) -> Self {
        Self(u64::from_parts(timestamp, instance, sequence))
    }

    fn timestamp(&self) -> u64 {
        self.0.timestamp()
    }

    fn instance(&self) -> u64 {
        self.0.instance()
    }

    fn sequence(&self) -> u64 {
        self.0.sequence()
    }
}

License

Licensed under either

Commit count: 55

cargo fmt