Skip to main content

qubit_id/snowflake/
id_mode.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! ID ordering mode for Qubit snowflake IDs.
11
12/// Ordering mode encoded in a Qubit snowflake ID.
13#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
14pub enum IdMode {
15    /// Timestamp bits are stored in normal order, producing time-ordered IDs.
16    Sequential,
17    /// Timestamp bits are reversed, spreading adjacent timestamps across the ID space.
18    Spread,
19}
20
21impl IdMode {
22    /// Returns the one-bit ordinal used by the Qubit layout.
23    ///
24    /// # Returns
25    /// `0` for [`IdMode::Sequential`] and `1` for [`IdMode::Spread`].
26    pub const fn ordinal(self) -> u64 {
27        match self {
28            Self::Sequential => 0,
29            Self::Spread => 1,
30        }
31    }
32
33    /// Decodes an ID mode from a one-bit value.
34    ///
35    /// # Parameters
36    /// - `bit`: Encoded one-bit mode value.
37    ///
38    /// # Returns
39    /// [`IdMode::Sequential`] for `0`; [`IdMode::Spread`] for every non-zero
40    /// value after masking by callers.
41    pub const fn from_bit(bit: u64) -> Self {
42        if bit == 0 {
43            Self::Sequential
44        } else {
45            Self::Spread
46        }
47    }
48}