pub struct Readable<T>where
T: 'static,{ /* private fields */ }Expand description
A type-erased readable state that only exposes the value type T.
This abstraction allows components to accept read-only state from any source without
knowing whether it comes from local state (use_state) or
global state (Freya Radio). Unlike Writable, this only
provides read access to the underlying value.
§Sources
Readable can be created from:
State<T>viaFromorIntoReadableRadioSliceviaIntoReadableWritable<T>viaFrom<Writable<T>>
§Example
ⓘ
#[derive(PartialEq)]
struct Counter {
count: Readable<i32>,
}
impl Component for Counter {
fn render(&self) -> impl IntoElement {
// The component only reads the value, never modifies it
format!("Count: {}", self.count.read())
}
}
fn app() -> impl IntoElement {
let local = use_state(|| 0);
let radio = use_radio(AppChannel::Count);
let slice = radio.slice_current(|s| &s.count);
rect()
// Pass local state as Readable
.child(Counter { count: local.into_readable() })
// Pass global radio slice as Readable
.child(Counter { count: slice.into_readable() })
}Implementations§
Source§impl<T> Readable<T>where
T: 'static,
impl<T> Readable<T>where
T: 'static,
Sourcepub fn from_state(state: State<T>) -> Readable<T>
pub fn from_state(state: State<T>) -> Readable<T>
Create from local State<T>.
Sourcepub fn from_value(value: T) -> Readable<T>
pub fn from_value(value: T) -> Readable<T>
Create from an owned value.
Sourcepub fn new(
read_fn: Box<dyn Fn() -> ReadableRef<T>>,
peek_fn: Box<dyn Fn() -> ReadableRef<T>>,
) -> Readable<T>
pub fn new( read_fn: Box<dyn Fn() -> ReadableRef<T>>, peek_fn: Box<dyn Fn() -> ReadableRef<T>>, ) -> Readable<T>
Create a new Readable with custom read and peek functions.
Sourcepub fn read(&self) -> ReadableRef<T>
pub fn read(&self) -> ReadableRef<T>
Read the value and subscribe to changes.
Sourcepub fn peek(&self) -> ReadableRef<T>
pub fn peek(&self) -> ReadableRef<T>
Read the value without subscribing.
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Readable<T>
impl<T> !RefUnwindSafe for Readable<T>
impl<T> !Send for Readable<T>
impl<T> !Sync for Readable<T>
impl<T> Unpin for Readable<T>
impl<T> !UnwindSafe for Readable<T>
Blanket Implementations§
§impl<T> AnyEq for T
impl<T> AnyEq for T
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
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> ComponentProps for T
impl<T> ComponentProps for T
fn changed(&self, other: &(dyn ComponentProps + 'static)) -> bool
§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Convert
Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can
then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Convert
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
Convert
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
Convert
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§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>
Converts
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>
Converts
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