소스 검색

use map in the signal iterator

Evan Almloff 1 년 전
부모
커밋
31489167f7
4개의 변경된 파일86개의 추가작업 그리고 24개의 파일을 삭제
  1. 4 4
      packages/generational-box/src/lib.rs
  2. 2 2
      packages/signals/examples/iterator.rs
  3. 37 10
      packages/signals/src/impls.rs
  4. 43 8
      packages/signals/src/map.rs

+ 4 - 4
packages/generational-box/src/lib.rs

@@ -479,7 +479,7 @@ impl Display for AlreadyBorrowedError {
 impl std::error::Error for AlreadyBorrowedError {}
 
 /// A reference to a value in a generational box.
-pub struct GenerationalRef<T: 'static> {
+pub struct GenerationalRef<T: ?Sized + 'static> {
     inner: Ref<'static, T>,
     #[cfg(any(debug_assertions, feature = "debug_borrows"))]
     borrow: GenerationalRefBorrowInfo,
@@ -487,7 +487,7 @@ pub struct GenerationalRef<T: 'static> {
 
 impl<T: 'static> GenerationalRef<T> {
     /// Map one ref type to another.
-    pub fn map<U, F>(orig: GenerationalRef<T>, f: F) -> GenerationalRef<U>
+    pub fn map<U: ?Sized, F>(orig: GenerationalRef<T>, f: F) -> GenerationalRef<U>
     where
         F: FnOnce(&T) -> &U,
     {
@@ -502,7 +502,7 @@ impl<T: 'static> GenerationalRef<T> {
     }
 
     /// Filter one ref type to another.
-    pub fn filter_map<U, F>(orig: GenerationalRef<T>, f: F) -> Option<GenerationalRef<U>>
+    pub fn filter_map<U: ?Sized, F>(orig: GenerationalRef<T>, f: F) -> Option<GenerationalRef<U>>
     where
         F: FnOnce(&T) -> Option<&U>,
     {
@@ -522,7 +522,7 @@ impl<T: 'static> GenerationalRef<T> {
     }
 }
 
-impl<T: 'static> Deref for GenerationalRef<T> {
+impl<T: ?Sized + 'static> Deref for GenerationalRef<T> {
     type Target = T;
 
     fn deref(&self) -> &Self::Target {

+ 2 - 2
packages/signals/examples/iterator.rs

@@ -16,8 +16,8 @@ fn App(cx: Scope) -> Element {
             },
             "Add one"
         }
-        for i in 0..signal().len() {
-            Child { signal: signal.map(move |v| v.get(i).unwrap()) }
+        for item in signal.iter_signals() {
+            Child { signal: item }
         }
     }
 }

+ 37 - 10
packages/signals/src/impls.rs

@@ -1,5 +1,6 @@
 use crate::rt::CopyValue;
 use crate::signal::{ReadOnlySignal, Signal, Write};
+use crate::SignalMap;
 use generational_box::GenerationalRef;
 use generational_box::GenerationalRefMut;
 
@@ -213,20 +214,20 @@ pub struct CopyValueIterator<T: 'static> {
     value: CopyValue<Vec<T>>,
 }
 
