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