1 use crate::component::Resource; 2 use std::cmp::Ordering; 3 use std::fmt; 4 use std::hash::{Hash, Hasher}; 5 use std::marker::PhantomData; 6 7 /// Represents a `ResourceTable` entry for a `waitable` or `waitable-set`. 8 /// 9 /// This is equivalent to a `Resource<T>`, except without any tracking of borrow 10 /// status (since neither `waitable`s nor `waitable-set`s can be borrowed) or 11 /// other resource-specific bookkeeping. 12 pub struct TableId<T> { 13 rep: u32, 14 _marker: PhantomData<fn() -> T>, 15 } 16 17 pub trait TableDebug { type_name() -> &'static str18 fn type_name() -> &'static str; 19 } 20 21 impl<T: TableDebug> fmt::Debug for TableId<T> { fmt(&self, f: &mut fmt::Formatter) -> fmt::Result22 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 23 write!(f, "{}({})", T::type_name(), self.rep) 24 } 25 } 26 27 impl<T> Hash for TableId<T> { hash<H: Hasher>(&self, state: &mut H)28 fn hash<H: Hasher>(&self, state: &mut H) { 29 self.rep.hash(state) 30 } 31 } 32 33 impl<T> PartialEq for TableId<T> { eq(&self, other: &Self) -> bool34 fn eq(&self, other: &Self) -> bool { 35 self.rep == other.rep 36 } 37 } 38 39 impl<T> Eq for TableId<T> {} 40 41 impl<T> PartialOrd for TableId<T> { partial_cmp(&self, other: &Self) -> Option<Ordering>42 fn partial_cmp(&self, other: &Self) -> Option<Ordering> { 43 self.rep.partial_cmp(&other.rep) 44 } 45 } 46 47 impl<T> Ord for TableId<T> { cmp(&self, other: &Self) -> Ordering48 fn cmp(&self, other: &Self) -> Ordering { 49 self.rep.cmp(&other.rep) 50 } 51 } 52 53 impl<T> TableId<T> { new(rep: u32) -> Self54 pub fn new(rep: u32) -> Self { 55 Self { 56 rep, 57 _marker: PhantomData, 58 } 59 } 60 } 61 62 impl<T> Clone for TableId<T> { clone(&self) -> Self63 fn clone(&self) -> Self { 64 Self::new(self.rep) 65 } 66 } 67 68 impl<T> Copy for TableId<T> {} 69 70 impl<T> TableId<T> { rep(&self) -> u3271 pub fn rep(&self) -> u32 { 72 self.rep 73 } 74 } 75 76 impl<T: 'static> From<Resource<T>> for TableId<T> { from(value: Resource<T>) -> Self77 fn from(value: Resource<T>) -> Self { 78 Self { 79 rep: value.rep(), 80 _marker: PhantomData, 81 } 82 } 83 } 84 85 impl<T: 'static> From<TableId<T>> for Resource<T> { from(value: TableId<T>) -> Self86 fn from(value: TableId<T>) -> Self { 87 Resource::new_own(value.rep) 88 } 89 } 90