-impl<T: Clone> Iterator for CopyValueIterator<T> {
-    type Item = T;
+impl<T> Iterator for CopyValueIterator<T> {
+    type Item = GenerationalRef<T>;
 
     fn next(&mut self) -> Option<Self::Item> {
         let index = self.index;
         self.index += 1;
-        self.value.get(index).map(|v| v.clone())
+        self.value.get(index)
     }
 }
 
-impl<T: Clone + 'static> IntoIterator for CopyValue<Vec<T>> {
+impl<T: 'static> IntoIterator for CopyValue<Vec<T>> {
     type IntoIter = CopyValueIterator<T>;
 
-    type Item = T;
+    type Item = GenerationalRef<T>;
 
     fn into_iter(self) -> Self::IntoIter {
         CopyValueIterator {
@@ -256,20 +257,20 @@ pub struct SignalIterator<T: 'static> {
     value: Signal<Vec<T>>,
 }
 
-impl<T: Clone> Iterator for SignalIterator<T> {
-    type Item = T;
+impl<T> Iterator for SignalIterator<T> {
+    type Item = GenerationalRef<T>;
 
     fn next(&mut self) -> Option<Self::Item> {
         let index = self.index;
         self.index += 1;
-        self.value.get(index).map(|v| v.clone())
+        self.value.get(index)
     }
 }
 
-impl<T: Clone + 'static> IntoIterator for Signal<Vec<T>> {
+impl<T: 'static> IntoIterator for Signal<Vec<T>> {
     type IntoIter = SignalIterator<T>;
 
-    type Item = T;
+    type Item = GenerationalRef<T>;
 
     fn into_iter(self) -> Self::IntoIter {
         SignalIterator {
@@ -279,11 +280,37 @@ impl<T: Clone + 'static> IntoIterator for Signal<Vec<T>> {
     }
 }
 
+/// An iterator over items in a `Signal<Vec<T>>` that yields [`SignalMap`]s.
+pub struct MappedSignalIterator<T: 'static> {
+    index: usize,
+    length: usize,
+    value: Signal<Vec<T>>,
+}
+
+impl<T> Iterator for MappedSignalIterator<T> {
+    type Item = SignalMap<T>;
+
+    fn next(&mut self) -> Option<Self::Item> {
+        let index = self.index;
+        self.index += 1;
+        (index < self.length).then(|| self.value.map(move |v| v.get(index).unwrap()))
+    }
+}
+
 impl<T: 'static> Signal<Vec<T>> {
     /// Returns a reference to an element or `None` if out of bounds.
     pub fn get_mut(&self, index: usize) -> Option<Write<T, Vec<T>>> {
         Write::filter_map(self.write(), |v| v.get_mut(index))
     }
+
+    /// Create an iterator of [`SignalMap`]s over the inner vector.
+    pub fn iter_signals(&self) -> MappedSignalIterator<T> {
+        MappedSignalIterator {
+            index: 0,
+            length: self.read().len(),
+            value: *self,
+        }
+    }
 }
 
 impl<T: 'static> Signal<Option<T>> {

+ 43 - 8
packages/signals/src/map.rs

@@ -1,14 +1,14 @@
 use crate::CopyValue;
 use crate::Signal;
 use dioxus_core::ScopeId;
-use std::cell::Ref;
+use generational_box::GenerationalRef;
 use std::fmt::Debug;
 use std::fmt::Display;
 
 /// A read only signal that has been mapped to a new type.
 pub struct SignalMap<U: 'static + ?Sized> {
     origin_scope: ScopeId,
-    mapping: CopyValue<Box<dyn Fn() -> Ref<'static, U>>>,
+    mapping: CopyValue<Box<dyn Fn() -> GenerationalRef<U>>>,
 }
 
 impl<U: ?Sized> SignalMap<U> {
@@ -16,7 +16,9 @@ impl<U: ?Sized> SignalMap<U> {
     pub fn new<T: 'static>(signal: Signal<T>, mapping: impl Fn(&T) -> &U + 'static) -> Self {
         Self {
             origin_scope: signal.origin_scope(),
-            mapping: CopyValue::new(Box::new(move || Ref::map(signal.read(), |v| (mapping)(v)))),
+            mapping: CopyValue::new(Box::new(move || {
+                GenerationalRef::map(signal.read(), |v| (mapping)(v))
+            })),
         }
     }
 
@@ -26,7 +28,7 @@ impl<U: ?Sized> SignalMap<U> {
     }
 
     /// Get the current value of the signal. This will subscribe the current scope to the signal.
-    pub fn read(&self) -> Ref<'static, U> {
+    pub fn read(&self) -> GenerationalRef<U> {
         (self.mapping.read())()
     }
 
@@ -71,8 +73,8 @@ impl<U: ?Sized + Debug> Debug for SignalMap<U> {
 
 impl<U> SignalMap<Vec<U>> {
     /// Read a value from the inner vector.
-    pub fn get(&self, index: usize) -> Option<Ref<'static, U>> {
-        Ref::filter_map(self.read(), |v| v.get(index)).ok()
+    pub fn get(&self, index: usize) -> Option<GenerationalRef<U>> {
+        GenerationalRef::filter_map(self.read(), |v| v.get(index))
     }
 }
 
@@ -86,7 +88,40 @@ impl<U: Clone + 'static> SignalMap<Option<U>> {
     }
 
     /// Attemps to read the inner value of the Option.
-    pub fn as_ref(&self) -> Option<Ref<'static, U>> {
-        Ref::filter_map(self.read(), |v| v.as_ref()).ok()
+    pub fn as_ref(&self) -> Option<GenerationalRef<U>> {
+        GenerationalRef::filter_map(self.read(), |v| v.as_ref())
+    }
+}
+
+impl<T> std::ops::Deref for SignalMap<T> {
+    type Target = dyn Fn() -> GenerationalRef<T>;
+
+    fn deref(&self) -> &Self::Target {
+        // https://github.com/dtolnay/case-studies/tree/master/callable-types
+
+        // First we create a closure that captures something with the Same in memory layout as Self (MaybeUninit<Self>).
+        let uninit_callable = std::mem::MaybeUninit::<Self>::uninit();
+        // Then move that value into the closure. We assume that the closure now has a in memory layout of Self.
+        let uninit_closure = move || Self::read(unsafe { &*uninit_callable.as_ptr() });
+
+        // Check that the size of the closure is the same as the size of Self in case the compiler changed the layout of the closure.
+        let size_of_closure = std::mem::size_of_val(&uninit_closure);
+        assert_eq!(size_of_closure, std::mem::size_of::<Self>());
+
+        // Then cast the lifetime of the closure to the lifetime of &self.
+        fn cast_lifetime<'a, T>(_a: &T, b: &'a T) -> &'a T {
+            b
+        }
+        let reference_to_closure = cast_lifetime(
+            {
+                // The real closure that we will never use.
+                &uninit_closure
+            },
+            // We transmute self into a reference to the closure. This is safe because we know that the closure has the same memory layout as Self so &Closure == &Self.
+            unsafe { std::mem::transmute(self) },
+        );
+
+        // Cast the closure to a trait object.
+        reference_to_closure as &Self::Target
     }
 }