Sync ReadSignal and Store (#4608)
* Add some traits read only signal works with sync storage * seal gats * minimize diff
This commit is contained in:
1 parent
022124e512
commit
da32278b3a
4 files changed
+304
-78
No files matched your search
@@ -1768,6 +1768,7 @@ fn looks_like_write_type(ty: &Type) -> bool {
|
||||
fn looks_like_store_type(ty: &Type) -> bool {
|
||||
last_segment_matches(ty, &parse_quote!(Store))
|
||||
|| last_segment_matches(ty, &parse_quote!(ReadStore))
|
||||
|| last_segment_matches(ty, &parse_quote!(WriteStore))
|
||||
}
|
||||
|
||||
fn looks_like_callback_type(ty: &Type) -> bool {
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
use dioxus::prelude::*;
|
||||
|
||||
fn main() {
|
||||
dioxus::launch(app);
|
||||
}
|
||||
|
||||
fn app() -> Element {
|
||||
let mut store = use_hook(|| Store::new_maybe_sync(0u32));
|
||||
|
||||
rsx! {
|
||||
button { onclick: move |_| store += 1, "Increase" }
|
||||
"{store}"
|
||||
Child { store }
|
||||
}
|
||||
}
|
||||
|
||||
#[component]
|
||||
fn Child(store: WriteStore<u32, SyncStorage>) -> Element {
|
||||
// A `WriteStore<T, SyncStorage>` can be sent to another thread!
|
||||
// Make sure to clean up your threads when the component unmounts.
|
||||
use_hook(|| {
|
||||
std::thread::spawn(move || loop {
|
||||
std::thread::sleep(std::time::Duration::from_secs(1));
|
||||
store += 1;
|
||||
|
||||
if store() >= 10 {
|
||||
break;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
rsx! {}
|
||||
}
|
||||
+219
-57
@@ -1,11 +1,12 @@
|
||||
use std::{any::Any, ops::Deref};
|
||||
|
||||
use dioxus_core::{IntoAttributeValue, IntoDynNode, Subscribers};
|
||||
use generational_box::{BorrowResult, UnsyncStorage};
|
||||
use generational_box::{BorrowResult, Storage, SyncStorage, UnsyncStorage};
|
||||
|
||||
use crate::{
|
||||
read_impls, write_impls, CopyValue, Global, InitializeFromFunction, MappedMutSignal,
|
||||
MappedSignal, Memo, Readable, ReadableExt, ReadableRef, Signal, Writable, WritableExt,
|
||||
MappedSignal, Memo, Readable, ReadableExt, ReadableRef, Signal, SignalData, Writable,
|
||||
WritableExt,
|
||||
};
|
||||
|
||||
/// A signal that can only be read from.
|
||||
@@ -13,18 +14,29 @@ use crate::{
|
||||
since = "0.7.0",
|
||||
note = "Use `ReadSignal` instead. Will be removed in 0.8"
|
||||
)]
|
||||
pub type ReadOnlySignal<T> = ReadSignal<T>;
|
||||
pub type ReadOnlySignal<T, S> = ReadSignal<T, S>;
|
||||
|
||||
/// A boxed version of [Readable] that can be used to store any readable type.
|
||||
pub struct ReadSignal<T: ?Sized> {
|
||||
value: CopyValue<Box<dyn Readable<Target = T, Storage = UnsyncStorage>>>,
|
||||
pub struct ReadSignal<T: ?Sized, S: BoxedSignalStorage<T> = UnsyncStorage> {
|
||||
value: CopyValue<Box<S::DynReadable<sealed::SealedToken>>, S>,
|
||||
}
|
||||
|
||||
impl<T: ?Sized + 'static> ReadSignal<T> {
|
||||
/// Create a new boxed readable value.
|
||||
pub fn new(value: impl Readable<Target = T, Storage = UnsyncStorage> + 'static) -> Self {
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + 'static, S: BoxedSignalStorage<T>> ReadSignal<T, S> {
|
||||
/// Create a new boxed readable value which may be sync
|
||||
pub fn new_maybe_sync<R>(value: R) -> Self
|
||||
where
|
||||
S: CreateBoxedSignalStorage<R>,
|
||||
R: Readable<Target = T>,
|
||||
{
|
||||
Self {
|
||||
value: CopyValue::new(Box::new(value)),
|
||||
value: CopyValue::new_maybe_sync(S::new_readable(value, sealed::SealedToken)),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,29 +68,33 @@ impl<T: ?Sized + 'static> ReadSignal<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Clone for ReadSignal<T> {
|
||||
impl<T: ?Sized, S: BoxedSignalStorage<T>> Clone for ReadSignal<T, S> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Copy for ReadSignal<T> {}
|
||||
impl<T: ?Sized, S: BoxedSignalStorage<T>> Copy for ReadSignal<T, S> {}
|
||||
|
||||
impl<T: ?Sized> PartialEq for ReadSignal<T> {
|
||||
impl<T: ?Sized, S: BoxedSignalStorage<T>> PartialEq for ReadSignal<T, S> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.value == other.value
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Default + 'static> Default for ReadSignal<T> {
|
||||
impl<
|
||||
T: Default + 'static,
|
||||
S: CreateBoxedSignalStorage<Signal<T, S>> + BoxedSignalStorage<T> + Storage<SignalData<T>>,
|
||||
> Default for ReadSignal<T, S>
|
||||
{
|
||||
fn default() -> Self {
|
||||
Self::new(Signal::new(T::default()))
|
||||
Self::new_maybe_sync(Signal::new_maybe_sync(T::default()))
|
||||
}
|
||||
}
|
||||
|
||||
read_impls!(ReadSignal<T>);
|
||||
read_impls!(ReadSignal<T, S: BoxedSignalStorage<T>>);
|
||||
|
||||
impl<T> IntoAttributeValue for ReadSignal<T>
|
||||
impl<T, S: BoxedSignalStorage<T>> IntoAttributeValue for ReadSignal<T, S>
|
||||
where
|
||||
T: Clone + IntoAttributeValue + 'static,
|
||||
{
|
||||
@@ -87,16 +103,17 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoDynNode for ReadSignal<T>
|
||||
impl<T, S> IntoDynNode for ReadSignal<T, S>
|
||||
where
|
||||
T: Clone + IntoDynNode + 'static,
|
||||
S: BoxedSignalStorage<T>,
|
||||
{
|
||||
fn into_dyn_node(self) -> dioxus_core::DynamicNode {
|
||||
self.with(|f| f.clone().into_dyn_node())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone + 'static> Deref for ReadSignal<T> {
|
||||
impl<T: Clone + 'static, S: BoxedSignalStorage<T>> Deref for ReadSignal<T, S> {
|
||||
type Target = dyn Fn() -> T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@@ -104,9 +121,9 @@ impl<T: Clone + 'static> Deref for ReadSignal<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Readable for ReadSignal<T> {
|
||||
impl<T: ?Sized, S: BoxedSignalStorage<T>> Readable for ReadSignal<T, S> {
|
||||
type Target = T;
|
||||
type Storage = UnsyncStorage;
|
||||
type Storage = S;
|
||||
|
||||
#[track_caller]
|
||||
fn try_read_unchecked(
|
||||
@@ -143,9 +160,13 @@ impl<T: ?Sized> Readable for ReadSignal<T> {
|
||||
// We can't implement From<impl Readable<Target = T, Storage = S> > for ReadSignal<T, S>
|
||||
// because it would conflict with the From<T> for T implementation, but we can implement it for
|
||||
// all specific readable types
|
||||
impl<T: 'static> From<Signal<T>> for ReadSignal<T> {
|
||||
fn from(value: Signal<T>) -> Self {
|
||||
Self::new(value)
|
||||
impl<
|
||||
T: 'static,
|
||||
S: CreateBoxedSignalStorage<Signal<T, S>> + BoxedSignalStorage<T> + Storage<SignalData<T>>,
|
||||
> From<Signal<T, S>> for ReadSignal<T, S>
|
||||
{
|
||||
fn from(value: Signal<T, S>) -> Self {
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
impl<T: PartialEq + 'static> From<Memo<T>> for ReadSignal<T> {
|
||||
@@ -153,9 +174,13 @@ impl<T: PartialEq + 'static> From<Memo<T>> for ReadSignal<T> {
|
||||
Self::new(value)
|
||||
}
|
||||
}
|
||||
impl<T: 'static> From<CopyValue<T>> for ReadSignal<T> {
|
||||
fn from(value: CopyValue<T>) -> Self {
|
||||
Self::new(value)
|
||||
impl<
|
||||
T: 'static,
|
||||
S: CreateBoxedSignalStorage<CopyValue<T, S>> + BoxedSignalStorage<T> + Storage<T>,
|
||||
> From<CopyValue<T, S>> for ReadSignal<T, S>
|
||||
{
|
||||
fn from(value: CopyValue<T, S>) -> Self {
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
impl<T, R> From<Global<T, R>> for ReadSignal<R>
|
||||
@@ -167,38 +192,41 @@ where
|
||||
Self::new(value)
|
||||
}
|
||||
}
|
||||
impl<V, O, F> From<MappedSignal<O, V, F>> for ReadSignal<O>
|
||||
impl<V, O, F, S> From<MappedSignal<O, V, F>> for ReadSignal<O, S>
|
||||
where
|
||||
O: ?Sized + 'static,
|
||||
V: Readable<Storage = UnsyncStorage> + 'static,
|
||||
V: Readable<Storage = S> + 'static,
|
||||
F: Fn(&V::Target) -> &O + 'static,
|
||||
S: BoxedSignalStorage<O> + CreateBoxedSignalStorage<MappedSignal<O, V, F>>,
|
||||
{
|
||||
fn from(value: MappedSignal<O, V, F>) -> Self {
|
||||
Self::new(value)
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
impl<V, O, F, FMut> From<MappedMutSignal<O, V, F, FMut>> for ReadSignal<O>
|
||||
impl<V, O, F, FMut, S> From<MappedMutSignal<O, V, F, FMut>> for ReadSignal<O, S>
|
||||
where
|
||||
O: ?Sized + 'static,
|
||||
V: Readable<Storage = UnsyncStorage> + 'static,
|
||||
V: Readable<Storage = S> + 'static,
|
||||
F: Fn(&V::Target) -> &O + 'static,
|
||||
FMut: 'static,
|
||||
S: BoxedSignalStorage<O> + CreateBoxedSignalStorage<MappedMutSignal<O, V, F, FMut>>,
|
||||
{
|
||||
fn from(value: MappedMutSignal<O, V, F, FMut>) -> Self {
|
||||
Self::new(value)
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
impl<T: ?Sized + 'static> From<WriteSignal<T>> for ReadSignal<T> {
|
||||
fn from(value: WriteSignal<T>) -> Self {
|
||||
Self::new(value)
|
||||
impl<T: ?Sized + 'static, S> From<WriteSignal<T, S>> for ReadSignal<T, S>
|
||||
where
|
||||
S: BoxedSignalStorage<T> + CreateBoxedSignalStorage<WriteSignal<T, S>>,
|
||||
{
|
||||
fn from(value: WriteSignal<T, S>) -> Self {
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// A boxed version of [Writable] that can be used to store any writable type.
|
||||
pub struct WriteSignal<T: ?Sized> {
|
||||
value: CopyValue<
|
||||
Box<dyn Writable<Target = T, Storage = UnsyncStorage, WriteMetadata = Box<dyn Any>>>,
|
||||
>,
|
||||
pub struct WriteSignal<T: ?Sized, S: BoxedSignalStorage<T> = UnsyncStorage> {
|
||||
value: CopyValue<Box<S::DynWritable<sealed::SealedToken>>, S>,
|
||||
}
|
||||
|
||||
impl<T: ?Sized + 'static> WriteSignal<T> {
|
||||
@@ -206,8 +234,19 @@ impl<T: ?Sized + 'static> WriteSignal<T> {
|
||||
pub fn new(
|
||||
value: impl Writable<Target = T, Storage = UnsyncStorage, WriteMetadata: 'static> + 'static,
|
||||
) -> Self {
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + 'static, S: BoxedSignalStorage<T>> WriteSignal<T, S> {
|
||||
/// Create a new boxed writable value which may be sync
|
||||
pub fn new_maybe_sync<R>(value: R) -> Self
|
||||
where
|
||||
R: Writable<Target = T, WriteMetadata: 'static>,
|
||||
S: CreateBoxedSignalStorage<R>,
|
||||
{
|
||||
Self {
|
||||
value: CopyValue::new(Box::new(BoxWriteMetadata::new(value))),
|
||||
value: CopyValue::new_maybe_sync(S::new_writable(value, sealed::SealedToken)),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -272,42 +311,44 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Clone for WriteSignal<T> {
|
||||
impl<T: ?Sized, S: BoxedSignalStorage<T>> Clone for WriteSignal<T, S> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Copy for WriteSignal<T> {}
|
||||
impl<T: ?Sized, S: BoxedSignalStorage<T>> Copy for WriteSignal<T, S> {}
|
||||
|
||||
impl<T: ?Sized> PartialEq for WriteSignal<T> {
|
||||
impl<T: ?Sized, S: BoxedSignalStorage<T>> PartialEq for WriteSignal<T, S> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.value == other.value
|
||||
}
|
||||
}
|
||||
|
||||
read_impls!(WriteSignal<T>);
|
||||
write_impls!(WriteSignal<T>);
|
||||
read_impls!(WriteSignal<T, S: BoxedSignalStorage<T>>);
|
||||
write_impls!(WriteSignal<T, S: BoxedSignalStorage<T>>);
|
||||
|
||||
impl<T> IntoAttributeValue for WriteSignal<T>
|
||||
impl<T, S> IntoAttributeValue for WriteSignal<T, S>
|
||||
where
|
||||
T: Clone + IntoAttributeValue + 'static,
|
||||
S: BoxedSignalStorage<T>,
|
||||
{
|
||||
fn into_value(self) -> dioxus_core::AttributeValue {
|
||||
self.with(|f| f.clone().into_value())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoDynNode for WriteSignal<T>
|
||||
impl<T, S> IntoDynNode for WriteSignal<T, S>
|
||||
where
|
||||
T: Clone + IntoDynNode + 'static,
|
||||
S: BoxedSignalStorage<T>,
|
||||
{
|
||||
fn into_dyn_node(self) -> dioxus_core::DynamicNode {
|
||||
self.with(|f| f.clone().into_dyn_node())
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Clone + 'static> Deref for WriteSignal<T> {
|
||||
impl<T: Clone + 'static, S: BoxedSignalStorage<T>> Deref for WriteSignal<T, S> {
|
||||
type Target = dyn Fn() -> T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@@ -315,9 +356,9 @@ impl<T: Clone + 'static> Deref for WriteSignal<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Readable for WriteSignal<T> {
|
||||
impl<T: ?Sized, S: BoxedSignalStorage<T>> Readable for WriteSignal<T, S> {
|
||||
type Target = T;
|
||||
type Storage = UnsyncStorage;
|
||||
type Storage = S;
|
||||
|
||||
#[track_caller]
|
||||
fn try_read_unchecked(
|
||||
@@ -351,7 +392,7 @@ impl<T: ?Sized> Readable for WriteSignal<T> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized> Writable for WriteSignal<T> {
|
||||
impl<T: ?Sized, S: BoxedSignalStorage<T>> Writable for WriteSignal<T, S> {
|
||||
type WriteMetadata = Box<dyn Any>;
|
||||
|
||||
fn try_write_unchecked(
|
||||
@@ -370,14 +411,22 @@ impl<T: ?Sized> Writable for WriteSignal<T> {
|
||||
// We can't implement From<impl Writable<Target = T, Storage = S>> for Write<T, S>
|
||||
// because it would conflict with the From<T> for T implementation, but we can implement it for
|
||||
// all specific readable types
|
||||
impl<T: 'static> From<Signal<T>> for WriteSignal<T> {
|
||||
fn from(value: Signal<T>) -> Self {
|
||||
Self::new(value)
|
||||
impl<
|
||||
T: 'static,
|
||||
S: CreateBoxedSignalStorage<Signal<T, S>> + BoxedSignalStorage<T> + Storage<SignalData<T>>,
|
||||
> From<Signal<T, S>> for WriteSignal<T, S>
|
||||
{
|
||||
fn from(value: Signal<T, S>) -> Self {
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
impl<T: 'static> From<CopyValue<T>> for WriteSignal<T> {
|
||||
fn from(value: CopyValue<T>) -> Self {
|
||||
Self::new(value)
|
||||
impl<
|
||||
T: 'static,
|
||||
S: CreateBoxedSignalStorage<CopyValue<T, S>> + BoxedSignalStorage<T> + Storage<T>,
|
||||
> From<CopyValue<T, S>> for WriteSignal<T, S>
|
||||
{
|
||||
fn from(value: CopyValue<T, S>) -> Self {
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
impl<T, R> From<Global<T, R>> for WriteSignal<R>
|
||||
@@ -389,14 +438,127 @@ where
|
||||
Self::new(value)
|
||||
}
|
||||
}
|
||||
impl<V, O, F, FMut> From<MappedMutSignal<O, V, F, FMut>> for WriteSignal<O>
|
||||
impl<V, O, F, FMut, S> From<MappedMutSignal<O, V, F, FMut>> for WriteSignal<O, S>
|
||||
where
|
||||
O: ?Sized + 'static,
|
||||
V: Writable<Storage = UnsyncStorage> + 'static,
|
||||
V: Writable<Storage = S> + 'static,
|
||||
F: Fn(&V::Target) -> &O + 'static,
|
||||
FMut: Fn(&mut V::Target) -> &mut O + 'static,
|
||||
S: CreateBoxedSignalStorage<MappedMutSignal<O, V, F, FMut>> + BoxedSignalStorage<O>,
|
||||
{
|
||||
fn from(value: MappedMutSignal<O, V, F, FMut>) -> Self {
|
||||
Self::new(value)
|
||||
Self::new_maybe_sync(value)
|
||||
}
|
||||
}
|
||||
|
||||
/// A trait for creating boxed readable and writable signals. This is implemented for
|
||||
/// [UnsyncStorage] and [SyncStorage].
|
||||
///
|
||||
/// You may need to add this trait as a bound when you use [ReadSignal] or [WriteSignal] while
|
||||
/// remaining generic over syncness.
|
||||
pub trait BoxedSignalStorage<T: ?Sized>:
|
||||
Storage<Box<Self::DynReadable<sealed::SealedToken>>>
|
||||
+ Storage<Box<Self::DynWritable<sealed::SealedToken>>>
|
||||
+ sealed::Sealed
|
||||
+ 'static
|
||||
{
|
||||
// This is not a public api, and is sealed to prevent external usage and implementations
|
||||
#[doc(hidden)]
|
||||
type DynReadable<Seal: sealed::SealedTokenTrait>: Readable<Target = T, Storage = Self> + ?Sized;
|
||||
// This is not a public api, and is sealed to prevent external usage and implementations
|
||||
#[doc(hidden)]
|
||||
type DynWritable<Seal: sealed::SealedTokenTrait>: Writable<Target = T, Storage = Self, WriteMetadata = Box<dyn Any>>
|
||||
+ ?Sized;
|
||||
}
|
||||
|
||||
/// A trait for creating boxed readable and writable signals. This is implemented for
|
||||
/// [UnsyncStorage] and [SyncStorage].
|
||||
///
|
||||
/// The storage type must implement `CreateReadOnlySignalStorage<T>` for every readable `T` type
|
||||
/// to be used with `ReadSignal` and `WriteSignal`.
|
||||
///
|
||||
/// You may need to add this trait as a bound when you call [ReadSignal::new_maybe_sync] or
|
||||
/// [WriteSignal::new_maybe_sync] while remaining generic over syncness.
|
||||
pub trait CreateBoxedSignalStorage<T: Readable + ?Sized>:
|
||||
BoxedSignalStorage<T::Target> + 'static
|
||||
{
|
||||
// This is not a public api, and is sealed to prevent external usage and implementations
|
||||
#[doc(hidden)]
|
||||
fn new_readable(
|
||||
value: T,
|
||||
_: sealed::SealedToken,
|
||||
) -> Box<Self::DynReadable<sealed::SealedToken>>
|
||||
where
|
||||
T: Sized;
|
||||
|
||||
// This is not a public api, and is sealed to prevent external usage and implementations
|
||||
#[doc(hidden)]
|
||||
fn new_writable(
|
||||
value: T,
|
||||
_: sealed::SealedToken,
|
||||
) -> Box<Self::DynWritable<sealed::SealedToken>>
|
||||
where
|
||||
T: Writable + Sized;
|
||||
}
|
||||
|
||||
impl<T: ?Sized + 'static> BoxedSignalStorage<T> for UnsyncStorage {
|
||||
type DynReadable<Seal: sealed::SealedTokenTrait> = dyn Readable<Target = T, Storage = Self>;
|
||||
type DynWritable<Seal: sealed::SealedTokenTrait> =
|
||||
dyn Writable<Target = T, Storage = Self, WriteMetadata = Box<dyn Any>>;
|
||||
}
|
||||
|
||||
impl<T: Readable<Storage = UnsyncStorage> + ?Sized + 'static> CreateBoxedSignalStorage<T>
|
||||
for UnsyncStorage
|
||||
{
|
||||
fn new_readable(value: T, _: sealed::SealedToken) -> Box<Self::DynReadable<sealed::SealedToken>>
|
||||
where
|
||||
T: Sized,
|
||||
{
|
||||
Box::new(value)
|
||||
}
|
||||
|
||||
fn new_writable(value: T, _: sealed::SealedToken) -> Box<Self::DynWritable<sealed::SealedToken>>
|
||||
where
|
||||
T: Writable + Sized,
|
||||
{
|
||||
Box::new(BoxWriteMetadata::new(value))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + 'static> BoxedSignalStorage<T> for SyncStorage {
|
||||
type DynReadable<Seal: sealed::SealedTokenTrait> =
|
||||
dyn Readable<Target = T, Storage = Self> + Send + Sync;
|
||||
type DynWritable<Seal: sealed::SealedTokenTrait> =
|
||||
dyn Writable<Target = T, Storage = Self, WriteMetadata = Box<dyn Any>> + Send + Sync;
|
||||
}
|
||||
|
||||
impl<T: Readable<Storage = SyncStorage> + Sync + Send + ?Sized + 'static>
|
||||
CreateBoxedSignalStorage<T> for SyncStorage
|
||||
{
|
||||
fn new_readable(value: T, _: sealed::SealedToken) -> Box<Self::DynReadable<sealed::SealedToken>>
|
||||
where
|
||||
T: Sized,
|
||||
{
|
||||
Box::new(value)
|
||||
}
|
||||
|
||||
fn new_writable(value: T, _: sealed::SealedToken) -> Box<Self::DynWritable<sealed::SealedToken>>
|
||||
where
|
||||
T: Writable + Sized,
|
||||
{
|
||||
Box::new(BoxWriteMetadata::new(value))
|
||||
}
|
||||
}
|
||||
|
||||
mod sealed {
|
||||
use generational_box::{SyncStorage, UnsyncStorage};
|
||||
|
||||
pub trait Sealed {}
|
||||
impl Sealed for UnsyncStorage {}
|
||||
impl Sealed for SyncStorage {}
|
||||
|
||||
pub struct SealedToken;
|
||||
|
||||
pub trait SealedTokenTrait {}
|
||||
impl SealedTokenTrait for SealedToken {}
|
||||
}
|
||||
@@ -6,9 +6,10 @@ use dioxus_core::{
|
||||
use_hook, AttributeValue, DynamicNode, IntoAttributeValue, IntoDynNode, Subscribers, SuperInto,
|
||||
};
|
||||
use dioxus_signals::{
|
||||
read_impls, write_impls, BorrowError, BorrowMutError, CopyValue, Global,
|
||||
InitializeFromFunction, MappedMutSignal, ReadSignal, Readable, ReadableExt, ReadableRef,
|
||||
Storage, UnsyncStorage, Writable, WritableExt, WritableRef, WriteSignal,
|
||||
read_impls, write_impls, BorrowError, BorrowMutError, BoxedSignalStorage, CopyValue,
|
||||
CreateBoxedSignalStorage, Global, InitializeFromFunction, MappedMutSignal, ReadSignal,
|
||||
Readable, ReadableExt, ReadableRef, Storage, UnsyncStorage, Writable, WritableExt, WritableRef,
|
||||
WriteSignal,
|
||||
};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
@@ -21,10 +22,10 @@ pub(crate) type MappedStore<
|
||||
> = Store<T, MappedMutSignal<T, Lens, F, FMut>>;
|
||||
|
||||
/// A type alias for a boxed read-only store.
|
||||
pub type ReadStore<T> = Store<T, ReadSignal<T>>;
|
||||
pub type ReadStore<T, S = UnsyncStorage> = Store<T, ReadSignal<T, S>>;
|
||||
|
||||
/// A type alias for a boxed writable-only store.
|
||||
pub type WriteStore<T> = Store<T, WriteSignal<T>>;
|
||||
pub type WriteStore<T, S = UnsyncStorage> = Store<T, WriteSignal<T, S>>;
|
||||
|
||||
/// Stores are a reactive type built for nested data structures. Each store will lazily create signals
|
||||
/// for each field/member of the data structure as needed.
|
||||
@@ -181,12 +182,13 @@ where
|
||||
}
|
||||
impl<T: ?Sized, Lens> Copy for Store<T, Lens> where Lens: Copy {}
|
||||
|
||||
impl<__F, __FMut, T: ?Sized, Lens> ::std::convert::From<MappedStore<T, Lens, __F, __FMut>>
|
||||
for Store<T, WriteSignal<T>>
|
||||
impl<__F, __FMut, T: ?Sized, S, Lens> ::std::convert::From<MappedStore<T, Lens, __F, __FMut>>
|
||||
for WriteStore<T, S>
|
||||
where
|
||||
Lens: Writable<Storage = UnsyncStorage> + 'static,
|
||||
Lens: Writable<Storage = S> + 'static,
|
||||
__F: Fn(&Lens::Target) -> &T + 'static,
|
||||
__FMut: Fn(&mut Lens::Target) -> &mut T + 'static,
|
||||
S: BoxedSignalStorage<T> + CreateBoxedSignalStorage<MappedMutSignal<T, Lens, __F, __FMut>>,
|
||||
T: 'static,
|
||||
{
|
||||
fn from(value: MappedStore<T, Lens, __F, __FMut>) -> Self {
|
||||
@@ -196,12 +198,13 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<__F, __FMut, T: ?Sized, Lens> ::std::convert::From<MappedStore<T, Lens, __F, __FMut>>
|
||||
for Store<T, ReadSignal<T>>
|
||||
impl<__F, __FMut, T: ?Sized, S, Lens> ::std::convert::From<MappedStore<T, Lens, __F, __FMut>>
|
||||
for ReadStore<T, S>
|
||||
where
|
||||
Lens: Writable<Storage = UnsyncStorage> + 'static,
|
||||
Lens: Writable<Storage = S> + 'static,
|
||||
__F: Fn(&Lens::Target) -> &T + 'static,
|
||||
__FMut: Fn(&mut Lens::Target) -> &mut T + 'static,
|
||||
S: BoxedSignalStorage<T> + CreateBoxedSignalStorage<MappedMutSignal<T, Lens, __F, __FMut>>,
|
||||
T: 'static,
|
||||
{
|
||||
fn from(value: MappedStore<T, Lens, __F, __FMut>) -> Self {
|
||||
@@ -211,11 +214,36 @@ where
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T> ::std::convert::From<Store<T>> for ReadStore<T>
|
||||
impl<T, S> ::std::convert::From<WriteStore<T, S>> for ReadStore<T, S>
|
||||
where
|
||||
T: ?Sized + 'static,
|
||||
S: BoxedSignalStorage<T> + CreateBoxedSignalStorage<WriteSignal<T, S>>,
|
||||
{
|
||||
fn from(value: Store<T>) -> Self {
|
||||
fn from(value: Store<T, WriteSignal<T, S>>) -> Self {
|
||||
Self {
|
||||
selector: value.selector.map_writer(::std::convert::Into::into),
|
||||
_phantom: ::std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T, S> ::std::convert::From<Store<T, CopyValue<T, S>>> for ReadStore<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: BoxedSignalStorage<T> + CreateBoxedSignalStorage<CopyValue<T, S>> + Storage<T>,
|
||||
{
|
||||
fn from(value: Store<T, CopyValue<T, S>>) -> Self {
|
||||
Self {
|
||||
selector: value.selector.map_writer(::std::convert::Into::into),
|
||||
_phantom: ::std::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<T, S> ::std::convert::From<Store<T, CopyValue<T, S>>> for WriteStore<T, S>
|
||||
where
|
||||
T: 'static,
|
||||
S: BoxedSignalStorage<T> + CreateBoxedSignalStorage<CopyValue<T, S>> + Storage<T>,
|
||||
{
|
||||
fn from(value: Store<T, CopyValue<T, S>>) -> Self {
|
||||
Self {
|
||||
selector: value.selector.map_writer(::std::convert::Into::into),
|
||||
_phantom: ::std::marker::PhantomData,
|
||||
@@ -225,25 +253,27 @@ where
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct SuperIntoReadSignalMarker;
|
||||
impl<T, Lens> SuperInto<ReadSignal<T>, SuperIntoReadSignalMarker> for Store<T, Lens>
|
||||
impl<T, S, Lens> SuperInto<ReadSignal<T, S>, SuperIntoReadSignalMarker> for Store<T, Lens>
|
||||
where
|
||||
T: ?Sized + 'static,
|
||||
Lens: Readable<Target = T, Storage = UnsyncStorage> + 'static,
|
||||
Lens: Readable<Target = T, Storage = S> + 'static,
|
||||
S: CreateBoxedSignalStorage<Store<T, Lens>> + BoxedSignalStorage<T>,
|
||||
{
|
||||
fn super_into(self) -> ReadSignal<T> {
|
||||
ReadSignal::new(self)
|
||||
fn super_into(self) -> ReadSignal<T, S> {
|
||||
ReadSignal::new_maybe_sync(self)
|
||||
}
|
||||
}
|
||||
|
||||
#[doc(hidden)]
|
||||
pub struct SuperIntoWriteSignalMarker;
|
||||
impl<T, Lens> SuperInto<WriteSignal<T>, SuperIntoWriteSignalMarker> for Store<T, Lens>
|
||||
impl<T, S, Lens> SuperInto<WriteSignal<T, S>, SuperIntoWriteSignalMarker> for Store<T, Lens>
|
||||
where
|
||||
T: ?Sized + 'static,
|
||||
Lens: Writable<Target = T, Storage = UnsyncStorage> + 'static,
|
||||
Lens: Writable<Target = T, Storage = S> + 'static,
|
||||
S: CreateBoxedSignalStorage<Store<T, Lens>> + BoxedSignalStorage<T>,
|
||||
{
|
||||
fn super_into(self) -> WriteSignal<T> {
|
||||
WriteSignal::new(self)
|
||||
fn super_into(self) -> WriteSignal<T, S> {
|
||||
WriteSignal::new_maybe_sync(self)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user