#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
pub struct Instance(pub(crate) u64);
impl From<u64> for Instance {
#[inline]
fn from(instance: u64) -> Self {
if cfg!(debug_assertions) && instance == u64::MAX {
re_log::warn!(
"u64::MAX is reserved to refer to all instances: {:#?}",
backtrace::Backtrace::new()
);
}
Self(instance)
}
}
impl Instance {
pub const ALL: Self = Self(u64::MAX);
#[inline]
pub fn get(self) -> u64 {
self.0
}
#[allow(clippy::should_implement_trait)]
#[inline]
pub fn from_iter(it: impl IntoIterator<Item = impl Into<Self>>) -> Vec<Self> {
it.into_iter().map(Into::into).collect::<Vec<_>>()
}
#[inline]
pub fn is_all(self) -> bool {
self == Self::ALL
}
#[inline]
pub fn is_specific(self) -> bool {
self != Self::ALL
}
#[inline]
pub fn specific_index(self) -> Option<Self> {
self.is_specific().then_some(self)
}
#[inline]
pub fn from_2d_image_coordinate([x, y]: [u32; 2], image_width: u64) -> Self {
Self((x as u64) + (y as u64) * image_width)
}
#[inline]
pub fn to_2d_image_coordinate(self, image_width: u64) -> [u32; 2] {
[(self.0 % image_width) as u32, (self.0 / image_width) as u32]
}
}
impl std::fmt::Display for Instance {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if self.is_all() {
"<all>".fmt(f)
} else {
re_format::format_uint(self.0).fmt(f)
}
}
}