11 个版本

0.3.6 2024 年 7 月 1 日
0.3.5 2024 年 3 月 11 日
0.2.2 2024 年 3 月 9 日
0.1.0 2024 年 3 月 7 日

#428算法

Download history • Rust 包仓库 46/week @ 2024-04-26 • Rust 包仓库 33/week @ 2024-05-03 • Rust 包仓库 55/week @ 2024-05-10 • Rust 包仓库 44/week @ 2024-05-17 • Rust 包仓库 90/week @ 2024-05-24 • Rust 包仓库 71/week @ 2024-05-31 • Rust 包仓库 52/week @ 2024-06-07 • Rust 包仓库 17/week @ 2024-06-14 • Rust 包仓库 5/week @ 2024-06-21 • Rust 包仓库 166/week @ 2024-06-28 • Rust 包仓库 24/week @ 2024-07-05 • Rust 包仓库 25/week @ 2024-07-12 • Rust 包仓库 22/week @ 2024-07-19 • Rust 包仓库 38/week @ 2024-07-26 • Rust 包仓库 18/week @ 2024-08-02 • Rust 包仓库 41/week @ 2024-08-09 • Rust 包仓库

每月 125 次下载
用于 configer

Apache-2.0

37KB
423

雪花

使用 Rust 简单实现的 Twitter snowflake 算法。

Twitter 雪花

API 文档

1.用法

将此添加到你的 Cargo.toml

[dependencies]
snowflaker = "0.3"

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

2.APIs

2.1.生成器

2.1.1.内置

// 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.动态

  • @since0.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.函数

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.

  • @since0.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.自定义

  • 数据中心 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.测试

3.1.cargo test

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

4.文档

4.1.功能

$ cargo doc --open --features dynamic

依赖项

~3.5–5.5MB
~107K SLoC