rafx_framework/visibility/
object_id.rs

1use std::hash::Hash;
2
3/// An opaque 64-bit handle used as a unique identifier for objects in the game world
4/// that are visible or otherwise relevant to the renderer's pipeline. Each `VisibilityObject`
5/// is associated with a specific `ObjectId`.
6#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, PartialOrd, Ord)]
7pub struct ObjectId(u64);
8
9impl Into<u64> for ObjectId {
10    fn into(self) -> u64 {
11        self.0
12    }
13}
14
15impl ObjectId {
16    pub fn new(id: u64) -> Self {
17        ObjectId(id)
18    }
19
20    pub fn from<T: 'static + Copy + Hash + Eq + PartialEq>(value: T) -> Self {
21        assert_eq!(std::mem::size_of::<T>(), std::mem::size_of::<u64>());
22        let id = unsafe { std::mem::transmute_copy(&value) };
23        ObjectId::new(id)
24    }
25
26    pub fn into<T: 'static + Copy + Hash + Eq + PartialEq>(self) -> T {
27        assert_eq!(std::mem::size_of::<T>(), std::mem::size_of::<u64>());
28        unsafe { std::mem::transmute_copy(&self.0) }
29    }
30}