#![doc = document_features::document_features!()]
use arrow2_convert::{ArrowDeserialize, ArrowField, ArrowSerialize};
#[derive(
Clone,
Copy,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
ArrowField,
ArrowSerialize,
ArrowDeserialize,
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Tuid {
time_ns: u64,
inc: u64,
}
impl Tuid {
pub const ZERO: Self = Self { time_ns: 0, inc: 0 };
pub const MAX: Self = Self {
time_ns: u64::MAX,
inc: u64::MAX,
};
#[inline]
#[cfg(not(target_arch = "wasm32"))] pub fn random() -> Self {
use std::cell::RefCell;
thread_local! {
pub static LATEST_TUID: RefCell<Tuid> = RefCell::new(Tuid{
time_ns: monotonic_nanos_since_epoch(),
inc: random_u64() & !(1_u64 << 63),
});
}
LATEST_TUID.with(|latest_tuid| {
let mut latest = latest_tuid.borrow_mut();
let new = Tuid {
time_ns: monotonic_nanos_since_epoch(),
inc: latest.inc + 1,
};
debug_assert!(
latest.time_ns <= new.time_ns,
"Time should be monotonically increasing"
);
*latest = new;
new
})
}
#[inline]
pub fn as_u128(&self) -> u128 {
((self.time_ns as u128) << 64) | (self.inc as u128)
}
}
#[inline]
#[cfg(not(target_arch = "wasm32"))]
fn monotonic_nanos_since_epoch() -> u64 {
use once_cell::sync::Lazy;
use std::time::Instant;
fn epoch_offset_and_start() -> (u64, Instant) {
if let Ok(duration_since_epoch) = std::time::UNIX_EPOCH.elapsed() {
let nanos_since_epoch = duration_since_epoch.as_nanos() as u64;
(nanos_since_epoch, Instant::now())
} else {
(0, Instant::now())
}
}
static START_TIME: Lazy<(u64, Instant)> = Lazy::new(epoch_offset_and_start);
START_TIME.0 + START_TIME.1.elapsed().as_nanos() as u64
}
#[inline]
#[cfg(not(target_arch = "wasm32"))]
fn random_u64() -> u64 {
let mut bytes = [0_u8; 8];
getrandom::getrandom(&mut bytes).expect("Couldn't get inc");
u64::from_le_bytes(bytes)
}
#[cfg(not(target_arch = "wasm32"))]
#[test]
fn test_tuid() {
use std::collections::{BTreeSet, HashSet};
fn is_sorted<T>(data: &[T]) -> bool
where
T: Ord,
{
data.windows(2).all(|w| w[0] <= w[1])
}
let num = 100_000;
let ids: Vec<Tuid> = (0..num).map(|_| Tuid::random()).collect();
assert!(is_sorted(&ids));
assert_eq!(ids.iter().cloned().collect::<HashSet::<Tuid>>().len(), num);
assert_eq!(ids.iter().cloned().collect::<BTreeSet::<Tuid>>().len(), num);
}