pub struct RwSignal<T>where
T: 'static,{ /* private fields */ }
Expand description
A signal that combines the getter and setter into one value, rather than
separating them into a ReadSignal
and a WriteSignal
. You may prefer this
its style, or it may be easier to pass around in a context or as a function argument.
§Core Trait Implementations
.get()
clones the current value of the signal. If you call it within an effect, it will cause that effect to subscribe to the signal, and to re-run whenever the value of the signal changes..get_untracked()
clones the value of the signal without reactively tracking it.
.with()
allows you to reactively access the signal’s value without cloning by applying a callback function..with_untracked()
allows you to access the signal’s value without reactively tracking it.
.set()
sets the signal’s value, and notifies all subscribers that the signal’s value has changed. to subscribe to the signal, and to re-run whenever the value of the signal changes..set_untracked()
sets the signal’s value without notifying its subscribers.
.update()
mutates the signal’s value in place and notifies all subscribers that the signal’s value has changed..update_untracked()
mutates the signal’s value in place without notifying its subscribers.
.to_stream()
converts the signal to anasync
stream of values.
let count = create_rw_signal(0);
// ✅ set the value
count.set(1);
assert_eq!(count.get(), 1);
// ❌ you can call the getter within the setter
// count.set(count.get() + 1);
// ✅ however, it's more efficient to use .update() and mutate the value in place
count.update(|count: &mut i32| *count += 1);
assert_eq!(count.get(), 2);
Implementations§
source§impl<T> RwSignal<T>
impl<T> RwSignal<T>
sourcepub fn new(value: T) -> RwSignal<T>
pub fn new(value: T) -> RwSignal<T>
Creates a reactive signal with the getter and setter unified in one value. You may prefer this style, or it may be easier to pass around in a context or as a function argument.
This is identical to create_rw_signal
.
let count = RwSignal::new(0);
// ✅ set the value
count.set(1);
assert_eq!(count.get(), 1);
// ❌ you can call the getter within the setter
// count.set(count.get() + 1);
// ✅ however, it's more efficient to use .update() and mutate the value in place
count.update(|count: &mut i32| *count += 1);
assert_eq!(count.get(), 2);
sourcepub fn read_only(&self) -> ReadSignal<T>
pub fn read_only(&self) -> ReadSignal<T>
Returns a read-only handle to the signal.
Useful if you’re trying to give read access to another component but ensure that it can’t write to the signal and cause other parts of the DOM to update.
let count = create_rw_signal(0);
let read_count = count.read_only();
assert_eq!(count.get(), 0);
assert_eq!(read_count.get(), 0);
count.set(1);
assert_eq!(count.get(), 1);
assert_eq!(read_count.get(), 1);
sourcepub fn write_only(&self) -> WriteSignal<T>
pub fn write_only(&self) -> WriteSignal<T>
Returns a write-only handle to the signal.
Useful if you’re trying to give write access to another component, or split an
RwSignal
into a ReadSignal
and a WriteSignal
.
let count = create_rw_signal(0);
let set_count = count.write_only();
assert_eq!(count.get(), 0);
set_count.set(1);
assert_eq!(count.get(), 1);
sourcepub fn split(&self) -> (ReadSignal<T>, WriteSignal<T>)
pub fn split(&self) -> (ReadSignal<T>, WriteSignal<T>)
Splits an RwSignal
into its getter and setter.
let count = create_rw_signal(0);
let (get_count, set_count) = count.split();
assert_eq!(count.get(), 0);
assert_eq!(get_count.get(), 0);
set_count.set(1);
assert_eq!(count.get(), 1);
assert_eq!(get_count.get(), 1);
Trait Implementations§
source§impl<'de, T> Deserialize<'de> for RwSignal<T>where
T: Deserialize<'de>,
impl<'de, T> Deserialize<'de> for RwSignal<T>where
T: Deserialize<'de>,
source§fn deserialize<D>(
deserializer: D
) -> Result<RwSignal<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>(
deserializer: D
) -> Result<RwSignal<T>, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
source§impl<T> From<RwSignal<T>> for MaybeSignal<T>
impl<T> From<RwSignal<T>> for MaybeSignal<T>
source§fn from(value: RwSignal<T>) -> MaybeSignal<T>
fn from(value: RwSignal<T>) -> MaybeSignal<T>
source§impl<T> From<RwSignal<T>> for SignalSetter<T>
impl<T> From<RwSignal<T>> for SignalSetter<T>
source§fn from(value: RwSignal<T>) -> SignalSetter<T>
fn from(value: RwSignal<T>) -> SignalSetter<T>
source§impl<T> IntoAttribute for RwSignal<T>where
T: IntoAttribute + Clone,
impl<T> IntoAttribute for RwSignal<T>where
T: IntoAttribute + Clone,
source§impl<T> IntoProperty for RwSignal<T>
impl<T> IntoProperty for RwSignal<T>
source§impl<T> PartialEq for RwSignal<T>
impl<T> PartialEq for RwSignal<T>
source§impl<T> Serialize for RwSignal<T>where
T: Serialize,
impl<T> Serialize for RwSignal<T>where
T: Serialize,
source§fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
fn serialize<S>(
&self,
serializer: S
) -> Result<<S as Serializer>::Ok, <S as Serializer>::Error>where
S: Serializer,
source§impl<T> SignalDispose for RwSignal<T>
impl<T> SignalDispose for RwSignal<T>
source§impl<T> SignalGet for RwSignal<T>where
T: Clone,
impl<T> SignalGet for RwSignal<T>where
T: Clone,
§Examples
let count = create_rw_signal(0);
assert_eq!(count.get(), 0);
// count() is shorthand for count.get() on `nightly`
// assert_eq!(count(), 0);
source§impl<T> SignalGetUntracked for RwSignal<T>where
T: Clone,
impl<T> SignalGetUntracked for RwSignal<T>where
T: Clone,
source§fn get_untracked(&self) -> T
fn get_untracked(&self) -> T
source§fn try_get_untracked(&self) -> Option<T>
fn try_get_untracked(&self) -> Option<T>
Some(T)
] if the signal is still
valid, None
otherwise.source§impl<T> SignalSet for RwSignal<T>
impl<T> SignalSet for RwSignal<T>
§Examples
let count = create_rw_signal(0);
assert_eq!(count.get(), 0);
count.set(1);
assert_eq!(count.get(), 1);
source§impl<T> SignalSetUntracked<T> for RwSignal<T>
impl<T> SignalSetUntracked<T> for RwSignal<T>
source§fn set_untracked(&self, new_value: T)
fn set_untracked(&self, new_value: T)
source§fn try_set_untracked(&self, new_value: T) -> Option<T>
fn try_set_untracked(&self, new_value: T) -> Option<T>
None
if the signal was set, [Some(T)
] otherwise.source§impl<T> SignalStream<T> for RwSignal<T>where
T: Clone,
impl<T> SignalStream<T> for RwSignal<T>where
T: Clone,
source§impl<T> SignalUpdate for RwSignal<T>
impl<T> SignalUpdate for RwSignal<T>
§Examples
let count = create_rw_signal(0);
// notifies subscribers
count.update(|n| *n = 1); // it's easier just to call set_count.set(1), though!
assert_eq!(count.get(), 1);
// you can include arbitrary logic in this update function
// also notifies subscribers, even though the value hasn't changed
count.update(|n| {
if *n > 3 {
*n += 1
}
});
assert_eq!(count.get(), 1);
source§impl<T> SignalUpdateUntracked<T> for RwSignal<T>
impl<T> SignalUpdateUntracked<T> for RwSignal<T>
source§fn update_untracked(&self, f: impl FnOnce(&mut T))
fn update_untracked(&self, f: impl FnOnce(&mut T))
source§impl<T> SignalWith for RwSignal<T>
impl<T> SignalWith for RwSignal<T>
§Examples
let name = create_rw_signal("Alice".to_string());
// ❌ unnecessarily clones the string
let first_char = move || name.get().chars().next().unwrap();
assert_eq!(first_char(), 'A');
// ✅ gets the first char without cloning the `String`
let first_char = move || name.with(|n| n.chars().next().unwrap());
assert_eq!(first_char(), 'A');
name.set("Bob".to_string());
assert_eq!(first_char(), 'B');
source§impl<T> SignalWithUntracked for RwSignal<T>
impl<T> SignalWithUntracked for RwSignal<T>
impl<T> Copy for RwSignal<T>
impl<T> Eq for RwSignal<T>
Auto Trait Implementations§
impl<T> Freeze for RwSignal<T>
impl<T> RefUnwindSafe for RwSignal<T>where
T: RefUnwindSafe,
impl<T> Send for RwSignal<T>where
T: Send,
impl<T> Sync for RwSignal<T>where
T: Sync,
impl<T> Unpin for RwSignal<T>where
T: Unpin,
impl<T> UnwindSafe for RwSignal<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.source§impl<CustErr, T, Request> FromReq<Cbor, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<Cbor, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Request> FromReq<GetUrl, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<GetUrl, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Request> FromReq<Json, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<Json, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Request> FromReq<PostUrl, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<PostUrl, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Request> FromReq<Streaming, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<Streaming, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Request> FromReq<StreamingText, Request, CustErr> for T
impl<CustErr, T, Request> FromReq<StreamingText, Request, CustErr> for T
source§async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
async fn from_req(req: Request) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Response> FromRes<Cbor, Response, CustErr> for T
impl<CustErr, T, Response> FromRes<Cbor, Response, CustErr> for T
source§async fn from_res(res: Response) -> Result<T, ServerFnError<CustErr>>
async fn from_res(res: Response) -> Result<T, ServerFnError<CustErr>>
source§impl<CustErr, T, Response> FromRes<Json, Response, CustErr> for T
impl<CustErr, T, Response> FromRes<Json, Response, CustErr> for T
source§async fn from_res(res: Response) -> Result<T, ServerFnError<CustErr>>
async fn from_res(res: Response) -> Result<T, ServerFnError<CustErr>>
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more