2个版本

0.1.1 2021年1月9日
0.1.0 2021年1月8日

#29 in #id-generator

MIT/Apache

6KB
100

萨米奥

分布式唯一ID生成器,受Twitter的雪花算法启发。

萨米奥创建的唯一ID范围在0 > n > (2 ^ 64) -1之间,也称为无符号64位整数。

用法

首先,将sarmio作为依赖项添加到你的cargo.toml文件中。

[dependencies]
sarmio = "0.1"

示例

fn main() {
    // Create new Sarmio instance with a machine-id of 255.
    let mut sarmio_one = sarmio::Sarmio::new(255);
    let mut sarmio_two = sarmio::Sarmio::new(555);
    // Sarmio implements Iterator
    // Which means you can iterate over it to create new IDs.
    let id1 = match sarmio_one.next_id() {
        Some(s) => s,
        None => 0,
    };

    let id2 = match sarmio_two.next_id() {
        Some(s) => s,
        None => 0,
    };

    // Or create a new  with next_id() syntax.

    // Decompose it, get the values like
    // Unix time in that moment, machine id
    // and the Unique ID.
    let id1_decomposed = sarmio::decompose(id1);
    let id2_decomposed = sarmio::decompose(id2);

    println!("{:?}", id1_decomposed); // ID { id: 27015264398737663, machine_id: 255, time: 1610235238 }
    println!("{:?}", id2_decomposed); // ID { id: 27015264398737963, machine_id: 555, time: 1610235238 }

    // Check which ID is older.
    let is_older = id2_decomposed.older(&id1_decomposed);

    println!("{:?}", is_older); // false

    // Check whether the ID's are created in the same machine.
    let same_machine = id2_decomposed.same_machine(&id1_decomposed);

    println!("{:?}", same_machine) // false
}

无运行时依赖