瀏覽代碼

remove AnyValueBounds and add some docs

= 2 年之前
父節點
當前提交
53b436c684
共有 3 個文件被更改,包括 25 次插入14 次删除
  1. 1 0
      packages/core/Cargo.toml
  2. 23 14
      packages/core/src/nodes.rs
  3. 1 0
      packages/native-core/Cargo.toml

+ 1 - 0
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 = []

+ 23 - 14
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<T: AnyValueBounds>(value: T) -> Self {
+    pub fn new<T: AnyValue + 'static>(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<T: AnyValueBounds>(&self) -> &T {
+    pub unsafe fn downcast_ref_unchecked<T: Any>(&self) -> &T {
         unsafe { &*(self.0.as_ref() as *const _ as *const T) }
     }
 
     /// Returns a reference to the inner value.
-    pub fn downcast_ref<T: AnyValueBounds>(&self) -> Option<&T> {
+    pub fn downcast_ref<T: Any>(&self) -> Option<&T> {
         if self.0.our_typeid() == TypeId::of::<T>() {
             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<T: AnyValueBounds>(&self) -> bool {
+    pub fn is<T: Any>(&self) -> bool {
         self.0.our_typeid() == TypeId::of::<T>()
     }
 }
@@ -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<T: Any + PartialEq + Send + Sync> AnyValueBounds for T {}
-#[cfg(not(feature = "sync_attributes"))]
-pub trait AnyValueBounds: Any + PartialEq {}
-#[cfg(not(feature = "sync_attributes"))]
-impl<T: Any + PartialEq> 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<T: Any + PartialEq + Send + Sync> 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<T: AnyValueBounds> AnyValue for T {
+#[cfg(not(feature = "sync_attributes"))]
+impl<T: Any + PartialEq> AnyValue for T {
     fn any_cmp(&self, other: &dyn AnyValue) -> bool {
         if self.our_typeid() != other.our_typeid() {
             return false;

+ 1 - 0
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"]