❄️ unique id generator docs.rs/snof/latest/snof/
0

Configure Feed

Select the types of activity you want to include in your feed.

[fix] formatting, trailing whitespaces

Maxwell (Feb 5, 2026, 9:29 PM +0100) c6164820 1c266b05

+10 -9
+10 -9
src/lib.rs
··· 1 1 //! ### ❄️ snof 2 - //! 2 + //! 3 3 //! *snof* is a unique ID generator. Loosely based on snowflake ID-s, *snof* 4 4 //! generates 64 bit long identifiers consisting of a 32 bit millisecond-based 5 5 //! timestamp, and 22 bits of sequence distinguishing identifiers generated within 6 6 //! the same millisecond. 7 - //! 7 + //! 8 8 //! The generator uses atomic operations for tracking state, thus it provides a 9 9 //! thread-safe, lock-free way of generating unique ID-s. In case of the sequence 10 10 //! being exhausted, or the clock moving backwards, the generator spins until 11 11 //! validity is restored. 12 - //! 12 + //! 13 13 //! #### Usage 14 - //! 14 + //! 15 15 //! ```rust 16 16 //! use snof::SnowflakeGenerator; 17 - //! 17 + //! 18 18 //! fn main() { 19 19 //! let generator = Arc::new(SnowflakeGenerator::new()); 20 - //! 20 + //! 21 21 //! let threads: Vec<_> = (0..4).map(|_| { 22 22 //! let other_generator = Arc::clone(&generator); 23 23 //! thread::spawn(move || { ··· 25 25 //! println!("thread id: {}", id.0); 26 26 //! }) 27 27 //! }).collect(); 28 - //! 28 + //! 29 29 //! for t in threads { t.join().unwrap(); } 30 30 //! } 31 31 //! ``` ··· 72 72 loop { 73 73 let last_ts = current_bits >> SEQUENCE_BITS; 74 74 let last_seq = current_bits & SEQUENCE_MASK; 75 - 75 + 76 76 let now_ms = unix_timestamp_now_ms(); 77 77 let now_ts = u64::try_from(now_ms.saturating_sub(EPOCH)) 78 78 .expect("Timestamp exceeds u64 capacity"); ··· 116 116 impl Snowflake { 117 117 /// Construct a [`Snowflake`] from timestamp and sequence. 118 118 fn new(timestamp: u128, sequence: u64) -> Self { 119 - let shifted = u64::try_from(timestamp - EPOCH).expect("Timestamp overflow") << SEQUENCE_BITS; 119 + let shifted = 120 + u64::try_from(timestamp - EPOCH).expect("Timestamp overflow") << SEQUENCE_BITS; 120 121 Snowflake(shifted | (sequence & SEQUENCE_MASK)) 121 122 } 122 123