pub struct Writable<T>where
T: 'static,{ /* private fields */ }Available on crate feature
radio only.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> Writable<T>where
T: 'static,
impl<T> Writable<T>where
T: 'static,
Sourcepub fn from_state(state: State<T>) -> Writable<T>
pub fn from_state(state: State<T>) -> Writable<T>
Create from local State<T>.
Sourcepub fn new(
peek_fn: Box<dyn Fn() -> <UnsyncStorage as AnyStorage>::Ref<'static, T>>,
write_fn: Box<dyn Fn() -> <UnsyncStorage as AnyStorage>::Mut<'static, T>>,
subscribe_fn: Box<dyn Fn()>,
notify_fn: Box<dyn Fn()>,
) -> Writable<T>
pub fn new( peek_fn: Box<dyn Fn() -> <UnsyncStorage as AnyStorage>::Ref<'static, T>>, write_fn: Box<dyn Fn() -> <UnsyncStorage as AnyStorage>::Mut<'static, T>>, subscribe_fn: Box<dyn Fn()>, notify_fn: Box<dyn Fn()>, ) -> Writable<T>
Create a new Writable with custom peek, write, subscribe, and notify functions.
Sourcepub fn read(&self) -> <UnsyncStorage as AnyStorage>::Ref<'static, T>
pub fn read(&self) -> <UnsyncStorage as AnyStorage>::Ref<'static, T>
Read the value and subscribe to changes.
Sourcepub fn peek(&self) -> <UnsyncStorage as AnyStorage>::Ref<'static, T>
pub fn peek(&self) -> <UnsyncStorage as AnyStorage>::Ref<'static, T>
Read the value without subscribing.
Sourcepub fn write(&mut self) -> <UnsyncStorage as AnyStorage>::Mut<'static, T>
pub fn write(&mut self) -> <UnsyncStorage as AnyStorage>::Mut<'static, T>
Write the value and notify subscribers.
pub fn write_if( &mut self, with: impl FnOnce(<UnsyncStorage as AnyStorage>::Mut<'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§
§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