#[derive(
Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord, serde::Serialize, serde::Deserialize,
)]
pub struct BodyHandle(u32);
impl BodyHandle {
pub const INVALID: Self = Self(u32::MAX);
#[inline]
pub const fn from_id(id: u32) -> Self {
Self(id)
}
#[inline]
pub const fn id(self) -> u32 {
self.0
}
#[inline]
pub fn is_valid(self) -> bool {
self != Self::INVALID
}
}
impl std::fmt::Display for BodyHandle {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if *self == Self::INVALID {
write!(f, "BodyHandle(INVALID)")
} else {
write!(f, "BodyHandle({})", self.0)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn from_id_roundtrips() {
let h = BodyHandle::from_id(42);
assert_eq!(h.id(), 42);
assert!(h.is_valid());
}
#[test]
fn invalid_sentinel() {
assert!(!BodyHandle::INVALID.is_valid());
assert_eq!(BodyHandle::INVALID.id(), u32::MAX);
}
#[test]
fn ordering_and_hash_by_id() {
use std::collections::HashSet;
assert!(BodyHandle::from_id(5) < BodyHandle::from_id(10));
let mut set = HashSet::new();
set.insert(BodyHandle::from_id(7));
assert!(set.contains(&BodyHandle::from_id(7)));
assert!(!set.contains(&BodyHandle::from_id(8)));
}
}