pub trait ReadableExt: Readable {
// Provided methods
fn read(&self) -> ReadableRef<'_, Self>
where Self::Target: 'static { ... }
fn try_read(&self) -> Result<ReadableRef<'_, Self>, BorrowError>
where Self::Target: 'static { ... }
fn read_unchecked(&self) -> ReadableRef<'static, Self>
where Self::Target: 'static { ... }
fn peek(&self) -> ReadableRef<'_, Self>
where Self::Target: 'static { ... }
fn try_peek(&self) -> Result<ReadableRef<'_, Self>, BorrowError>
where Self::Target: 'static { ... }
fn peek_unchecked(&self) -> ReadableRef<'static, Self>
where Self::Target: 'static { ... }
fn map<F, O>(self, f: F) -> MappedSignal<O, Self, F>
where Self: Clone + Sized,
F: Fn(&Self::Target) -> &O { ... }
fn cloned(&self) -> Self::Target
where Self::Target: Clone + 'static { ... }
fn with<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> O
where Self::Target: 'static { ... }
fn with_peek<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> O
where Self::Target: 'static { ... }
fn index<I>(
&self,
index: I,
) -> ReadableRef<'_, Self, <Self::Target as Index<I>>::Output>
where Self::Target: Index<I> + 'static { ... }
}
Expand description
An extension trait for Readable
types that provides some convenience methods.
Provided Methods§
Sourcefn read(&self) -> ReadableRef<'_, Self>where
Self::Target: 'static,
fn read(&self) -> ReadableRef<'_, Self>where
Self::Target: 'static,
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. Calling this on a Signal is the same as using the signal() syntax to read and subscribe to its value
Sourcefn try_read(&self) -> Result<ReadableRef<'_, Self>, BorrowError>where
Self::Target: 'static,
fn try_read(&self) -> Result<ReadableRef<'_, Self>, BorrowError>where
Self::Target: 'static,
Try to get the current value of the state. If this is a signal, this will subscribe the current scope to the signal.
Sourcefn read_unchecked(&self) -> ReadableRef<'static, Self>where
Self::Target: 'static,
fn read_unchecked(&self) -> ReadableRef<'static, Self>where
Self::Target: 'static,
Get a reference to the value without checking the lifetime. This will subscribe the current scope to the signal.
NOTE: This method is completely safe because borrow checking is done at runtime.
Sourcefn peek(&self) -> ReadableRef<'_, Self>where
Self::Target: 'static,
fn peek(&self) -> ReadableRef<'_, Self>where
Self::Target: 'static,
Get the current value of the state without subscribing to updates. If the value has been dropped, this will panic.
§Example
fn MyComponent(mut count: Signal<i32>) -> Element {
let mut event_source = use_signal(|| None);
let doubled = use_memo(move || {
// We want to log the value of the event_source, but we don't need to rerun the doubled value if the event_source changes (because the value of doubled doesn't depend on the event_source)
// We can read the value with peek without subscribing to updates
let source = event_source.peek();
tracing::info!("Clicked: {source:?}");
count() * 2
});
rsx! {
div { "Count: {count}" }
div { "Doubled: {doubled}" }
button {
onclick: move |_| {
event_source.set(Some("Click me button"));
count += 1;
},
"Click me"
}
button {
onclick: move |_| {
event_source.set(Some("Double me button"));
count += 1;
},
"Double me"
}
}
}
Sourcefn try_peek(&self) -> Result<ReadableRef<'_, Self>, BorrowError>where
Self::Target: 'static,
fn try_peek(&self) -> Result<ReadableRef<'_, Self>, BorrowError>where
Self::Target: 'static,
Try to peek the current value of the signal without subscribing to updates. If the value has been dropped, this will return an error.
Sourcefn peek_unchecked(&self) -> ReadableRef<'static, Self>where
Self::Target: 'static,
fn peek_unchecked(&self) -> ReadableRef<'static, Self>where
Self::Target: 'static,
Get the current value of the signal without checking the lifetime. Unlike read, this will not subscribe the current scope to the signal which can cause parts of your UI to not update.
If the signal has been dropped, this will panic.
Sourcefn map<F, O>(self, f: F) -> MappedSignal<O, Self, F>
fn map<F, O>(self, f: F) -> MappedSignal<O, Self, F>
Map the references of the readable value to a new type. This lets you provide a view into the readable value without creating a new signal or cloning the value.
Anything that subscribes to the readable value will be rerun whenever the original value changes, even if the view does not
change. If you want to memorize the view, you can use a crate::Memo
instead. For fine grained scoped updates, use
stores instead
§Example
fn List(list: Signal<Vec<i32>>) -> Element {
rsx! {
for index in 0..list.len() {
// We can use the `map` method to provide a view into the single item in the list that the child component will render
Item { item: list.map(move |v| &v[index]) }
}
}
}
// The child component doesn't need to know that the mapped value is coming from a list
#[component]
fn Item(item: ReadSignal<i32>) -> Element {
rsx! {
div { "Item: {item}" }
}
}
Sourcefn cloned(&self) -> Self::Target
fn cloned(&self) -> Self::Target
Clone the inner value and return it. If the value has been dropped, this will panic.
Sourcefn with<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> Owhere
Self::Target: 'static,
fn with<O>(&self, f: impl FnOnce(&Self::Target) -> O) -> Owhere
Self::Target: 'static,
Run a function with a reference to the value. If the value has been dropped, this will panic.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.