wasmtime/runtime/component/concurrent/
table.rs

1use crate::component::Resource;
2use std::cmp::Ordering;
3use std::fmt;
4use std::hash::{Hash, Hasher};
5use 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.
12pub struct TableId<T> {
13    rep: u32,
14    _marker: PhantomData<fn() -> T>,
15}
16
17pub trait TableDebug {
18    fn type_name() -> &'static str;
19}
20
21impl<T: TableDebug> fmt::Debug for TableId<T> {
22    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
23        write!(f, "{}({})", T::type_name(), self.rep)
24    }
25}
26
27impl<T> Hash for TableId<T> {
28    fn hash<H: Hasher>(&self, state: &mut H) {
29        self.rep.hash(state)
30    }
31}
32
33impl<T> PartialEq for TableId<T> {
34    fn eq(&self, other: &Self) -> bool {
35        self.rep == other.rep
36    }
37}
38
39impl<T> Eq for TableId<T> {}
40
41impl<T> PartialOrd for TableId<T> {
42    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
43        self.rep.partial_cmp(&other.rep)
44    }
45}
46
47impl<T> Ord for TableId<T> {
48    fn cmp(&self, other: &Self) -> Ordering {
49        self.rep.cmp(&other.rep)
50    }
51}
52
53impl<T> TableId<T> {
54    pub fn new(rep: u32) -> Self {
55        Self {
56            rep,
57            _marker: PhantomData,
58        }
59    }
60}
61
62impl<T> Clone for TableId<T> {
63    fn clone(&self) -> Self {
64        Self::new(self.rep)
65    }
66}
67
68impl<T> Copy for TableId<T> {}
69
70impl<T> TableId<T> {
71    pub fn rep(&self) -> u32 {
72        self.rep
73    }
74}
75
76impl<T: 'static> From<Resource<T>> for TableId<T> {
77    fn from(value: Resource<T>) -> Self {
78        Self {
79            rep: value.rep(),
80            _marker: PhantomData,
81        }
82    }
83}
84
85impl<T: 'static> From<TableId<T>> for Resource<T> {
86    fn from(value: TableId<T>) -> Self {
87        Resource::new_own(value.rep)
88    }
89}