Crates.io | snowflaker |
lib.rs | snowflaker |
version | 0.3.6 |
source | src |
created_at | 2024-03-07 17:43:01.628796 |
updated_at | 2024-07-01 15:56:23.954247 |
description | A Twitter snowflake algorithm simply implemented in Rust. |
homepage | https://github.com/photowey/snowflake |
repository | https://github.com/photowey/snowflake |
max_upload_size | |
id | 1166141 |
size | 50,653 |
snowflake
A Twitter
snowflake
algorithm simply implemented in Rust
.
Usage
Add this to your Cargo.toml
:
[dependencies]
snowflaker = "0.3"
# Or
# @see SnowflakeGenerator::dynamic()
snowflaker = { version = "0.3", features = ["dynamic"] }
APIs
Generator
builtin
// use the default data-center ID and worker ID
let gen = SnowflakeGenerator::builtin();
assert!(gen.is_ok());
new
let gen = SnowflakeGenerator::new(31, 31);
assert!(gen.is_ok());
let gen = SnowflakeGenerator::new(32, 32);
assert!(gen.is_err());
dynamic
@since 0.2.0
[dependencies]
snowflaker = { version = "${version}", features = ["dynamic"] }
let gen = SnowflakeGenerator::dynamic();
assert!(gen.is_ok());
let rvt = gen.unwrap().next_id();
assert!(rvt.is_ok());
Functions
next_id
// 122235238222008321
let rvt = snowflaker::next_id();
assert!(rvt.is_ok());
next_id_string
// 122256588529602560
let rvt = snowflaker::next_id_string();
assert!(rvt.is_ok());
Macros
@since 0.3.0
snowflake_builtin
let rvt = snowflake_builtin!();
assert!(rvt.is_ok());
snowflake_builtin_string
let rvt = snowflake_builtin_string!();
assert!(rvt.is_ok());
snowflake_dynamic
let rvt = snowflake_dynamic!();
assert!(rvt.is_ok());
snowflake_dynamic_string
let rvt = snowflake_dynamic_string!();
assert!(rvt.is_ok());
Custom
data-center
ID
worker
ID
let center_id = 16;
let worker_id = 16;
let gen = SnowflakeGenerator::new(center_id, worker_id);
assert!(gen.is_ok());
let rvt = gen.unwrap().next_id();
assert!(rvt.is_ok());
Test
cargo test
$ cargo test --features "dynamic" -- --show-output
$ cargo test --features "dynamic"
Docs
features
$ cargo doc --open --features dynamic