From 53b436c684325567650edda2ac9f971a9d4e286e Mon Sep 17 00:00:00 2001 From: = Date: Sun, 11 Dec 2022 21:40:40 -0600 Subject: [PATCH] remove AnyValueBounds and add some docs --- packages/core/Cargo.toml | 1 + packages/core/src/nodes.rs | 37 ++++++++++++++++++++------------- packages/native-core/Cargo.toml | 1 + 3 files changed, 25 insertions(+), 14 deletions(-) diff --git a/packages/core/Cargo.toml b/packages/core/Cargo.toml index 367cdf400..b4ce6010e 100644 --- a/packages/core/Cargo.toml +++ b/packages/core/Cargo.toml @@ -44,4 +44,5 @@ dioxus = { path = "../dioxus" } [features] default = [] serialize = ["serde"] +# Adds a Sync + Send bound on any values in attributes sync_attributes = [] \ No newline at end of file diff --git a/packages/core/src/nodes.rs b/packages/core/src/nodes.rs index cc1c5f907..07f9739f6 100644 --- a/packages/core/src/nodes.rs +++ b/packages/core/src/nodes.rs @@ -390,7 +390,7 @@ impl PartialEq for AnyValueContainer { impl AnyValueContainer { /// Create a new AnyValueContainer containing the specified data. - pub fn new(value: T) -> Self { + pub fn new(value: T) -> Self { #[cfg(feature = "sync_attributes")] return Self(std::sync::Arc::new(value)); #[cfg(not(feature = "sync_attributes"))] @@ -401,12 +401,12 @@ impl AnyValueContainer { /// /// # Safety /// The caller must ensure that the type of the inner value is `T`. - pub unsafe fn downcast_ref_unchecked(&self) -> &T { + pub unsafe fn downcast_ref_unchecked(&self) -> &T { unsafe { &*(self.0.as_ref() as *const _ as *const T) } } /// Returns a reference to the inner value. - pub fn downcast_ref(&self) -> Option<&T> { + pub fn downcast_ref(&self) -> Option<&T> { if self.0.our_typeid() == TypeId::of::() { Some(unsafe { self.downcast_ref_unchecked() }) } else { @@ -415,7 +415,7 @@ impl AnyValueContainer { } /// Checks if the inner value is of type `T`. - pub fn is(&self) -> bool { + pub fn is(&self) -> bool { self.0.our_typeid() == TypeId::of::() } } @@ -480,15 +480,8 @@ impl<'a> PartialEq for AttributeValue<'a> { } } -#[cfg(feature = "sync_attributes")] -pub trait AnyValueBounds: Any + PartialEq + Sync + Send {} -#[cfg(feature = "sync_attributes")] -impl AnyValueBounds for T {} -#[cfg(not(feature = "sync_attributes"))] -pub trait AnyValueBounds: Any + PartialEq {} -#[cfg(not(feature = "sync_attributes"))] -impl AnyValueBounds for T {} - +// Some renderers need attributes to be sync and send. The rest of the attributes are already sync and send, but there is no way to force Any values to be sync and send on the renderer side. +// The sync_attributes flag restricts any valuse to be sync and send. #[doc(hidden)] #[cfg(feature = "sync_attributes")] pub trait AnyValue: Sync + Send { @@ -496,6 +489,21 @@ pub trait AnyValue: Sync + Send { fn our_typeid(&self) -> TypeId; } +#[cfg(feature = "sync_attributes")] +impl AnyValue for T { + fn any_cmp(&self, other: &dyn AnyValue) -> bool { + if self.our_typeid() != other.our_typeid() { + return false; + } + + self == unsafe { &*(other as *const _ as *const T) } + } + + fn our_typeid(&self) -> TypeId { + self.type_id() + } +} + #[doc(hidden)] #[cfg(not(feature = "sync_attributes"))] pub trait AnyValue { @@ -503,7 +511,8 @@ pub trait AnyValue { fn our_typeid(&self) -> TypeId; } -impl AnyValue for T { +#[cfg(not(feature = "sync_attributes"))] +impl AnyValue for T { fn any_cmp(&self, other: &dyn AnyValue) -> bool { if self.our_typeid() != other.our_typeid() { return false; diff --git a/packages/native-core/Cargo.toml b/packages/native-core/Cargo.toml index 057317779..ce56fd5da 100644 --- a/packages/native-core/Cargo.toml +++ b/packages/native-core/Cargo.toml @@ -30,4 +30,5 @@ dioxus = { path = "../dioxus", version = "^0.2.1" } [features] default = ["parallel"] +# This makes core only accept sync and send any values for attributes to allow for parallel resolution of passes. parallel = ["dioxus-core/sync_attributes"] \ No newline at end of file