remove extra generic from SignalMap
This commit is contained in:
1 parent
7ea868bd08
commit
365868f636
1 file changed
+17
-19
+17
-19
@@ -4,18 +4,16 @@ use std::cell::Ref;
|
|||||||
use std::fmt::Debug;
|
use std::fmt::Debug;
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
/// A signal that can only be read from.
|
/// A read only signal that has been mapped to a new type.
|
||||||
pub struct SignalMap<T: 'static, U: ?Sized> {
|
pub struct SignalMap<U: ?Sized> {
|
||||||
inner: Signal<T>,
|
mapping: Box<dyn Fn() -> Ref<'static, U>>,
|
||||||
mapping: fn(&T) -> &U,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static, U: ?Sized> SignalMap<T, 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 {
|
pub fn new(signal: Signal<T>, mapping: fn(&T) -> &U) -> Self {
|
||||||
Self {
|
Self {
|
||||||
inner: signal,
|
mapping: Box::new(move || Ref::map(signal.read(), |v| (mapping)(v))),
|
||||||
mapping,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -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.
|
/// 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<U> {
|
||||||
Ref::map(self.inner.read(), |v| (self.mapping)(v))
|
(self.mapping)()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Run a closure with a reference to the signal's value.
|
/// Run a closure with a reference to the signal's value.
|
||||||
pub fn with<O>(&self, f: impl FnOnce(&U) -> O) -> O {
|
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.
|
/// Get the current value of the signal. This will subscribe the current scope to the signal.
|
||||||
pub fn value(&self) -> U {
|
pub fn value(&self) -> U {
|
||||||
self.read().clone()
|
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 {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.inner == other.inner
|
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 {
|
fn clone(&self) -> 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 {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.with(|v| Display::fmt(v, f))
|
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 {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.with(|v| Debug::fmt(v, f))
|
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.
|
/// 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()
|
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.
|
/// Unwraps the inner value and clones it.
|
||||||
pub fn unwrap(&self) -> U
|
pub fn unwrap(&self) -> U
|
||||||
where
|
where
|
||||||
@@ -85,7 +83,7 @@ impl<T, U: Clone + 'static> SignalMap<T, Option<U>> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Attemps to read the inner value of the Option.
|
/// 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()
|
Ref::filter_map(self.read(), |v| v.as_ref()).ok()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user