From bf9901a6cd98d9487174f00b9b344f6105677017 Mon Sep 17 00:00:00 2001 From: Jon Kelley Date: Sat, 17 Sep 2022 10:50:05 -0700 Subject: [PATCH] fix: use typeid of atoms for their unique ID (#558) --- packages/fermi/src/atoms/atom.rs | 9 ++++++--- packages/fermi/src/atoms/atomfamily.rs | 9 ++++++--- packages/fermi/src/atoms/atomref.rs | 7 +++++-- packages/fermi/src/root.rs | 24 +++++++++++++++++++++--- 4 files changed, 38 insertions(+), 11 deletions(-) diff --git a/packages/fermi/src/atoms/atom.rs b/packages/fermi/src/atoms/atom.rs index 2b99174b5..81e04ce0b 100644 --- a/packages/fermi/src/atoms/atom.rs +++ b/packages/fermi/src/atoms/atom.rs @@ -3,7 +3,7 @@ use crate::{AtomId, AtomRoot, Readable, Writable}; pub type Atom = fn(AtomBuilder) -> T; pub struct AtomBuilder; -impl Readable for Atom { +impl Readable for Atom { fn read(&self, _root: AtomRoot) -> Option { todo!() } @@ -11,11 +11,14 @@ impl Readable for Atom { (*self)(AtomBuilder) } fn unique_id(&self) -> AtomId { - *self as *const () + AtomId { + ptr: *self as *const (), + type_id: std::any::TypeId::of::(), + } } } -impl Writable for Atom { +impl Writable for Atom { fn write(&self, _root: AtomRoot, _value: V) { todo!() } diff --git a/packages/fermi/src/atoms/atomfamily.rs b/packages/fermi/src/atoms/atomfamily.rs index 60fc22bcb..29cecd4b2 100644 --- a/packages/fermi/src/atoms/atomfamily.rs +++ b/packages/fermi/src/atoms/atomfamily.rs @@ -4,7 +4,7 @@ use im_rc::HashMap as ImMap; pub struct AtomFamilyBuilder; pub type AtomFamily = fn(AtomFamilyBuilder) -> ImMap; -impl Readable> for AtomFamily { +impl Readable> for AtomFamily { fn read(&self, _root: AtomRoot) -> Option> { todo!() } @@ -14,11 +14,14 @@ impl Readable> for AtomFamily { } fn unique_id(&self) -> AtomId { - *self as *const () + AtomId { + ptr: *self as *const (), + type_id: std::any::TypeId::of::(), + } } } -impl Writable> for AtomFamily { +impl Writable> for AtomFamily { fn write(&self, _root: AtomRoot, _value: ImMap) { todo!() } diff --git a/packages/fermi/src/atoms/atomref.rs b/packages/fermi/src/atoms/atomref.rs index 59e412031..c103fa662 100644 --- a/packages/fermi/src/atoms/atomref.rs +++ b/packages/fermi/src/atoms/atomref.rs @@ -4,7 +4,7 @@ use std::cell::RefCell; pub struct AtomRefBuilder; pub type AtomRef = fn(AtomRefBuilder) -> T; -impl Readable> for AtomRef { +impl Readable> for AtomRef { fn read(&self, _root: AtomRoot) -> Option> { todo!() } @@ -14,7 +14,10 @@ impl Readable> for AtomRef { } fn unique_id(&self) -> AtomId { - *self as *const () + AtomId { + ptr: *self as *const (), + type_id: std::any::TypeId::of::(), + } } } diff --git a/packages/fermi/src/root.rs b/packages/fermi/src/root.rs index f77930c75..68a5d5d33 100644 --- a/packages/fermi/src/root.rs +++ b/packages/fermi/src/root.rs @@ -1,11 +1,21 @@ -use std::{any::Any, cell::RefCell, collections::HashMap, rc::Rc, sync::Arc}; +use std::{ + any::{Any, TypeId}, + cell::RefCell, + collections::HashMap, + rc::Rc, + sync::Arc, +}; use dioxus_core::ScopeId; use im_rc::HashSet; use crate::Readable; -pub type AtomId = *const (); +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub struct AtomId { + pub(crate) ptr: *const (), + pub(crate) type_id: TypeId, +} pub struct AtomRoot { pub atoms: RefCell>, @@ -44,7 +54,15 @@ impl AtomRoot { // initialize the value if it's not already initialized if let Some(slot) = atoms.get_mut(&f.unique_id()) { slot.subscribers.insert(scope); - slot.value.clone().downcast().unwrap() + match slot.value.clone().downcast() { + Ok(res) => res, + Err(e) => panic!( + "Downcasting atom failed: {:?}. Has typeid of {:?} but needs typeid of {:?}", + f.unique_id(), + e.type_id(), + TypeId::of::() + ), + } } else { let value = Rc::new(f.init()); let mut subscribers = HashSet::new();