use core::marker::PhantomData;
#[repr(transparent)]
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Default, Hash)]
pub struct PodId(pub u16);
#[repr(C)]
pub struct Handle<T> {
pub(crate) id: PodId,
pub(crate) generation: u16,
_marker: PhantomData<T>,
}
impl<T> Copy for Handle<T> {}
impl<T> Clone for Handle<T> {
#[inline(always)]
fn clone(&self) -> Self {
*self
}
}
impl<T> PartialEq for Handle<T> {
#[inline(always)]
fn eq(&self, other: &Self) -> bool {
self.id == other.id && self.generation == other.generation
}
}
impl<T> Eq for Handle<T> {}
impl<T> core::fmt::Debug for Handle<T> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("Handle")
.field("id", &self.id)
.field("generation", &self.generation)
.finish()
}
}
impl<T> Handle<T> {
#[inline(always)]
pub const fn new(id: PodId) -> Self {
Self {
id,
generation: 0,
_marker: PhantomData,
}
}
#[inline(always)]
pub const fn with_generation(id: PodId, generation: u16) -> Self {
Self {
id,
generation,
_marker: PhantomData,
}
}
#[inline(always)]
pub const fn pod_id(self) -> PodId {
self.id
}
#[inline(always)]
pub const fn generation(self) -> u16 {
self.generation
}
}
impl<T: crate::element::View> Handle<T> {
#[inline]
pub fn update<R>(
self,
cx: &mut crate::async_rt::AsyncApp,
f: impl FnOnce(&mut T, &mut super::ActionScope) -> R,
) -> Option<R> {
cx.update(self, f)
}
#[inline]
pub fn read<R>(
self,
cx: &crate::async_rt::AsyncApp,
f: impl FnOnce(&T) -> R,
) -> Option<R> {
cx.read(self, f)
}
}