#snowflake #twitter #algorithm #implemented #dynamic

snowflaker

A Twitter snowflake algorithm simply implemented in Rust

11 releases

0.3.6 Jul 1, 2024
0.3.5 Mar 11, 2024
0.2.2 Mar 9, 2024
0.1.0 Mar 7, 2024

#520 in Algorithms

Download history 32/week @ 2024-07-22 26/week @ 2024-07-29 22/week @ 2024-08-05 43/week @ 2024-08-12 19/week @ 2024-08-19 25/week @ 2024-08-26 2/week @ 2024-09-02 9/week @ 2024-09-09 37/week @ 2024-09-16 34/week @ 2024-09-23 27/week @ 2024-09-30 39/week @ 2024-10-07 98/week @ 2024-10-14 69/week @ 2024-10-21 23/week @ 2024-10-28 18/week @ 2024-11-04

224 downloads per month
Used in configer

Apache-2.0

37KB
423 lines

snowflake

A Twitter snowflake algorithm simply implemented in Rust.

Twitter Snowflake

APIs Documents

1.Usage

Add this to your Cargo.toml:

[dependencies]
snowflaker = "0.3"

# Or
# @see SnowflakeGenerator::dynamic()
snowflaker = { version = "0.3", features = ["dynamic"] }

2.APIs

2.1.Generator

2.1.1.builtin

// use the default data-center ID and worker ID
let gen = SnowflakeGenerator::builtin();
assert!(gen.is_ok());

2.1.2.new

let gen = SnowflakeGenerator::new(31, 31);
assert!(gen.is_ok());

let gen = SnowflakeGenerator::new(32, 32);
assert!(gen.is_err());

2.1.3.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());

2.2.Functions

2.2.1.next_id

// 122235238222008321
let rvt = snowflaker::next_id();
assert!(rvt.is_ok());

2.2.2.next_id_string

// 122256588529602560
let rvt = snowflaker::next_id_string();
assert!(rvt.is_ok());

2.3.Macros

  • @since 0.3.0

2.3.1.snowflake_builtin

let rvt = snowflake_builtin!();
assert!(rvt.is_ok());

2.3.2.snowflake_builtin_string

let rvt = snowflake_builtin_string!();
assert!(rvt.is_ok());

2.3.3.snowflake_dynamic

let rvt = snowflake_dynamic!();
assert!(rvt.is_ok());

2.3.4.snowflake_dynamic_string

let rvt = snowflake_dynamic_string!();
assert!(rvt.is_ok());

2.4.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());

3.Test

3.1.cargo test

$ cargo test --features "dynamic" -- --show-output
$ cargo test --features "dynamic"

4.Docs

4.1.features

$ cargo doc --open --features dynamic

Dependencies

~3.5–5.5MB
~110K SLoC