feat: Option<T> read-like traits & deprecate MaybeSignal (#3098)

This commit is contained in:
zakstucke 2024-10-25 20:41:26 +03:00 committed by GitHub
parent a437289f81
commit 396327b667
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 664 additions and 59 deletions

View file

@ -6,9 +6,7 @@ use leptos_axum::ResponseOptions;
// A basic function to display errors served by the error boundaries.
// Feel free to do more complicated things here than just displaying them.
#[component]
pub fn ErrorTemplate(
#[prop(into)] errors: MaybeSignal<Errors>,
) -> impl IntoView {
pub fn ErrorTemplate(#[prop(into)] errors: Signal<Errors>) -> impl IntoView {
// Get Errors from Signal
// Downcast lets us take a type that implements `std::error::Error`
let errors = Memo::new(move |_| {

View file

@ -10,7 +10,7 @@ struct Then {
// the type with Option<...> and marking the option as #[prop(optional)].
#[slot]
struct ElseIf {
cond: MaybeSignal<bool>,
cond: Signal<bool>,
children: ChildrenFn,
}
@ -22,7 +22,7 @@ struct Fallback {
// Slots are added to components like any other prop.
#[component]
fn SlotIf(
cond: MaybeSignal<bool>,
cond: Signal<bool>,
then: Then,
#[prop(default=vec![])] else_if: Vec<ElseIf>,
#[prop(optional)] fallback: Option<Fallback>,
@ -43,9 +43,9 @@ fn SlotIf(
#[component]
pub fn App() -> impl IntoView {
let (count, set_count) = signal(0);
let is_even = MaybeSignal::derive(move || count.get() % 2 == 0);
let is_div5 = MaybeSignal::derive(move || count.get() % 5 == 0);
let is_div7 = MaybeSignal::derive(move || count.get() % 7 == 0);
let is_even = Signal::derive(move || count.get() % 2 == 0);
let is_div5 = Signal::derive(move || count.get() % 5 == 0);
let is_div7 = Signal::derive(move || count.get() % 7 == 0);
view! {
<button on:click=move |_| set_count.update(|value| *value += 1)>"+1"</button>

View file

@ -38,7 +38,7 @@ pub fn TimerDemo() -> impl IntoView {
pub fn use_interval<T, F>(interval_millis: T, f: F)
where
F: Fn() + Clone + 'static,
T: Into<MaybeSignal<u64>> + 'static,
T: Into<Signal<u64>> + 'static,
{
let interval_millis = interval_millis.into();
Effect::new(move |prev_handle: Option<IntervalHandle>| {

View file

@ -84,6 +84,7 @@ pub mod owner;
#[cfg(feature = "serde")]
mod serde;
pub mod signal;
mod trait_options;
pub mod traits;
pub mod transition;
pub mod wrappers;

View file

@ -1,3 +1,5 @@
#[allow(deprecated)]
use crate::wrappers::read::{MaybeProp, MaybeSignal};
use crate::{
computed::{ArcMemo, Memo},
owner::Storage,
@ -7,7 +9,7 @@ use crate::{
},
traits::{Get, Set},
wrappers::{
read::{ArcSignal, MaybeProp, MaybeSignal, Signal},
read::{ArcSignal, Signal, SignalTypes},
write::SignalSetter,
},
};
@ -112,7 +114,8 @@ macro_rules! impl_get_fn_traits_get_arena {
($($ty:ident),*) => {
$(
#[cfg(feature = "nightly")]
impl<T, S> FnOnce<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> {
#[allow(deprecated)]
impl<T, S> FnOnce<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>> {
type Output = <Self as Get>::Value;
#[inline(always)]
@ -122,7 +125,8 @@ macro_rules! impl_get_fn_traits_get_arena {
}
#[cfg(feature = "nightly")]
impl<T, S> FnMut<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> {
#[allow(deprecated)]
impl<T, S> FnMut<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>> {
#[inline(always)]
extern "rust-call" fn call_mut(&mut self, _args: ()) -> Self::Output {
self.get()
@ -130,7 +134,8 @@ macro_rules! impl_get_fn_traits_get_arena {
}
#[cfg(feature = "nightly")]
impl<T, S> Fn<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> {
#[allow(deprecated)]
impl<T, S> Fn<()> for $ty<T, S> where $ty<T, S>: Get, S: Storage<T> + Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>> {
#[inline(always)]
extern "rust-call" fn call(&self, _args: ()) -> Self::Output {
self.get()

View file

@ -1,9 +1,11 @@
#[allow(deprecated)]
use crate::wrappers::read::{MaybeProp, MaybeSignal};
use crate::{
computed::{ArcMemo, Memo},
owner::Storage,
signal::{ArcReadSignal, ArcRwSignal, ReadSignal, RwSignal},
traits::With,
wrappers::read::{MaybeProp, MaybeSignal, Signal, SignalTypes},
wrappers::read::{Signal, SignalTypes},
};
use serde::{Deserialize, Serialize};
@ -73,6 +75,7 @@ impl<T: Serialize + 'static, St: Storage<T>> Serialize for ArcMemo<T, St> {
}
}
#[allow(deprecated)]
impl<T, St> Serialize for MaybeSignal<T, St>
where
T: Clone + Send + Sync + Serialize,
@ -86,6 +89,7 @@ where
}
}
#[allow(deprecated)]
impl<T, St> Serialize for MaybeProp<T, St>
where
T: Send + Sync + Serialize,
@ -96,15 +100,8 @@ where
S: serde::Serializer,
{
match &self.0 {
None | Some(MaybeSignal::Static(None)) => {
None::<T>.serialize(serializer)
}
Some(MaybeSignal::Static(Some(value))) => {
value.serialize(serializer)
}
Some(MaybeSignal::Dynamic(signal)) => {
signal.with(|value| value.serialize(serializer))
}
None => None::<T>.serialize(serializer),
Some(signal) => signal.with(|value| value.serialize(serializer)),
}
}
}
@ -146,6 +143,7 @@ impl<'de, T: Deserialize<'de>> Deserialize<'de> for ArcRwSignal<T> {
}
}
#[allow(deprecated)]
impl<'de, T: Deserialize<'de>, St> Deserialize<'de> for MaybeSignal<T, St>
where
St: Storage<T>,

View file

@ -0,0 +1,238 @@
use crate::{
traits::{
DefinedAt, Get, GetUntracked, Read, ReadUntracked, Track, With,
WithUntracked,
},
unwrap_signal,
};
use std::panic::Location;
impl<T> DefinedAt for Option<T>
where
T: DefinedAt,
{
fn defined_at(&self) -> Option<&'static Location<'static>> {
self.as_ref().map(DefinedAt::defined_at).unwrap_or(None)
}
}
impl<T> Track for Option<T>
where
T: Track,
{
fn track(&self) {
if let Some(signal) = self {
signal.track();
}
}
}
/// An alternative [`ReadUntracked`](crate) trait that works with `Option<Readable>` types.
pub trait ReadUntrackedOptional: Sized + DefinedAt {
/// The guard type that will be returned, which can be dereferenced to the value.
type Value;
/// Returns the guard, or `None` if the signal has already been disposed.
#[track_caller]
fn try_read_untracked(&self) -> Option<Self::Value>;
/// Returns the guard.
///
/// # Panics
/// Panics if you try to access a signal that has been disposed.
#[track_caller]
fn read_untracked(&self) -> Self::Value {
self.try_read_untracked()
.unwrap_or_else(unwrap_signal!(self))
}
}
impl<T> ReadUntrackedOptional for Option<T>
where
Self: DefinedAt,
T: ReadUntracked,
{
type Value = Option<<T as ReadUntracked>::Value>;
fn try_read_untracked(&self) -> Option<Self::Value> {
Some(if let Some(signal) = self {
Some(signal.try_read_untracked()?)
} else {
None
})
}
}
/// An alternative [`Read`](crate) trait that works with `Option<Readable>` types.
pub trait ReadOptional: DefinedAt {
/// The guard type that will be returned, which can be dereferenced to the value.
type Value;
/// Subscribes to the signal, and returns the guard, or `None` if the signal has already been disposed.
#[track_caller]
fn try_read(&self) -> Option<Self::Value>;
/// Subscribes to the signal, and returns the guard.
///
/// # Panics
/// Panics if you try to access a signal that has been disposed.
#[track_caller]
fn read(&self) -> Self::Value {
self.try_read().unwrap_or_else(unwrap_signal!(self))
}
}
impl<T> ReadOptional for Option<T>
where
Self: DefinedAt,
T: Read,
{
type Value = Option<<T as Read>::Value>;
fn try_read(&self) -> Option<Self::Value> {
Some(if let Some(readable) = self {
Some(readable.try_read()?)
} else {
None
})
}
}
/// An alternative [`WithUntracked`](crate) trait that works with `Option<Withable>` types.
pub trait WithUntrackedOptional: DefinedAt {
/// The type of the value contained in the signal.
type Value: ?Sized;
/// Applies the closure to the value, and returns the result,
/// or `None` if the signal has already been disposed.
#[track_caller]
fn try_with_untracked<U>(
&self,
fun: impl FnOnce(Option<&Self::Value>) -> U,
) -> Option<U>;
/// Applies the closure to the value, and returns the result.
///
/// # Panics
/// Panics if you try to access a signal that has been disposed.
#[track_caller]
fn with_untracked<U>(
&self,
fun: impl FnOnce(Option<&Self::Value>) -> U,
) -> U {
self.try_with_untracked(fun)
.unwrap_or_else(unwrap_signal!(self))
}
}
impl<T> WithUntrackedOptional for Option<T>
where
Self: DefinedAt,
T: WithUntracked,
<T as WithUntracked>::Value: Sized,
{
type Value = <T as WithUntracked>::Value;
fn try_with_untracked<U>(
&self,
fun: impl FnOnce(Option<&Self::Value>) -> U,
) -> Option<U> {
if let Some(signal) = self {
Some(signal.try_with_untracked(|val| fun(Some(val)))?)
} else {
Some(fun(None))
}
}
}
/// An alternative [`With`](crate) trait that works with `Option<Withable>` types.
pub trait WithOptional: DefinedAt {
/// The type of the value contained in the signal.
type Value: ?Sized;
/// Subscribes to the signal, applies the closure to the value, and returns the result,
/// or `None` if the signal has already been disposed.
#[track_caller]
fn try_with<U>(
&self,
fun: impl FnOnce(Option<&Self::Value>) -> U,
) -> Option<U>;
/// Subscribes to the signal, applies the closure to the value, and returns the result.
///
/// # Panics
/// Panics if you try to access a signal that has been disposed.
#[track_caller]
fn with<U>(&self, fun: impl FnOnce(Option<&Self::Value>) -> U) -> U {
self.try_with(fun).unwrap_or_else(unwrap_signal!(self))
}
}
impl<T> WithOptional for Option<T>
where
Self: DefinedAt,
T: With,
<T as With>::Value: Sized,
{
type Value = <T as With>::Value;
fn try_with<U>(
&self,
fun: impl FnOnce(Option<&Self::Value>) -> U,
) -> Option<U> {
if let Some(signal) = self {
Some(signal.try_with(|val| fun(Some(val)))?)
} else {
Some(fun(None))
}
}
}
impl<T> GetUntracked for Option<T>
where
Self: DefinedAt,
T: GetUntracked,
{
type Value = Option<<T as GetUntracked>::Value>;
fn try_get_untracked(&self) -> Option<Self::Value> {
Some(if let Some(signal) = self {
Some(signal.try_get_untracked()?)
} else {
None
})
}
}
impl<T> Get for Option<T>
where
Self: DefinedAt,
T: Get,
{
type Value = Option<<T as Get>::Value>;
fn try_get(&self) -> Option<Self::Value> {
Some(if let Some(signal) = self {
Some(signal.try_get()?)
} else {
None
})
}
}
/// Helper trait to implement flatten() on Option<&Option<T>>.
pub trait FlattenOptionRefOption {
/// The type of the value contained in the double option.
type Value;
/// Converts from `Option<&Option<T>>` to `Option<&T>`.
fn flatten(&self) -> Option<&Self::Value>;
}
impl<'a, T> FlattenOptionRefOption for Option<&'a Option<T>> {
type Value = T;
fn flatten(&self) -> Option<&'a T> {
self.map(Option::as_ref).flatten()
}
}

View file

@ -48,6 +48,7 @@
//! there isn't an `RwLock` so you can't wrap in a [`ReadGuard`](crate::signal::guards::ReadGuard),
//! but you can still implement [`WithUntracked`] and [`Track`], the same traits will still be implemented.
pub use crate::trait_options::*;
use crate::{
effect::Effect,
graph::{Observer, Source, Subscriber, ToAnySource},

View file

@ -708,6 +708,34 @@ pub mod read {
}
}
impl<T> From<ArcReadSignal<T>> for Signal<T>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: ArcReadSignal<T>) -> Self {
Self {
inner: ArenaItem::new(SignalTypes::ReadSignal(value)),
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller(),
}
}
}
impl<T> From<ArcReadSignal<T>> for Signal<T, LocalStorage>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: ArcReadSignal<T>) -> Self {
Self {
inner: ArenaItem::new_local(SignalTypes::ReadSignal(value)),
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller(),
}
}
}
impl<T> From<RwSignal<T>> for Signal<T>
where
T: Send + Sync + 'static,
@ -740,6 +768,38 @@ pub mod read {
}
}
impl<T> From<ArcRwSignal<T>> for Signal<T>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: ArcRwSignal<T>) -> Self {
Self {
inner: ArenaItem::new(SignalTypes::ReadSignal(
value.read_only(),
)),
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller(),
}
}
}
impl<T> From<ArcRwSignal<T>> for Signal<T, LocalStorage>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: ArcRwSignal<T>) -> Self {
Self {
inner: ArenaItem::new_local(SignalTypes::ReadSignal(
value.read_only(),
)),
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller(),
}
}
}
impl<T> From<Memo<T>> for Signal<T>
where
T: Send + Sync + 'static,
@ -768,6 +828,74 @@ pub mod read {
}
}
impl<T> From<ArcMemo<T>> for Signal<T>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: ArcMemo<T>) -> Self {
Self {
inner: ArenaItem::new(SignalTypes::Memo(value)),
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller(),
}
}
}
impl<T> From<ArcMemo<T, LocalStorage>> for Signal<T, LocalStorage>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: ArcMemo<T, LocalStorage>) -> Self {
Self {
inner: ArenaItem::new_local(SignalTypes::Memo(value)),
#[cfg(debug_assertions)]
defined_at: std::panic::Location::caller(),
}
}
}
impl<T> From<T> for Signal<Option<T>>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: T) -> Self {
Signal::stored(Some(value))
}
}
impl<T> From<T> for Signal<Option<T>, LocalStorage>
where
T: 'static,
{
#[track_caller]
fn from(value: T) -> Self {
Signal::stored_local(Some(value))
}
}
impl<T> From<Signal<T>> for Signal<Option<T>>
where
T: Clone + Send + Sync + 'static,
{
#[track_caller]
fn from(value: Signal<T>) -> Self {
Signal::derive(move || Some(value.get()))
}
}
impl<T> From<Signal<T, LocalStorage>> for Signal<Option<T>, LocalStorage>
where
T: Clone + 'static,
{
#[track_caller]
fn from(value: Signal<T, LocalStorage>) -> Self {
Signal::derive_local(move || Some(value.get()))
}
}
impl From<&str> for Signal<String> {
#[track_caller]
fn from(value: &str) -> Self {
@ -782,6 +910,147 @@ pub mod read {
}
}
impl From<&str> for Signal<Option<String>> {
#[track_caller]
fn from(value: &str) -> Self {
Signal::stored(Some(value.to_string()))
}
}
impl From<&str> for Signal<Option<String>, LocalStorage> {
#[track_caller]
fn from(value: &str) -> Self {
Signal::stored_local(Some(value.to_string()))
}
}
impl From<Signal<&'static str>> for Signal<String> {
#[track_caller]
fn from(value: Signal<&'static str>) -> Self {
Signal::derive(move || value.read().to_string())
}
}
impl From<Signal<&'static str>> for Signal<String, LocalStorage> {
#[track_caller]
fn from(value: Signal<&'static str>) -> Self {
Signal::derive_local(move || value.read().to_string())
}
}
impl From<Signal<&'static str>> for Signal<Option<String>> {
#[track_caller]
fn from(value: Signal<&'static str>) -> Self {
Signal::derive(move || Some(value.read().to_string()))
}
}
impl From<Signal<&'static str>> for Signal<Option<String>, LocalStorage> {
#[track_caller]
fn from(value: Signal<&'static str>) -> Self {
Signal::derive_local(move || Some(value.read().to_string()))
}
}
impl From<Signal<Option<&'static str>>> for Signal<Option<String>> {
#[track_caller]
fn from(value: Signal<Option<&'static str>>) -> Self {
Signal::derive(move || value.read().map(str::to_string))
}
}
impl From<Signal<Option<&'static str>>>
for Signal<Option<String>, LocalStorage>
{
#[track_caller]
fn from(value: Signal<Option<&'static str>>) -> Self {
Signal::derive_local(move || value.read().map(str::to_string))
}
}
#[allow(deprecated)]
impl<T> From<MaybeSignal<T>> for Signal<T>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: MaybeSignal<T>) -> Self {
match value {
MaybeSignal::Static(value) => Signal::stored(value),
MaybeSignal::Dynamic(signal) => signal,
}
}
}
#[allow(deprecated)]
impl<T> From<MaybeSignal<T, LocalStorage>> for Signal<T, LocalStorage>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: MaybeSignal<T, LocalStorage>) -> Self {
match value {
MaybeSignal::Static(value) => Signal::stored_local(value),
MaybeSignal::Dynamic(signal) => signal,
}
}
}
#[allow(deprecated)]
impl<T> From<MaybeSignal<T>> for Signal<Option<T>>
where
T: Clone + Send + Sync + 'static,
{
#[track_caller]
fn from(value: MaybeSignal<T>) -> Self {
match value {
MaybeSignal::Static(value) => Signal::stored(Some(value)),
MaybeSignal::Dynamic(signal) => {
Signal::derive(move || Some(signal.get()))
}
}
}
}
#[allow(deprecated)]
impl<T> From<MaybeSignal<T, LocalStorage>> for Signal<Option<T>, LocalStorage>
where
T: Clone + Send + Sync + 'static,
{
#[track_caller]
fn from(value: MaybeSignal<T, LocalStorage>) -> Self {
match value {
MaybeSignal::Static(value) => Signal::stored_local(Some(value)),
MaybeSignal::Dynamic(signal) => {
Signal::derive_local(move || Some(signal.get()))
}
}
}
}
#[allow(deprecated)]
impl<T> From<MaybeProp<T>> for Option<Signal<Option<T>>>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: MaybeProp<T>) -> Self {
value.0
}
}
#[allow(deprecated)]
impl<T> From<MaybeProp<T, LocalStorage>>
for Option<Signal<Option<T>, LocalStorage>>
where
T: Send + Sync + 'static,
{
#[track_caller]
fn from(value: MaybeProp<T, LocalStorage>) -> Self {
value.0
}
}
/// A wrapper for a value that is *either* `T` or [`Signal<T>`].
///
/// This allows you to create APIs that take either a reactive or a non-reactive value
@ -810,6 +1079,12 @@ pub mod read {
/// assert_eq!(above_3(&memoized_double_count.into()), true);
/// ```
#[derive(Debug, PartialEq, Eq)]
#[deprecated(
since = "0.7.0-rc0",
note = "`MaybeSignal<T>` is deprecated in favour of `Signal<T>` which \
is `Copy`, now has a more efficient From<T> implementation \
and other benefits in 0.7."
)]
pub enum MaybeSignal<T, S = SyncStorage>
where
T: 'static,
@ -821,6 +1096,7 @@ pub mod read {
Dynamic(Signal<T, S>),
}
#[allow(deprecated)]
impl<T: Clone, S> Clone for MaybeSignal<T, S>
where
S: Storage<T>,
@ -833,8 +1109,10 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T: Copy, S> Copy for MaybeSignal<T, S> where S: Storage<T> {}
#[allow(deprecated)]
impl<T: Default, S> Default for MaybeSignal<T, S>
where
S: Storage<T>,
@ -844,6 +1122,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T, S> DefinedAt for MaybeSignal<T, S>
where
S: Storage<T>,
@ -855,6 +1134,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T, S> Track for MaybeSignal<T, S>
where
S: Storage<T> + Storage<SignalTypes<T, S>>,
@ -867,6 +1147,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T, S> ReadUntracked for MaybeSignal<T, S>
where
T: Clone,
@ -891,6 +1172,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> MaybeSignal<T>
where
T: Send + Sync,
@ -904,6 +1186,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> MaybeSignal<T, LocalStorage> {
/// Wraps a derived signal, i.e., any computation that accesses one or more
/// reactive signals.
@ -912,6 +1195,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<T> for MaybeSignal<T, SyncStorage>
where
SyncStorage: Storage<T>,
@ -921,6 +1205,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> FromLocal<T> for MaybeSignal<T, LocalStorage>
where
LocalStorage: Storage<T>,
@ -930,6 +1215,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<ReadSignal<T>> for MaybeSignal<T>
where
T: Send + Sync,
@ -939,12 +1225,14 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<ReadSignal<T, LocalStorage>> for MaybeSignal<T, LocalStorage> {
fn from(value: ReadSignal<T, LocalStorage>) -> Self {
Self::Dynamic(value.into())
}
}
#[allow(deprecated)]
impl<T> From<RwSignal<T>> for MaybeSignal<T>
where
T: Send + Sync,
@ -954,12 +1242,14 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<RwSignal<T, LocalStorage>> for MaybeSignal<T, LocalStorage> {
fn from(value: RwSignal<T, LocalStorage>) -> Self {
Self::Dynamic(value.into())
}
}
#[allow(deprecated)]
impl<T> From<Memo<T>> for MaybeSignal<T>
where
T: Send + Sync,
@ -969,12 +1259,14 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<Memo<T, LocalStorage>> for MaybeSignal<T, LocalStorage> {
fn from(value: Memo<T, LocalStorage>) -> Self {
Self::Dynamic(value.into())
}
}
#[allow(deprecated)]
impl<T> From<ArcReadSignal<T>> for MaybeSignal<T>
where
T: Send + Sync,
@ -984,12 +1276,14 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> FromLocal<ArcReadSignal<T>> for MaybeSignal<T, LocalStorage> {
fn from_local(value: ArcReadSignal<T>) -> Self {
ReadSignal::from_local(value).into()
}
}
#[allow(deprecated)]
impl<T> From<ArcRwSignal<T>> for MaybeSignal<T>
where
T: Send + Sync + 'static,
@ -999,6 +1293,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> FromLocal<ArcRwSignal<T>> for MaybeSignal<T, LocalStorage>
where
T: 'static,
@ -1008,6 +1303,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<ArcMemo<T>> for MaybeSignal<T>
where
T: Send + Sync,
@ -1017,12 +1313,14 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> FromLocal<ArcMemo<T, LocalStorage>> for MaybeSignal<T, LocalStorage> {
fn from_local(value: ArcMemo<T, LocalStorage>) -> Self {
Memo::from_local(value).into()
}
}
#[allow(deprecated)]
impl<T, S> From<Signal<T, S>> for MaybeSignal<T, S>
where
S: Storage<T>,
@ -1032,6 +1330,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<S> From<&str> for MaybeSignal<String, S>
where
S: Storage<String> + Storage<Arc<RwLock<String>>>,
@ -1073,35 +1372,41 @@ pub mod read {
/// assert_eq!(above_3(&memoized_double_count.into()), true);
/// ```
#[derive(Debug, PartialEq, Eq)]
#[allow(deprecated)]
pub struct MaybeProp<T: 'static, S = SyncStorage>(
pub(crate) Option<MaybeSignal<Option<T>, S>>,
pub(crate) Option<Signal<Option<T>, S>>,
)
where
S: Storage<Option<T>>;
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>;
impl<T: Clone, S> Clone for MaybeProp<T, S>
impl<T, S> Clone for MaybeProp<T, S>
where
S: Storage<Option<T>>,
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,
{
fn clone(&self) -> Self {
Self(self.0.clone())
*self
}
}
impl<T: Copy, S> Copy for MaybeProp<T, S> where S: Storage<Option<T>> {}
impl<T, S> Copy for MaybeProp<T, S> where
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>
{
}
#[allow(deprecated)]
impl<T, S> Default for MaybeProp<T, S>
where
S: Storage<Option<T>>,
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,
{
fn default() -> Self {
Self(None)
}
}
#[allow(deprecated)]
impl<T, S> DefinedAt for MaybeProp<T, S>
where
S: Storage<Option<T>>,
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,
{
fn defined_at(&self) -> Option<&'static Location<'static>> {
// TODO this can be improved by adding a defined_at field
@ -1109,6 +1414,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T, S> Track for MaybeProp<T, S>
where
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,
@ -1121,10 +1427,11 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T, S> ReadUntracked for MaybeProp<T, S>
where
T: Clone,
S: Storage<SignalTypes<Option<T>, S>> + Storage<Option<T>>,
S: Storage<Option<T>> + Storage<SignalTypes<Option<T>, S>>,
{
type Value = ReadGuard<Option<T>, SignalReadGuard<Option<T>, S>>;
@ -1143,6 +1450,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> MaybeProp<T>
where
T: Send + Sync,
@ -1152,46 +1460,55 @@ pub mod read {
pub fn derive(
derived_signal: impl Fn() -> Option<T> + Send + Sync + 'static,
) -> Self {
Self(Some(MaybeSignal::derive(derived_signal)))
Self(Some(Signal::derive(derived_signal)))
}
}
#[allow(deprecated)]
impl<T> From<T> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: T) -> Self {
Self(Some(MaybeSignal::from(Some(value))))
Self(Some(Signal::stored(Some(value))))
}
}
#[allow(deprecated)]
impl<T> From<Option<T>> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: Option<T>) -> Self {
Self(Some(MaybeSignal::from(value)))
Self(Some(Signal::stored(value)))
}
}
#[allow(deprecated)]
impl<T> From<MaybeSignal<Option<T>>> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: MaybeSignal<Option<T>>) -> Self {
Self(Some(value))
Self(Some(value.into()))
}
}
#[allow(deprecated)]
impl<T> From<Option<MaybeSignal<Option<T>>>> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: Option<MaybeSignal<Option<T>>>) -> Self {
Self(value)
Self(value.map(Into::into))
}
}
#[allow(deprecated)]
impl<T> From<ReadSignal<Option<T>>> for MaybeProp<T>
where
T: Send + Sync,
@ -1201,6 +1518,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<RwSignal<Option<T>>> for MaybeProp<T>
where
T: Send + Sync,
@ -1210,6 +1528,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<Memo<Option<T>>> for MaybeProp<T>
where
T: Send + Sync,
@ -1219,95 +1538,112 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<Signal<Option<T>>> for MaybeProp<T>
where
T: Send + Sync,
SyncStorage: Storage<Option<T>>,
{
fn from(value: Signal<Option<T>>) -> Self {
Self(Some(value.into()))
Self(Some(value))
}
}
#[allow(deprecated)]
impl<T> From<ReadSignal<T>> for MaybeProp<T>
where
T: Send + Sync + Clone,
{
fn from(value: ReadSignal<T>) -> Self {
Self(Some(MaybeSignal::derive(move || Some(value.get()))))
Self(Some(Signal::derive(move || Some(value.get()))))
}
}
#[allow(deprecated)]
impl<T> From<RwSignal<T>> for MaybeProp<T>
where
T: Send + Sync + Clone,
{
fn from(value: RwSignal<T>) -> Self {
Self(Some(MaybeSignal::derive(move || Some(value.get()))))
Self(Some(Signal::derive(move || Some(value.get()))))
}
}
#[allow(deprecated)]
impl<T> From<Memo<T>> for MaybeProp<T>
where
T: Send + Sync + Clone,
{
fn from(value: Memo<T>) -> Self {
Self(Some(MaybeSignal::derive(move || Some(value.get()))))
Self(Some(Signal::derive(move || Some(value.get()))))
}
}
#[allow(deprecated)]
impl<T> From<Signal<T>> for MaybeProp<T>
where
T: Send + Sync + Clone,
{
fn from(value: Signal<T>) -> Self {
Self(Some(MaybeSignal::derive(move || Some(value.get()))))
Self(Some(Signal::derive(move || Some(value.get()))))
}
}
#[allow(deprecated)]
impl From<&str> for MaybeProp<String> {
fn from(value: &str) -> Self {
Self(Some(MaybeSignal::from(Some(value.to_string()))))
Self(Some(Signal::from(Some(value.to_string()))))
}
}
#[allow(deprecated)]
impl<T> MaybeProp<T, LocalStorage> {
/// Wraps a derived signal, i.e., any computation that accesses one or more
/// reactive signals.
pub fn derive_local(
derived_signal: impl Fn() -> Option<T> + 'static,
) -> Self {
Self(Some(MaybeSignal::derive_local(derived_signal)))
Self(Some(Signal::derive_local(derived_signal)))
}
}
#[allow(deprecated)]
impl<T> FromLocal<T> for MaybeProp<T, LocalStorage> {
fn from_local(value: T) -> Self {
Self(Some(MaybeSignal::from_local(Some(value))))
Self(Some(Signal::stored_local(Some(value))))
}
}
#[allow(deprecated)]
impl<T> FromLocal<Option<T>> for MaybeProp<T, LocalStorage> {
fn from_local(value: Option<T>) -> Self {
Self(Some(MaybeSignal::from_local(value)))
Self(Some(Signal::stored_local(value)))
}
}
#[allow(deprecated)]
impl<T> From<MaybeSignal<Option<T>, LocalStorage>>
for MaybeProp<T, LocalStorage>
where
T: Send + Sync,
{
fn from(value: MaybeSignal<Option<T>, LocalStorage>) -> Self {
Self(Some(value))
Self(Some(value.into()))
}
}
#[allow(deprecated)]
impl<T> From<Option<MaybeSignal<Option<T>, LocalStorage>>>
for MaybeProp<T, LocalStorage>
where
T: Send + Sync,
{
fn from(value: Option<MaybeSignal<Option<T>, LocalStorage>>) -> Self {
Self(value)
Self(value.map(Into::into))
}
}
#[allow(deprecated)]
impl<T> From<ReadSignal<Option<T>, LocalStorage>> for MaybeProp<T, LocalStorage>
where
T: Send + Sync,
@ -1317,6 +1653,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<RwSignal<Option<T>, LocalStorage>> for MaybeProp<T, LocalStorage>
where
T: Send + Sync,
@ -1326,6 +1663,7 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<Memo<Option<T>, LocalStorage>> for MaybeProp<T, LocalStorage>
where
T: Send + Sync,
@ -1335,51 +1673,57 @@ pub mod read {
}
}
#[allow(deprecated)]
impl<T> From<Signal<Option<T>, LocalStorage>> for MaybeProp<T, LocalStorage> {
fn from(value: Signal<Option<T>, LocalStorage>) -> Self {
Self(Some(value.into()))
Self(Some(value))
}
}
#[allow(deprecated)]
impl<T> From<ReadSignal<T, LocalStorage>> for MaybeProp<T, LocalStorage>
where
T: Send + Sync + Clone,
{
fn from(value: ReadSignal<T, LocalStorage>) -> Self {
Self(Some(MaybeSignal::derive_local(move || Some(value.get()))))
Self(Some(Signal::derive_local(move || Some(value.get()))))
}
}
#[allow(deprecated)]
impl<T> From<RwSignal<T, LocalStorage>> for MaybeProp<T, LocalStorage>
where
T: Send + Sync + Clone,
{
fn from(value: RwSignal<T, LocalStorage>) -> Self {
Self(Some(MaybeSignal::derive_local(move || Some(value.get()))))
Self(Some(Signal::derive_local(move || Some(value.get()))))
}
}
#[allow(deprecated)]
impl<T> From<Memo<T, LocalStorage>> for MaybeProp<T, LocalStorage>
where
T: Send + Sync + Clone,
{
fn from(value: Memo<T, LocalStorage>) -> Self {
Self(Some(MaybeSignal::derive_local(move || Some(value.get()))))
Self(Some(Signal::derive_local(move || Some(value.get()))))
}
}
#[allow(deprecated)]
impl<T> From<Signal<T, LocalStorage>> for MaybeProp<T, LocalStorage>
where
T: Send + Sync + Clone,
{
fn from(value: Signal<T, LocalStorage>) -> Self {
Self(Some(MaybeSignal::derive_local(move || Some(value.get()))))
Self(Some(Signal::derive_local(move || Some(value.get()))))
}
}
#[allow(deprecated)]
impl From<&str> for MaybeProp<String, LocalStorage> {
fn from(value: &str) -> Self {
Self(Some(MaybeSignal::from_local(Some(value.to_string()))))
Self(Some(Signal::stored_local(Some(value.to_string()))))
}
}

