From 2c205f8ca56f350d5d8a52cd0a5b2e276f721cda Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Sun, 29 Jan 2023 09:17:06 -0600 Subject: [PATCH] fix some type bounds --- packages/native-core/src/passes.rs | 26 ++++++++++++------------ packages/native-core/src/real_dom.rs | 9 +++++---- packages/native-core/src/tree.rs | 30 ++++++++++++++++------------ 3 files changed, 35 insertions(+), 30 deletions(-) diff --git a/packages/native-core/src/passes.rs b/packages/native-core/src/passes.rs index 57a4b8609..525e22461 100644 --- a/packages/native-core/src/passes.rs +++ b/packages/native-core/src/passes.rs @@ -109,7 +109,7 @@ impl DirtyNodeStates { } } -pub trait Pass: Any { +pub trait Pass: Any + Send + Sync { /// This is a tuple of (T: Any, ..) type ParentDependencies: Dependancy; /// This is a tuple of (T: Any, ..) @@ -299,17 +299,17 @@ pub enum PassDirection { type PassCallback = Box bool + Send + Sync>; pub trait AnyMapLike<'a> { - fn get(self) -> Option<&'a T>; + fn get(self) -> Option<&'a T>; } impl<'a> AnyMapLike<'a> for &'a AnyMap { - fn get(self) -> Option<&'a T> { + fn get(self) -> Option<&'a T> { self.get() } } impl<'a> AnyMapLike<'a> for &'a SendAnyMap { - fn get(self) -> Option<&'a T> { + fn get(self) -> Option<&'a T> { todo!() } } @@ -327,7 +327,7 @@ pub trait Dependancy { macro_rules! impl_dependancy { ($($t:ident),*) => { - impl< $($t: Any),* > Dependancy for ($($t,)*) { + impl< $($t: Any + Send + Sync),* > Dependancy for ($($t,)*) { type ElementBorrowed<'a> = ($(&'a $t,)*) where Self: 'a; #[allow(unused_variables, clippy::unused_unit, non_snake_case)] @@ -423,7 +423,7 @@ pub fn resolve_passes( // } // impl AnyMapLike for Number { -// fn get(&self) -> Option<&T> { +// fn get(&self) -> Option<&T> { // if TypeId::of::() == TypeId::of::() { // Some(unsafe { &*(self as *const Self as *const T) }) // } else { @@ -431,7 +431,7 @@ pub fn resolve_passes( // } // } -// fn get_mut(&mut self) -> Option<&mut T> { +// fn get_mut(&mut self) -> Option<&mut T> { // if TypeId::of::() == TypeId::of::() { // Some(unsafe { &mut *(self as *mut Self as *mut T) }) // } else { @@ -530,7 +530,7 @@ pub fn resolve_passes( // } // impl AnyMapLike for NumberState { -// fn get(&self) -> Option<&T> { +// fn get(&self) -> Option<&T> { // if TypeId::of::() == TypeId::of::() { // Some(unsafe { &*(&self.add_number as *const AddNumber as *const T) }) // } else if TypeId::of::() == TypeId::of::() { @@ -540,7 +540,7 @@ pub fn resolve_passes( // } // } -// fn get_mut(&mut self) -> Option<&mut T> { +// fn get_mut(&mut self) -> Option<&mut T> { // if TypeId::of::() == TypeId::of::() { // Some(unsafe { &mut *(&mut self.add_number as *mut AddNumber as *mut T) }) // } else if TypeId::of::() == TypeId::of::() { @@ -636,7 +636,7 @@ pub fn resolve_passes( // } // impl AnyMapLike for NumberState { -// fn get(&self) -> Option<&T> { +// fn get(&self) -> Option<&T> { // if TypeId::of::() == TypeId::of::() { // Some(unsafe { &*(&self.add_number as *const AddNumber as *const T) }) // } else if TypeId::of::() == TypeId::of::() { @@ -646,7 +646,7 @@ pub fn resolve_passes( // } // } -// fn get_mut(&mut self) -> Option<&mut T> { +// fn get_mut(&mut self) -> Option<&mut T> { // if TypeId::of::() == TypeId::of::() { // Some(unsafe { &mut *(&mut self.add_number as *mut AddNumber as *mut T) }) // } else if TypeId::of::() == TypeId::of::() { @@ -718,7 +718,7 @@ pub fn resolve_passes( // } // impl AnyMapLike for NumberState { -// fn get(&self) -> Option<&T> { +// fn get(&self) -> Option<&T> { // if TypeId::of::() == TypeId::of::() { // Some(unsafe { &*(&self.add_number as *const AddNumber as *const T) }) // } else { @@ -726,7 +726,7 @@ pub fn resolve_passes( // } // } -// fn get_mut(&mut self) -> Option<&mut T> { +// fn get_mut(&mut self) -> Option<&mut T> { // if TypeId::of::() == TypeId::of::() { // Some(unsafe { &mut *(&mut self.add_number as *mut AddNumber as *mut T) }) // } else { diff --git a/packages/native-core/src/real_dom.rs b/packages/native-core/src/real_dom.rs index 9a874ea99..5a7b34d46 100644 --- a/packages/native-core/src/real_dom.rs +++ b/packages/native-core/src/real_dom.rs @@ -37,6 +37,7 @@ pub struct RealDom { impl RealDom { pub fn new(mut passes: Box<[TypeErasedPass]>) -> RealDom { let mut tree = Tree::new(); + tree.insert_slab::>(); let root_id = tree.root(); let root: &mut NodeData = tree.get_mut(root_id).unwrap(); let mut root_node: NodeData = NodeData::new(NodeType::Element(ElementNode { @@ -579,7 +580,7 @@ pub trait NodeImmutable: Sized { &self.node_data().node_type } - fn get(&self) -> Option<&T> { + fn get(&self) -> Option<&T> { self.real_dom().tree.get(self.id()) } @@ -611,7 +612,7 @@ pub trait NodeImmutable: Sized { } pub trait NodeMutable: Sized + NodeImmutable { - fn get_mut(&mut self) -> Option<&mut T>; + fn get_mut(&mut self) -> Option<&mut T>; } #[derive(Clone, Copy)] @@ -647,7 +648,7 @@ impl<'a, V: FromAnyValue + Send + Sync> NodeImmutable for NodeMut<'a, V> { } impl<'a, V: FromAnyValue + Send + Sync> NodeMutable for NodeMut<'a, V> { - fn get_mut(&mut self) -> Option<&mut T> { + fn get_mut(&mut self) -> Option<&mut T> { todo!("get_mut with mark as dirty") } } @@ -781,7 +782,7 @@ impl<'a, V: FromAnyValue + Send + Sync> NodeImmutable for NodeMutRaw<'a, V> { } impl<'a, V: FromAnyValue + Send + Sync> NodeMutable for NodeMutRaw<'a, V> { - fn get_mut(&mut self) -> Option<&mut T> { + fn get_mut(&mut self) -> Option<&mut T> { self.dom.tree.get_mut::(self.id) } } diff --git a/packages/native-core/src/tree.rs b/packages/native-core/src/tree.rs index 9c6f73457..0e80aa04c 100644 --- a/packages/native-core/src/tree.rs +++ b/packages/native-core/src/tree.rs @@ -36,6 +36,10 @@ impl Tree { Self { nodes, root } } + pub(crate) fn insert_slab(&mut self) { + self.nodes.get_or_insert_slab_mut::(); + } + fn node_slab(&self) -> &SlabStorage { self.nodes.read_slab() } @@ -164,11 +168,11 @@ impl Tree { } } - pub fn get(&self, id: NodeId) -> Option<&T> { + pub fn get(&self, id: NodeId) -> Option<&T> { self.nodes.read_slab().get(id) } - pub fn get_mut(&mut self, id: NodeId) -> Option<&mut T> { + pub fn get_mut(&mut self, id: NodeId) -> Option<&mut T> { self.nodes.write_slab().get_mut(id) } @@ -234,7 +238,7 @@ pub struct TreeStateViewEntry<'a, 'b> { } impl<'a, 'b> AnyMapLike<'a> for TreeStateViewEntry<'a, 'b> { - fn get(self) -> Option<&'a T> { + fn get(self) -> Option<&'a T> { self.view.get_slab().and_then(|slab| slab.get(self.id)) } } @@ -245,7 +249,7 @@ pub struct TreeStateView<'a, 'b> { } impl<'a, 'b> TreeStateView<'a, 'b> { - fn get_slab(&self) -> Option<&SlabStorage> { + fn get_slab(&self) -> Option<&SlabStorage> { self.nodes .get(&TypeId::of::()) .and_then(|gaurd| match gaurd { @@ -254,7 +258,7 @@ impl<'a, 'b> TreeStateView<'a, 'b> { }) } - fn get_slab_mut(&mut self) -> Option<&mut SlabStorage> { + fn get_slab_mut(&mut self) -> Option<&mut SlabStorage> { self.nodes .get_mut(&TypeId::of::()) .and_then(|gaurd| match gaurd { @@ -279,11 +283,11 @@ impl<'a, 'b> TreeStateView<'a, 'b> { T::borrow_elements_from(self.entry(id)) } - pub fn get_single(&self, id: NodeId) -> Option<&T> { + pub fn get_single(&self, id: NodeId) -> Option<&T> { self.get_slab().and_then(|slab| slab.get(id)) } - pub fn get_mut(&mut self, id: NodeId) -> Option<&mut T> { + pub fn get_mut(&mut self, id: NodeId) -> Option<&mut T> { self.get_slab_mut().and_then(|slab| slab.get_mut(id)) } @@ -543,23 +547,23 @@ impl AnySlab { } } - fn try_read_slab(&self) -> Option<&SlabStorage> { + fn try_read_slab(&self) -> Option<&SlabStorage> { self.data .get(&TypeId::of::()) .map(|x| x.as_any().downcast_ref().unwrap()) } - fn read_slab(&self) -> &SlabStorage { + fn read_slab(&self) -> &SlabStorage { self.try_read_slab().unwrap() } - fn try_write_slab(&mut self) -> Option<&mut SlabStorage> { + fn try_write_slab(&mut self) -> Option<&mut SlabStorage> { self.data .get_mut(&TypeId::of::()) .map(|x| x.as_any_mut().downcast_mut().unwrap()) } - fn write_slab(&mut self) -> &mut SlabStorage { + fn write_slab(&mut self) -> &mut SlabStorage { self.try_write_slab().unwrap() } @@ -624,11 +628,11 @@ impl EntryBuilder<'_> { .insert(self.id, value); } - pub fn get(&self) -> Option<&T> { + pub fn get(&self) -> Option<&T> { self.inner.read_slab().get(self.id) } - pub fn get_mut(&mut self) -> Option<&mut T> { + pub fn get_mut(&mut self) -> Option<&mut T> { self.inner.write_slab().get_mut(self.id) }