make mapped signal readable
This commit is contained in:
1 parent
1af8f56e89
commit
c8937cb4df
8 files changed
+176
-126
No files matched your search
@@ -25,7 +25,7 @@ futures-util = { workspace = true }
|
|||||||
flume = { version = "0.11.0", default-features = false, features = ["async"] }
|
flume = { version = "0.11.0", default-features = false, features = ["async"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
dioxus = { workspace = true }
|
dioxus = { workspace = true, features = ["desktop"]}
|
||||||
# dioxus-desktop = { workspace = true }
|
# dioxus-desktop = { workspace = true }
|
||||||
tokio = { version = "1", features = ["full"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
tracing-subscriber = "0.3.17"
|
tracing-subscriber = "0.3.17"
|
||||||
|
|||||||
@@ -0,0 +1,51 @@
|
|||||||
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
use dioxus::prelude::*;
|
||||||
|
use dioxus_signals::Signal;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
launch(app);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn app() -> Element {
|
||||||
|
let mut vec = use_signal(|| vec![0]);
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
button {
|
||||||
|
onclick: move |_| {
|
||||||
|
let mut write = vec.write();
|
||||||
|
let len = write.len() as i32;
|
||||||
|
write.push(len);
|
||||||
|
},
|
||||||
|
"Create"
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
onclick: move |_| {
|
||||||
|
vec.write().pop();
|
||||||
|
},
|
||||||
|
"Destroy"
|
||||||
|
}
|
||||||
|
|
||||||
|
for i in 0..vec.len() {
|
||||||
|
Child { count: vec.map(move |v| &v[i]) }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn Child(count: MappedSignal<i32, Signal<Vec<i32>>>) -> Element {
|
||||||
|
use_memo({
|
||||||
|
to_owned![count];
|
||||||
|
move || {
|
||||||
|
let value = count.read();
|
||||||
|
print!("Child value: {value}");
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
rsx! {
|
||||||
|
div {
|
||||||
|
"Child: {count}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -2,11 +2,11 @@ use crate::read::Readable;
|
|||||||
use crate::write::Writable;
|
use crate::write::Writable;
|
||||||
use crate::Write;
|
use crate::Write;
|
||||||
use dioxus_core::prelude::{IntoAttributeValue, ScopeId};
|
use dioxus_core::prelude::{IntoAttributeValue, ScopeId};
|
||||||
use generational_box::{AnyStorage, GenerationalRef, UnsyncStorage};
|
use generational_box::{AnyStorage, UnsyncStorage};
|
||||||
use std::{cell::Ref, mem::MaybeUninit, ops::Deref};
|
use std::{mem::MaybeUninit, ops::Deref};
|
||||||
|
|
||||||
use super::get_global_context;
|
use super::get_global_context;
|
||||||
use crate::{MappedSignal, Signal};
|
use crate::Signal;
|
||||||
|
|
||||||
/// A signal that can be accessed from anywhere in the application and created in a static
|
/// A signal that can be accessed from anywhere in the application and created in a static
|
||||||
pub struct GlobalSignal<T> {
|
pub struct GlobalSignal<T> {
|
||||||
@@ -60,15 +60,6 @@ impl<T: 'static> GlobalSignal<T> {
|
|||||||
self.signal().with_mut(f)
|
self.signal().with_mut(f)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map the signal to a new type.
|
|
||||||
pub fn map<O>(
|
|
||||||
&self,
|
|
||||||
f: impl Fn(&T) -> &O + 'static,
|
|
||||||
) -> MappedSignal<GenerationalRef<Ref<'static, O>>> {
|
|
||||||
// MappedSignal::new(self.signal(), f)
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the generational id of the signal.
|
/// Get the generational id of the signal.
|
||||||
pub fn id(&self) -> generational_box::GenerationalBoxId {
|
pub fn id(&self) -> generational_box::GenerationalBoxId {
|
||||||
self.signal().id()
|
self.signal().id()
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ use crate::copy_value::CopyValue;
|
|||||||
use crate::read::Readable;
|
use crate::read::Readable;
|
||||||
use crate::signal::Signal;
|
use crate::signal::Signal;
|
||||||
use crate::write::Writable;
|
use crate::write::Writable;
|
||||||
use crate::{GlobalMemo, GlobalSignal, ReadOnlySignal, SignalData};
|
use crate::{GlobalMemo, GlobalSignal, MappedSignal, ReadOnlySignal, SignalData};
|
||||||
use generational_box::Storage;
|
use generational_box::Storage;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
@@ -10,7 +10,7 @@ use std::{
|
|||||||
ops::{Add, Div, Mul, Sub},
|
ops::{Add, Div, Mul, Sub},
|
||||||
};
|
};
|
||||||
|
|
||||||
macro_rules! read_impls {
|
macro_rules! default_impl {
|
||||||
($ty:ident $(: $extra_bounds:path)? $(, $bound_ty:ident : $bound:path, $vec_bound_ty:ident : $vec_bound:path)?) => {
|
($ty:ident $(: $extra_bounds:path)? $(, $bound_ty:ident : $bound:path, $vec_bound_ty:ident : $vec_bound:path)?) => {
|
||||||
$(
|
$(
|
||||||
impl<T: Default + 'static, $bound_ty: $bound> Default for $ty<T, $bound_ty> {
|
impl<T: Default + 'static, $bound_ty: $bound> Default for $ty<T, $bound_ty> {
|
||||||
@@ -20,7 +20,11 @@ macro_rules! read_impls {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
)?
|
)?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! read_impls {
|
||||||
|
($ty:ident $(: $extra_bounds:path)? $(, $bound_ty:ident : $bound:path, $vec_bound_ty:ident : $vec_bound:path)?) => {
|
||||||
impl<T: $($extra_bounds + )? Display + 'static $(,$bound_ty: $bound)?> Display for $ty<T $(, $bound_ty)?> {
|
impl<T: $($extra_bounds + )? Display + 'static $(,$bound_ty: $bound)?> Display for $ty<T $(, $bound_ty)?> {
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
@@ -113,6 +117,7 @@ macro_rules! write_impls {
|
|||||||
}
|
}
|
||||||
|
|
||||||
read_impls!(CopyValue, S: Storage<T>, S: Storage<Vec<T>>);
|
read_impls!(CopyValue, S: Storage<T>, S: Storage<Vec<T>>);
|
||||||
|
default_impl!(CopyValue, S: Storage<T>, S: Storage<Vec<T>>);
|
||||||
write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
|
write_impls!(CopyValue, Storage<T>, Storage<Vec<T>>);
|
||||||
|
|
||||||
impl<T: 'static, S: Storage<T>> Clone for CopyValue<T, S> {
|
impl<T: 'static, S: Storage<T>> Clone for CopyValue<T, S> {
|
||||||
@@ -124,6 +129,7 @@ impl<T: 'static, S: Storage<T>> Clone for CopyValue<T, S> {
|
|||||||
impl<T: 'static, S: Storage<T>> Copy for CopyValue<T, S> {}
|
impl<T: 'static, S: Storage<T>> Copy for CopyValue<T, S> {}
|
||||||
|
|
||||||
read_impls!(Signal, S: Storage<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
|
read_impls!(Signal, S: Storage<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
|
||||||
|
default_impl!(Signal, S: Storage<SignalData<T>>, S: Storage<SignalData<Vec<T>>>);
|
||||||
write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
|
write_impls!(Signal, Storage<SignalData<T>>, Storage<SignalData<Vec<T>>>);
|
||||||
|
|
||||||
impl<T: 'static, S: Storage<SignalData<T>>> Clone for Signal<T, S> {
|
impl<T: 'static, S: Storage<SignalData<T>>> Clone for Signal<T, S> {
|
||||||
@@ -139,6 +145,11 @@ read_impls!(
|
|||||||
S: Storage<SignalData<T>>,
|
S: Storage<SignalData<T>>,
|
||||||
S: Storage<SignalData<Vec<T>>>
|
S: Storage<SignalData<Vec<T>>>
|
||||||
);
|
);
|
||||||
|
default_impl!(
|
||||||
|
ReadOnlySignal,
|
||||||
|
S: Storage<SignalData<T>>,
|
||||||
|
S: Storage<SignalData<Vec<T>>>
|
||||||
|
);
|
||||||
|
|
||||||
impl<T: 'static, S: Storage<SignalData<T>>> Clone for ReadOnlySignal<T, S> {
|
impl<T: 'static, S: Storage<SignalData<T>>> Clone for ReadOnlySignal<T, S> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
@@ -149,5 +160,8 @@ impl<T: 'static, S: Storage<SignalData<T>>> Clone for ReadOnlySignal<T, S> {
|
|||||||
impl<T: 'static, S: Storage<SignalData<T>>> Copy for ReadOnlySignal<T, S> {}
|
impl<T: 'static, S: Storage<SignalData<T>>> Copy for ReadOnlySignal<T, S> {}
|
||||||
|
|
||||||
read_impls!(GlobalSignal);
|
read_impls!(GlobalSignal);
|
||||||
|
default_impl!(GlobalSignal);
|
||||||
|
|
||||||
read_impls!(GlobalMemo: PartialEq);
|
read_impls!(GlobalMemo: PartialEq);
|
||||||
|
|
||||||
|
read_impls!(MappedSignal, S: Readable, S: Readable);
|
||||||
+84
-90
@@ -1,107 +1,101 @@
|
|||||||
|
use std::{ops::Deref, rc::Rc};
|
||||||
|
|
||||||
use crate::read::Readable;
|
use crate::read::Readable;
|
||||||
use crate::CopyValue;
|
use dioxus_core::prelude::*;
|
||||||
use crate::Signal;
|
|
||||||
use crate::SignalData;
|
|
||||||
use dioxus_core::ScopeId;
|
|
||||||
use generational_box::AnyStorage;
|
|
||||||
use generational_box::Storage;
|
|
||||||
use generational_box::UnsyncStorage;
|
|
||||||
use std::fmt::Debug;
|
|
||||||
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 MappedSignal<U: ?Sized + 'static, S: AnyStorage = UnsyncStorage> {
|
pub struct MappedSignal<O: ?Sized + 'static, R: Readable> {
|
||||||
mapping: CopyValue<Box<dyn Fn() -> S::Ref<U>>>,
|
readable: R,
|
||||||
|
mapping: Rc<dyn Fn(&R::Target) -> &O + 'static>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MappedSignal<()> {
|
impl<O: ?Sized, R: Readable + Clone> Clone for MappedSignal<O, R> {
|
||||||
/// Create a new mapped signal.
|
fn clone(&self) -> Self {
|
||||||
pub fn new<R, T, S, U>(readable: R, mapping: impl Fn(&T) -> &U + 'static) -> MappedSignal<U, S>
|
MappedSignal {
|
||||||
where
|
readable: self.readable.clone(),
|
||||||
S: Storage<SignalData<T>>,
|
mapping: self.mapping.clone(),
|
||||||
U: ?Sized,
|
}
|
||||||
{
|
|
||||||
todo!()
|
|
||||||
// MappedSignal {
|
|
||||||
// mapping: CopyValue::new(Box::new(move || S::map(signal.read(), &mapping))),
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// impl<U: ?Sized> MappedSignal<U> {
|
impl<O, R> MappedSignal<O, R>
|
||||||
// /// Get the current value of the signal. This will subscribe the current scope to the signal.
|
where
|
||||||
// pub fn read(&self) -> U {
|
O: ?Sized,
|
||||||
// (self.mapping.read())()
|
R: Readable + 'static,
|
||||||
// }
|
{
|
||||||
|
/// Create a new mapped signal.
|
||||||
|
pub(crate) fn new(readable: R, mapping: impl Fn(&R::Target) -> &O + 'static) -> Self {
|
||||||
|
MappedSignal {
|
||||||
|
readable,
|
||||||
|
mapping: Rc::new(mapping),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// /// Run a closure with a reference to the signal's value.
|
impl<O, R> Readable for MappedSignal<O, R>
|
||||||
// pub fn with<O>(&self, f: impl FnOnce(U) -> O) -> O {
|
where
|
||||||
// f(self.read())
|
O: ?Sized,
|
||||||
// }
|
R: Readable,
|
||||||
// }
|
{
|
||||||
|
type Target = O;
|
||||||
|
type Ref<J: ?Sized + 'static> = R::Ref<J>;
|
||||||
|
|
||||||
// impl<U: ?Sized + Clone> MappedSignal<U> {
|
fn map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> &U>(
|
||||||
// /// Get the current value of the signal. This will subscribe the current scope to the signal.
|
ref_: Self::Ref<I>,
|
||||||
// pub fn value(&self) -> U {
|
f: F,
|
||||||
// self.read().clone()
|
) -> Self::Ref<U> {
|
||||||
// }
|
R::map_ref(ref_, f)
|
||||||
// }
|
}
|
||||||
|
|
||||||
// impl<U: ?Sized> PartialEq for MappedSignal<U> {
|
fn try_map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> Option<&U>>(
|
||||||
// fn eq(&self, other: &Self) -> bool {
|
ref_: Self::Ref<I>,
|
||||||
// self.mapping == other.mapping
|
f: F,
|
||||||
// }
|
) -> Option<Self::Ref<U>> {
|
||||||
// }
|
R::try_map_ref(ref_, f)
|
||||||
|
}
|
||||||
|
|
||||||
// impl<U> std::clone::Clone for MappedSignal<U> {
|
fn try_read(&self) -> Result<Self::Ref<O>, generational_box::BorrowError> {
|
||||||
// fn clone(&self) -> Self {
|
self.readable
|
||||||
// *self
|
.try_read()
|
||||||
// }
|
.map(|ref_| R::map_ref(ref_, |r| (self.mapping)(r)))
|
||||||
// }
|
}
|
||||||
|
|
||||||
// impl<U> Copy for MappedSignal<U> {}
|
fn peek(&self) -> Self::Ref<Self::Target> {
|
||||||
|
R::map_ref(self.readable.peek(), |r| (self.mapping)(r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// impl<U: Display> Display for MappedSignal<U> {
|
impl<O, R> IntoAttributeValue for MappedSignal<O, R>
|
||||||
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
where
|
||||||
// self.with(|v| Display::fmt(&v, f))
|
O: Clone + IntoAttributeValue + ?Sized,
|
||||||
// }
|
R: Readable + 'static,
|
||||||
// }
|
{
|
||||||
|
fn into_value(self) -> dioxus_core::AttributeValue {
|
||||||
|
self.with(|f| f.clone().into_value())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// impl<U: Debug> Debug for MappedSignal<U> {
|
impl<O, R> PartialEq for MappedSignal<O, R>
|
||||||
// fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
where
|
||||||
// self.with(|v| Debug::fmt(&v, f))
|
O: ?Sized,
|
||||||
// }
|
R: PartialEq + Readable + 'static,
|
||||||
// }
|
{
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.readable == other.readable && std::ptr::eq(&self.mapping, &other.mapping)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// impl<T> std::ops::Deref for MappedSignal<T> {
|
/// Allow calling a signal with signal() syntax
|
||||||
// type Target = dyn Fn() -> T;
|
///
|
||||||
|
/// Currently only limited to copy types, though could probably specialize for string/arc/rc
|
||||||
|
impl<O, R> Deref for MappedSignal<O, R>
|
||||||
|
where
|
||||||
|
O: Clone,
|
||||||
|
R: Readable + 'static,
|
||||||
|
{
|
||||||
|
type Target = dyn Fn() -> O;
|
||||||
|
|
||||||
// fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
// // https://github.com/dtolnay/case-studies/tree/master/callable-types
|
Readable::deref_impl(self)
|
||||||
|
}
|
||||||
// // 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
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
@@ -3,13 +3,15 @@ use std::{
|
|||||||
ops::{Deref, Index},
|
ops::{Deref, Index},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
use crate::MappedSignal;
|
||||||
|
|
||||||
/// A trait for states that can be read from like [`crate::Signal`], [`crate::GlobalSignal`], or [`crate::ReadOnlySignal`]. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API. For example, instead of creating two functions, one that accepts a [`crate::Signal`] and one that accepts a [`crate::GlobalSignal`], you can create one function that accepts a [`Readable`] type.
|
/// A trait for states that can be read from like [`crate::Signal`], [`crate::GlobalSignal`], or [`crate::ReadOnlySignal`]. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API. For example, instead of creating two functions, one that accepts a [`crate::Signal`] and one that accepts a [`crate::GlobalSignal`], you can create one function that accepts a [`Readable`] type.
|
||||||
pub trait Readable {
|
pub trait Readable {
|
||||||
/// The target type of the reference.
|
/// The target type of the reference.
|
||||||
type Target: ?Sized + 'static;
|
type Target: ?Sized + 'static;
|
||||||
|
|
||||||
/// The type of the reference.
|
/// The type of the reference.
|
||||||
type Ref<R: ?Sized + 'static>: Deref<Target = R>;
|
type Ref<R: ?Sized + 'static>: Deref<Target = R> + 'static;
|
||||||
|
|
||||||
/// Map the reference to a new type.
|
/// Map the reference to a new type.
|
||||||
fn map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F)
|
fn map_ref<I: ?Sized, U: ?Sized, F: FnOnce(&I) -> &U>(ref_: Self::Ref<I>, f: F)
|
||||||
@@ -24,6 +26,14 @@ pub trait Readable {
|
|||||||
/// Try to get the current value of the state. If this is a signal, this will subscribe the current scope to the signal. If the value has been dropped, this will panic.
|
/// Try to get the current value of the state. If this is a signal, this will subscribe the current scope to the signal. If the value has been dropped, this will panic.
|
||||||
fn try_read(&self) -> Result<Self::Ref<Self::Target>, generational_box::BorrowError>;
|
fn try_read(&self) -> Result<Self::Ref<Self::Target>, generational_box::BorrowError>;
|
||||||
|
|
||||||
|
/// Map the readable type to a new type.
|
||||||
|
fn map<O>(self, f: impl Fn(&Self::Target) -> &O + 'static) -> MappedSignal<O, Self>
|
||||||
|
where
|
||||||
|
Self: Sized + 'static,
|
||||||
|
{
|
||||||
|
MappedSignal::new(self, f)
|
||||||
|
}
|
||||||
|
|
||||||
/// Get the current value of the state. If this is a signal, this will subscribe the current scope to the signal. If the value has been dropped, this will panic.
|
/// Get the current value of the state. If this is a signal, this will subscribe the current scope to the signal. If the value has been dropped, this will panic.
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn read(&self) -> Self::Ref<Self::Target> {
|
fn read(&self) -> Self::Ref<Self::Target> {
|
||||||
@@ -132,26 +142,24 @@ pub trait ReadableVecExt<T: 'static>: Readable<Target = Vec<T>> {
|
|||||||
|
|
||||||
/// Get an iterator over the values of the inner vector.
|
/// Get an iterator over the values of the inner vector.
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn iter(&self) -> ReadableValueIterator<'_, T, Self>
|
fn iter(&self) -> ReadableValueIterator<'_, Self>
|
||||||
where
|
where
|
||||||
Self: Sized,
|
Self: Sized,
|
||||||
{
|
{
|
||||||
ReadableValueIterator {
|
ReadableValueIterator {
|
||||||
index: 0,
|
index: 0,
|
||||||
value: self,
|
value: self,
|
||||||
phantom: std::marker::PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An iterator over the values of a `Readable<Vec<T>>`.
|
/// An iterator over the values of a `Readable<Vec<T>>`.
|
||||||
pub struct ReadableValueIterator<'a, T, R> {
|
pub struct ReadableValueIterator<'a, R> {
|
||||||
index: usize,
|
index: usize,
|
||||||
value: &'a R,
|
value: &'a R,
|
||||||
phantom: std::marker::PhantomData<T>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T: 'static, R: Readable<Target = Vec<T>>> Iterator for ReadableValueIterator<'a, T, R> {
|
impl<'a, T: 'static, R: Readable<Target = Vec<T>>> Iterator for ReadableValueIterator<'a, R> {
|
||||||
type Item = R::Ref<T>;
|
type Item = R::Ref<T>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
read::Readable, write::Writable, CopyValue, GlobalMemo, GlobalSignal, MappedSignal,
|
read::Readable, write::Writable, CopyValue, GlobalMemo, GlobalSignal, ReactiveContext,
|
||||||
ReactiveContext, ReadOnlySignal,
|
ReadOnlySignal,
|
||||||
};
|
};
|
||||||
use dioxus_core::{
|
use dioxus_core::{
|
||||||
prelude::{flush_sync, spawn, IntoAttributeValue},
|
prelude::{flush_sync, spawn, IntoAttributeValue},
|
||||||
@@ -187,12 +187,6 @@ impl<T: 'static, S: Storage<SignalData<T>>> Signal<T, S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Map the signal to a new type.
|
|
||||||
pub fn map<O>(self, f: impl Fn(&T) -> &O + 'static) -> MappedSignal<S::Ref<O>> {
|
|
||||||
// MappedSignal::new(self, f)
|
|
||||||
todo!()
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Get the generational id of the signal.
|
/// Get the generational id of the signal.
|
||||||
pub fn id(&self) -> generational_box::GenerationalBoxId {
|
pub fn id(&self) -> generational_box::GenerationalBoxId {
|
||||||
self.inner.id()
|
self.inner.id()
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ use crate::read::Readable;
|
|||||||
/// A trait for states that can be read from like [`crate::Signal`], or [`crate::GlobalSignal`]. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API. For example, instead of creating two functions, one that accepts a [`crate::Signal`] and one that accepts a [`crate::GlobalSignal`], you can create one function that accepts a [`Writable`] type.
|
/// A trait for states that can be read from like [`crate::Signal`], or [`crate::GlobalSignal`]. You may choose to accept this trait as a parameter instead of the concrete type to allow for more flexibility in your API. For example, instead of creating two functions, one that accepts a [`crate::Signal`] and one that accepts a [`crate::GlobalSignal`], you can create one function that accepts a [`Writable`] type.
|
||||||
pub trait Writable: Readable {
|
pub trait Writable: Readable {
|
||||||
/// The type of the reference.
|
/// The type of the reference.
|
||||||
type Mut<R: ?Sized + 'static>: DerefMut<Target = R>;
|
type Mut<R: ?Sized + 'static>: DerefMut<Target = R> + 'static;
|
||||||
|
|
||||||
/// Map the reference to a new type.
|
/// Map the reference to a new type.
|
||||||
fn map_mut<I: ?Sized, U: ?Sized, F: FnOnce(&mut I) -> &mut U>(
|
fn map_mut<I: ?Sized, U: ?Sized, F: FnOnce(&mut I) -> &mut U>(
|
||||||
@@ -68,7 +68,7 @@ pub trait Writable: Readable {
|
|||||||
where
|
where
|
||||||
Self::Target: Default,
|
Self::Target: Default,
|
||||||
{
|
{
|
||||||
self.with_mut(|v| std::mem::take(v))
|
self.with_mut(std::mem::take)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Replace the value in the Signal, returning the old value.
|
/// Replace the value in the Signal, returning the old value.
|
||||||
@@ -184,26 +184,24 @@ pub trait WritableVecExt<T: 'static>: Writable<Target = Vec<T>> {
|
|||||||
|
|
||||||
/// Gets an iterator over the values of the vector.
|
/// Gets an iterator over the values of the vector.
|
||||||
#[track_caller]
|
#[track_caller]
|
||||||
fn iter_mut(&self) -> WritableValueIterator<T, Self>
|
fn iter_mut(&self) -> WritableValueIterator<Self>
|
||||||
where
|
where
|
||||||
Self: Sized + Clone,
|
Self: Sized + Clone,
|
||||||
{
|
{
|
||||||
WritableValueIterator {
|
WritableValueIterator {
|
||||||
index: 0,
|
index: 0,
|
||||||
value: self.clone(),
|
value: self.clone(),
|
||||||
phantom: std::marker::PhantomData,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An iterator over the values of a `Writable<Vec<T>>`.
|
/// An iterator over the values of a `Writable<Vec<T>>`.
|
||||||
pub struct WritableValueIterator<T, R> {
|
pub struct WritableValueIterator<R> {
|
||||||
index: usize,
|
index: usize,
|
||||||
value: R,
|
value: R,
|
||||||
phantom: std::marker::PhantomData<T>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static, R: Writable<Target = Vec<T>>> Iterator for WritableValueIterator<T, R> {
|
impl<T: 'static, R: Writable<Target = Vec<T>>> Iterator for WritableValueIterator<R> {
|
||||||
type Item = R::Mut<T>;
|
type Item = R::Mut<T>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
|||||||
Reference in new issue
Block a user