finish removing generic from SignalMap

This commit is contained in:
Evan Almloff committed 2023-10-17 13:50:21 -05:00
1 parent 4b8701e3eb
commit bfacf94db4
2 files changed
+16 -13

No files matched your search

+13 -10
View File
@@ -1,3 +1,4 @@
use crate::CopyValue;
use crate::Signal; use crate::Signal;
use dioxus_core::ScopeId; use dioxus_core::ScopeId;
use std::cell::Ref; use std::cell::Ref;
@@ -5,26 +6,28 @@ use std::fmt::Debug;
use std::fmt::Display; use std::fmt::Display;
/// A read only signal that has been mapped to a new type. /// A read only signal that has been mapped to a new type.
pub struct SignalMap<U: ?Sized> { pub struct SignalMap<U: 'static + ?Sized> {
mapping: Box<dyn Fn() -> Ref<'static, U>>, origin_scope: ScopeId,
mapping: CopyValue<Box<dyn Fn() -> Ref<'static, U>>>,
} }
impl<T: 'static, U: ?Sized> SignalMap<T, U> { impl<U: ?Sized> SignalMap<U> {
/// Create a new mapped signal. /// Create a new mapped signal.
pub fn new(signal: Signal<T>, mapping: fn(&T) -> &U) -> Self { pub fn new<T: 'static>(signal: Signal<T>, mapping: fn(&T) -> &U) -> Self {
Self { Self {
mapping: Box::new(move || Ref::map(signal.read(), |v| (mapping)(v))), origin_scope: signal.origin_scope(),
mapping: CopyValue::new(Box::new(move || Ref::map(signal.read(), |v| (mapping)(v)))),
} }
} }
/// Get the scope that the signal was created in. /// Get the scope that the signal was created in.
pub fn origin_scope(&self) -> ScopeId { pub fn origin_scope(&self) -> ScopeId {
self.inner.origin_scope() self.origin_scope
} }
/// Get the current value of the signal. This will subscribe the current scope to the signal. /// Get the current value of the signal. This will subscribe the current scope to the signal.
pub fn read(&self) -> Ref<U> { pub fn read(&self) -> Ref<'static, U> {
(self.mapping)() (self.mapping.read())()
} }
/// Run a closure with a reference to the signal's value. /// Run a closure with a reference to the signal's value.
@@ -42,7 +45,7 @@ impl<U: ?Sized + Clone> SignalMap<U> {
impl<U: ?Sized> PartialEq for SignalMap<U> { impl<U: ?Sized> PartialEq for SignalMap<U> {
fn eq(&self, other: &Self) -> bool { fn eq(&self, other: &Self) -> bool {
self.inner == other.inner self.mapping == other.mapping
} }
} }
@@ -77,7 +80,7 @@ impl<U: Clone + 'static> SignalMap<Option<U>> {
/// Unwraps the inner value and clones it. /// Unwraps the inner value and clones it.
pub fn unwrap(&self) -> U pub fn unwrap(&self) -> U
where where
T: Clone, U: Clone,
{ {
self.with(|v| v.clone()).unwrap() self.with(|v| v.clone()).unwrap()
} }
+3 -3
View File
@@ -171,7 +171,7 @@ impl<T: 'static> Signal<T> {
/// Get the current value of the signal. This will subscribe the current scope to the signal. /// Get the current value of the signal. This will subscribe the current scope to the signal.
/// If the signal has been dropped, this will panic. /// If the signal has been dropped, this will panic.
pub fn read(&self) -> Ref<T> { pub fn read(&self) -> Ref<'static, T> {
let inner = self.inner.read(); let inner = self.inner.read();
if let Some(effect) = Effect::current() { if let Some(effect) = Effect::current() {
let mut effect_subscribers = inner.effect_subscribers.borrow_mut(); let mut effect_subscribers = inner.effect_subscribers.borrow_mut();
@@ -200,7 +200,7 @@ impl<T: 'static> Signal<T> {
/// Get a mutable reference to the signal's value. /// Get a mutable reference to the signal's value.
/// If the signal has been dropped, this will panic. /// If the signal has been dropped, this will panic.
pub fn write(&self) -> Write<'_, T> { pub fn write(&self) -> Write<'static, T> {
let inner = self.inner.write(); let inner = self.inner.write();
let borrow = RefMut::map(inner, |v| &mut v.value); let borrow = RefMut::map(inner, |v| &mut v.value);
Write { Write {
@@ -257,7 +257,7 @@ impl<T: 'static> Signal<T> {
} }
/// Map the signal to a new type. /// Map the signal to a new type.
pub fn map<O>(self, f: fn(&T) -> &O) -> SignalMap<T, O> { pub fn map<O>(self, f: fn(&T) -> &O) -> SignalMap<O> {
SignalMap::new(self, f) SignalMap::new(self, f)
} }
} }