Crates.io | snowflake-rust |
lib.rs | snowflake-rust |
version | 0.5.6 |
source | src |
created_at | 2020-01-06 09:05:33.774741 |
updated_at | 2020-01-13 11:39:17.815865 |
description | 'twitter' snowflakes. |
homepage | |
repository | https://github.com/bejens/snowflake.git |
max_upload_size | |
id | 195716 |
size | 4,761 |
Kubernetes "twitter" snowflakes.this is not release version,please do not use in production.
By default the original Twitter snowflake format defines:
Add this to your Cargo.toml
:
[dependencies]
snowflake-rust = "0.5.6"
and this to your crate root:
use snowflake_rust;
use snowflake_rust::Snowflake;
fn main() {
let mut s = Snowflake::kubernetes();
let id = s.generate().unwrap();
println!("{:?}", id)
}
// singleton example
use snowflake_rust::Snowflake;
fn main() {
let id = id().unwrap();
println!("{:?}", id)
}
pub fn id() -> Option<i64> {
let instance = get_instance();
let mut sf = instance.lock().unwrap();
sf.generate()
}
fn get_instance() -> Arc<Mutex<Snowflake>> {
static mut SINGLETON: Option<Arc<Mutex<Snowflake>>> = None;
unsafe {
SINGLETON.get_or_insert_with( || {
Arc::new(Mutex::new(Snowflake::kubernetes()))
}).clone()
}
}