View file

@ -452,6 +452,7 @@ where
mod stable {
macro_rules! class_signal_arena {
($sig:ident) => {
#[allow(deprecated)]
impl<C, S> IntoClass for $sig<C, S>
where
$sig<C, S>: Get<Value = C>,
@ -507,6 +508,7 @@ mod stable {
}
}
#[allow(deprecated)]
impl<S> IntoClass for (&'static str, $sig<bool, S>)
where
$sig<bool, S>: Get<Value = bool>,
@ -686,12 +688,14 @@ mod stable {
use super::RenderEffect;
use crate::html::class::IntoClass;
#[allow(deprecated)]
use reactive_graph::wrappers::read::MaybeSignal;
use reactive_graph::{
computed::{ArcMemo, Memo},
owner::Storage,
signal::{ArcReadSignal, ArcRwSignal, ReadSignal, RwSignal},
traits::Get,
wrappers::read::{ArcSignal, MaybeSignal, Signal},
wrappers::read::{ArcSignal, Signal},
};
class_signal_arena!(RwSignal);

View file

@ -89,13 +89,15 @@ where
#[cfg(not(feature = "nightly"))]
mod stable {
use crate::html::element::InnerHtmlValue;
#[allow(deprecated)]
use reactive_graph::wrappers::read::MaybeSignal;
use reactive_graph::{
computed::{ArcMemo, Memo},
effect::RenderEffect,
owner::Storage,
signal::{ArcReadSignal, ArcRwSignal, ReadSignal, RwSignal},
traits::Get,
wrappers::read::{ArcSignal, MaybeSignal, Signal},
wrappers::read::{ArcSignal, Signal},
};
macro_rules! inner_html_signal {
@ -159,6 +161,7 @@ mod stable {
macro_rules! inner_html_signal_arena {
($sig:ident) => {
#[allow(deprecated)]
impl<V, S> InnerHtmlValue for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,

View file

@ -507,13 +507,15 @@ mod stable {
RenderHtml,
},
};
#[allow(deprecated)]
use reactive_graph::wrappers::read::MaybeSignal;
use reactive_graph::{
computed::{ArcMemo, Memo},
effect::RenderEffect,
owner::Storage,
signal::{ArcReadSignal, ArcRwSignal, ReadSignal, RwSignal},
traits::Get,
wrappers::read::{ArcSignal, MaybeSignal, Signal},
wrappers::read::{ArcSignal, Signal},
};
macro_rules! signal_impl {
@ -683,6 +685,7 @@ mod stable {
macro_rules! signal_impl_arena {
($sig:ident $dry_resolve:literal) => {
#[allow(deprecated)]
impl<V, S> Render for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,
@ -707,6 +710,7 @@ mod stable {
}
}
#[allow(deprecated)]
impl<V, S> AddAnyAttr for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,
@ -728,6 +732,7 @@ mod stable {
}
}
#[allow(deprecated)]
impl<V, S> RenderHtml for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,
@ -793,6 +798,7 @@ mod stable {
}
}
#[allow(deprecated)]
impl<V, S> AttributeValue for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,

View file

@ -82,13 +82,15 @@ where
#[cfg(not(feature = "nightly"))]
mod stable {
use crate::html::property::IntoProperty;
#[allow(deprecated)]
use reactive_graph::wrappers::read::MaybeSignal;
use reactive_graph::{
computed::{ArcMemo, Memo},
effect::RenderEffect,
owner::Storage,
signal::{ArcReadSignal, ArcRwSignal, ReadSignal, RwSignal},
traits::Get,
wrappers::read::{ArcSignal, MaybeSignal, Signal},
wrappers::read::{ArcSignal, Signal},
};
macro_rules! property_signal {
@ -136,6 +138,7 @@ mod stable {
macro_rules! property_signal_arena {
($sig:ident) => {
#[allow(deprecated)]
impl<V, S> IntoProperty for $sig<V, S>
where
$sig<V, S>: Get<Value = V>,

View file

@ -393,6 +393,7 @@ mod stable {
macro_rules! style_signal_arena {
($sig:ident) => {
#[allow(deprecated)]
impl<C, S> IntoStyle for $sig<C, S>
where
$sig<C, S>: Get<Value = C>,
@ -458,6 +459,7 @@ mod stable {
}
}
#[allow(deprecated)]
impl<S, St> IntoStyle for (&'static str, $sig<S, St>)
where
$sig<S, St>: Get<Value = S>,
@ -534,12 +536,14 @@ mod stable {
use super::RenderEffect;
use crate::html::style::IntoStyle;
#[allow(deprecated)]
use reactive_graph::wrappers::read::MaybeSignal;
use reactive_graph::{
computed::{ArcMemo, Memo},
owner::Storage,
signal::{ArcReadSignal, ArcRwSignal, ReadSignal, RwSignal},
traits::Get,
wrappers::read::{ArcSignal, MaybeSignal, Signal},
wrappers::read::{ArcSignal, Signal},
};
use std::borrow::Cow;