#![cfg_attr(feature = "std", allow(dead_code))]
use core::num::NonZeroUsize;
const SENITEL: NonZeroUsize = unsafe { NonZeroUsize::new_unchecked(usize::MAX) };
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub(crate) struct ThreadId(pub(crate) NonZeroUsize);
impl ThreadId {
#[inline(always)]
pub(crate) const fn new(value: NonZeroUsize) -> Self {
Self(value)
}
#[inline]
pub(crate) fn current_thread() -> Self {
Self::new(SENITEL)
}
}
impl PartialEq for ThreadId {
fn eq(&self, other: &Self) -> bool {
match (self.0, other.0) {
(SENITEL, _) => false,
(_, SENITEL) => false,
(a, b) => a == b,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::format;
use core::convert::TryInto;
#[test]
fn test_thread_ids_eq() {
let a = ThreadId::new(32.try_into().unwrap());
let b = ThreadId::new(32.try_into().unwrap());
let c = ThreadId::new(16.try_into().unwrap());
assert_eq!(a, b);
assert_ne!(a, c);
}
#[test]
fn test_thread_senitel_ne() {
let a = ThreadId::current_thread();
let b = ThreadId::current_thread();
let c = ThreadId::new(32.try_into().unwrap());
assert_ne!(a, b);
assert_ne!(a, c);
assert_ne!(c, b);
}
#[test]
fn test_debug_strings() {
let a = ThreadId::current_thread();
let b = ThreadId::current_thread();
let c = ThreadId::new(32.try_into().unwrap());
assert_eq!(format!("{:?}", &a), format!("{:?}", &b));
assert_ne!(format!("{:?}", &a), format!("{:?}", &c));
}
}