pub struct Writable<T: 'static> { /* private fields */ }Expand description
A type-erased writable state that only exposes the value type T.
This abstraction allows components to accept state from any source without knowing
whether it comes from local state (use_state) or
global state (Freya Radio). It hides the implementation details, providing a
uniform interface for reading and writing values.
§Sources
Writable can be created from:
State<T>viaFromorIntoWritableRadioSliceMutviaIntoWritable
§Example
ⓘ
#[derive(PartialEq)]
struct NameInput {
name: Writable<String>,
}
impl Component for NameInput {
fn render(&self) -> impl IntoElement {
// The component doesn't care if this is local or global state
Input::new(self.name.clone())
}
}
fn app() -> impl IntoElement {
let local = use_state(|| "Alice".to_string());
let radio = use_radio(AppChannel::Name);
let slice = radio.slice_mut_current(|s| &mut s.name);
rect()
// Pass local state
.child(NameInput { name: local.into_writable() })
// Pass global radio slice
.child(NameInput { name: slice.into_writable() })
}Implementations§
Source§impl<T: 'static> Writable<T>
impl<T: 'static> Writable<T>
Sourcepub fn from_state(state: State<T>) -> Self
pub fn from_state(state: State<T>) -> Self
Create from local State<T>.
Sourcepub fn new(
peek_fn: Box<dyn Fn() -> ReadRef<'static, T>>,
write_fn: Box<dyn Fn() -> WriteRef<'static, T>>,
subscribe_fn: Box<dyn Fn()>,
notify_fn: Box<dyn Fn()>,
) -> Self
pub fn new( peek_fn: Box<dyn Fn() -> ReadRef<'static, T>>, write_fn: Box<dyn Fn() -> WriteRef<'static, T>>, subscribe_fn: Box<dyn Fn()>, notify_fn: Box<dyn Fn()>, ) -> Self
Create a new Writable with custom peek, write, subscribe, and notify functions.
pub fn write_if(&mut self, with: impl FnOnce(WriteRef<'static, T>) -> bool)
Trait Implementations§
Auto Trait Implementations§
impl<T> Freeze for Writable<T>
impl<T> !RefUnwindSafe for Writable<T>
impl<T> !Send for Writable<T>
impl<T> !Sync for Writable<T>
impl<T> Unpin for Writable<T>
impl<T> !UnwindSafe for Writable<T>
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
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> 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