finish removing generic from SignalMap
This commit is contained in:
1 parent
4b8701e3eb
commit
bfacf94db4
2 files changed
+16
-13
No files matched your search
+13
-10
@@ -1,3 +1,4 @@
|
||||
use crate::CopyValue;
|
||||
use crate::Signal;
|
||||
use dioxus_core::ScopeId;
|
||||
use std::cell::Ref;
|
||||
@@ -5,26 +6,28 @@ use std::fmt::Debug;
|
||||
use std::fmt::Display;
|
||||
|
||||
/// A read only signal that has been mapped to a new type.
|
||||
pub struct SignalMap<U: ?Sized> {
|
||||
mapping: Box<dyn Fn() -> Ref<'static, U>>,
|
||||
pub struct SignalMap<U: 'static + ?Sized> {
|
||||
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.
|
||||
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 {
|
||||
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.
|
||||
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.
|
||||
pub fn read(&self) -> Ref<U> {
|
||||
(self.mapping)()
|
||||
pub fn read(&self) -> Ref<'static, U> {
|
||||
(self.mapping.read())()
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
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.
|
||||
pub fn unwrap(&self) -> U
|
||||
where
|
||||
T: Clone,
|
||||
U: Clone,
|
||||
{
|
||||
self.with(|v| v.clone()).unwrap()
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
/// 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();
|
||||
if let Some(effect) = Effect::current() {
|
||||
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.
|
||||
/// 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 borrow = RefMut::map(inner, |v| &mut v.value);
|
||||
Write {
|
||||
@@ -257,7 +257,7 @@ impl<T: 'static> Signal<T> {
|
||||
}
|
||||
|
||||
/// 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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user