This commit is contained in:
2025-08-15 15:48:00 -04:00
parent c5aa0a02e2
commit 78ea738b8e
8 changed files with 222 additions and 52 deletions

View File

@@ -16,6 +16,7 @@ pub struct AnyWidget;
///
/// W does not need to implement widget so that AnyWidget is valid;
/// Instead, add generic bounds on methods that take an ID if they need specific data.
#[repr(C)]
#[derive(Eq, Hash, PartialEq, Debug)]
pub struct WidgetId<W = AnyWidget> {
pub(super) ty: TypeId,
@@ -38,10 +39,16 @@ impl<W> WidgetId<W> {
_pd: PhantomData,
}
}
pub fn erase_type(self) -> WidgetId<AnyWidget> {
self.cast_type()
}
pub fn as_any(&self) -> &WidgetId<AnyWidget> {
// safety: self is repr(C) and generic only used for phantom data
unsafe { std::mem::transmute(self) }
}
fn cast_type<W2>(self) -> WidgetId<W2> {
WidgetId {
ty: self.ty,