remove extra generic from SignalMap

This commit is contained in:
Evan Almloff committed 2023-10-17 13:45:25 -05:00
1 parent 7ea868bd08
commit 365868f636
1 file changed
+17 -19
+17 -19
View File
@@ -4,18 +4,16 @@ use std::cell::Ref;
use std::fmt::Debug;
use std::fmt::Display;
/// A signal that can only be read from.
pub struct SignalMap<T: 'static, U: ?Sized> {
inner: Signal<T>,
mapping: fn(&T) -> &U,
/// A read only signal that has been mapped to a new type.
pub struct SignalMap<U: ?Sized> {
mapping: Box<dyn Fn() -> Ref<'static, U>>,
}
impl<T: 'static, U: ?Sized> SignalMap<T, U> {
/// Create a new read-only signal.
/// Create a new mapped signal.
pub fn new(signal: Signal<T>, mapping: fn(&T) -> &U) -> Self {
Self {
inner: signal,
mapping,
mapping: Box::new(move || Ref::map(signal.read(), |v| (mapping)(v))),
}
}
@@ -26,56 +24,56 @@ impl<T: 'static, U: ?Sized> SignalMap<T, U> {
/// Get the current value of the signal. This will subscribe the current scope to the signal.
pub fn read(&self) -> Ref<U> {
Ref::map(self.inner.read(), |v| (self.mapping)(v))
(self.mapping)()
}
/// Run a closure with a reference to the signal's value.
pub fn with<O>(&self, f: impl FnOnce(&U) -> O) -> O {
self.inner.with(|v| f((self.mapping)(v)))
f(&*self.read())
}
}
impl<T: 'static, U: ?Sized + Clone> SignalMap<T, U> {
impl<U: ?Sized + Clone> SignalMap<U> {
/// Get the current value of the signal. This will subscribe the current scope to the signal.
pub fn value(&self) -> U {
self.read().clone()
}
}
impl<T: 'static, U: ?Sized> PartialEq for SignalMap<T, U> {
impl<U: ?Sized> PartialEq for SignalMap<U> {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}
impl<T, U> std::clone::Clone for SignalMap<T, U> {
impl<U> std::clone::Clone for SignalMap<U> {
fn clone(&self) -> Self {
*self
}
}
impl<T, U> Copy for SignalMap<T, U> {}
impl<U> Copy for SignalMap<U> {}
impl<T: 'static, U: ?Sized + Display> Display for SignalMap<T, U> {
impl<U: ?Sized + Display> Display for SignalMap<U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Display::fmt(v, f))
}
}
impl<T: 'static, U: ?Sized + Debug> Debug for SignalMap<T, U> {
impl<U: ?Sized + Debug> Debug for SignalMap<U> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.with(|v| Debug::fmt(v, f))
}
}
impl<T: 'static, U> SignalMap<T, Vec<U>> {
impl<U> SignalMap<Vec<U>> {
/// Read a value from the inner vector.
pub fn get(&self, index: usize) -> Option<Ref<'_, U>> {
pub fn get(&self, index: usize) -> Option<Ref<'static, U>> {
Ref::filter_map(self.read(), |v| v.get(index)).ok()
}
}
impl<T, U: Clone + 'static> SignalMap<T, Option<U>> {
impl<U: Clone + 'static> SignalMap<Option<U>> {
/// Unwraps the inner value and clones it.
pub fn unwrap(&self) -> U
where
@@ -85,7 +83,7 @@ impl<T, U: Clone + 'static> SignalMap<T, Option<U>> {
}
/// Attemps to read the inner value of the Option.
pub fn as_ref(&self) -> Option<Ref<'_, U>> {
pub fn as_ref(&self) -> Option<Ref<'static, U>> {
Ref::filter_map(self.read(), |v| v.as_ref()).ok()
}
}