spore_vm/val/
id.rs

1use std::{hash::Hash, marker::PhantomData};
2
3/// A unique identifier for an object in `ValStore`.
4#[derive(Default)]
5pub struct ValId<T> {
6    pub(crate) vm_id: u16,
7    pub(crate) obj_id: u16,
8    pub(crate) idx: u32,
9    pub(crate) _marker: PhantomData<T>,
10}
11
12impl<T> Eq for ValId<T> {}
13impl<T> Copy for ValId<T> {}
14impl<T> std::fmt::Debug for ValId<T> {
15    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
16        f.debug_struct("ValId")
17            .field("vm_id", &self.vm_id)
18            .field("obj_id", &self.obj_id)
19            .field("idx", &self.idx)
20            .field("type", &std::any::type_name::<T>())
21            .finish()
22    }
23}
24impl<T> Clone for ValId<T> {
25    fn clone(&self) -> Self {
26        *self
27    }
28}
29impl<T> PartialEq for ValId<T> {
30    fn eq(&self, other: &Self) -> bool {
31        self.idx == other.idx
32    }
33}
34impl<T> Hash for ValId<T> {
35    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
36        self.idx.hash(state);
37    }
38}
39
40impl<T> ValId<T> {
41    /// Get the id as `usize` number.
42    pub(crate) fn as_usize(self) -> usize {
43        self.idx as usize
44    }
45}
46
47#[cfg(test)]
48mod tests {
49    use super::*;
50
51    #[test]
52    fn hacks_for_code_coverage() {
53        // There is not much value in testing this so calling function to appease code coverage
54        // tool.
55        assert_ne!(format!("{:?}", ValId::<()>::default()), "");
56    }
57}