pub trait Readable {
type Target: ?Sized;
type Storage: AnyStorage;
// Required methods
fn try_read_unchecked(
&self,
) -> Result<ReadableRef<'static, Self>, BorrowError>
where Self::Target: 'static;
fn try_peek_unchecked(
&self,
) -> Result<ReadableRef<'static, Self>, BorrowError>
where Self::Target: 'static;
fn subscribers(&self) -> Subscribers
where Self::Target: 'static;
}Expand description
A trait for states that can be read from like crate::Signal, crate::GlobalSignal, or crate::ReadSignal. 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.
§Example
fn double(something_readable: &impl Readable<Target = i32>) -> i32 {
something_readable.cloned() * 2
}
static COUNT: GlobalSignal<i32> = Signal::global(|| 0);
fn MyComponent(count: Signal<i32>) -> Element {
// Since we defined the function in terms of the readable trait, we can use it with any readable type (Signal, GlobalSignal, ReadSignal, etc)
let doubled = use_memo(move || double(&count));
let global_count_doubled = use_memo(|| double(&COUNT));
rsx! {
div {
"Count local: {count}"
"Doubled local: {doubled}"
"Count global: {COUNT}"
"Doubled global: {global_count_doubled}"
}
}
}Required Associated Types§
Sourcetype Storage: AnyStorage
type Storage: AnyStorage
The type of the storage this readable uses.
Required Methods§
Sourcefn try_read_unchecked(&self) -> Result<ReadableRef<'static, Self>, BorrowError>where
Self::Target: 'static,
fn try_read_unchecked(&self) -> Result<ReadableRef<'static, Self>, BorrowError>where
Self::Target: 'static,
Try to 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 try_peek_unchecked(&self) -> Result<ReadableRef<'static, Self>, BorrowError>where
Self::Target: 'static,
fn try_peek_unchecked(&self) -> Result<ReadableRef<'static, 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.
NOTE: This method is completely safe because borrow checking is done at runtime.
Sourcefn subscribers(&self) -> Subscriberswhere
Self::Target: 'static,
fn subscribers(&self) -> Subscriberswhere
Self::Target: 'static,
Get the underlying subscriber list for this readable. This is used to track when the value changes and notify subscribers.