velox_engine/pod/
handle.rs1use core::marker::PhantomData;
4
5#[repr(transparent)]
7#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Default, Hash)]
8pub struct PodId(pub u16);
9
10#[repr(C)]
12pub struct Handle<T> {
13 pub(crate) id: PodId,
14 pub(crate) generation: u16,
15 _marker: PhantomData<T>,
16}
17
18impl<T> Copy for Handle<T> {}
19impl<T> Clone for Handle<T> {
20 #[inline(always)]
21 fn clone(&self) -> Self {
22 *self
23 }
24}
25
26impl<T> PartialEq for Handle<T> {
27 #[inline(always)]
28 fn eq(&self, other: &Self) -> bool {
29 self.id == other.id && self.generation == other.generation
30 }
31}
32impl<T> Eq for Handle<T> {}
33
34impl<T> core::fmt::Debug for Handle<T> {
35 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
36 f.debug_struct("Handle")
37 .field("id", &self.id)
38 .field("generation", &self.generation)
39 .finish()
40 }
41}
42
43impl<T> Handle<T> {
44 #[inline(always)]
46 pub const fn new(id: PodId) -> Self {
47 Self {
48 id,
49 generation: 0,
50 _marker: PhantomData,
51 }
52 }
53
54 #[inline(always)]
56 pub const fn with_generation(id: PodId, generation: u16) -> Self {
57 Self {
58 id,
59 generation,
60 _marker: PhantomData,
61 }
62 }
63
64 #[inline(always)]
66 pub const fn pod_id(self) -> PodId {
67 self.id
68 }
69
70 #[inline(always)]
72 pub const fn generation(self) -> u16 {
73 self.generation
74 }
75}
76
77impl<T: crate::element::View> Handle<T> {
78 #[inline]
82 pub fn update<R>(
83 self,
84 cx: &mut crate::async_rt::AsyncApp,
85 f: impl FnOnce(&mut T, &mut super::ActionScope) -> R,
86 ) -> Option<R> {
87 cx.update(self, f)
88 }
89
90 #[inline]
92 pub fn read<R>(
93 self,
94 cx: &crate::async_rt::AsyncApp,
95 f: impl FnOnce(&T) -> R,
96 ) -> Option<R> {
97 cx.read(self, f)
98 }
99}
100