pub enum Val<S: State, T: 'static> {
State {
factory: Box<dyn Fn(&S) -> T>,
value: Option<T>,
},
Val(T),
}Expand description
A value that’s either dependent on the state or independent.
Use Val::new_state or the val!() macro from the maycoon-macros crate to create a new state dependent value.
Use Val::new_val or just YourValue.into() to create an independent value. You can also use val!(), because why not?
NOTE for Widget Developers: Inside the Widget::update method, all values must be invalidated using Val::invalidate.
Variants§
State
A state dependent value.
The inner value may need to be re-computed, when invalid.
Fields
Val(T)
An independent value.
The inner value will always be the same and does not need any re-computation.
More performant than state dependent values.
Implementations§
Source§impl<S: State, T: 'static> Val<S, T>
impl<S: State, T: 'static> Val<S, T>
Sourcepub fn new_state(factory: impl Fn(&S) -> T + 'static) -> Self
pub fn new_state(factory: impl Fn(&S) -> T + 'static) -> Self
Create a new state dependent value from the given closure.
Sourcepub fn get(self, state: &S) -> T
pub fn get(self, state: &S) -> T
Get the inner value of this Val.
This will compute the value if it’s state dependent and not yet computed, so it will always return something.
Sourcepub fn get_ref(&mut self, state: &S) -> &T
pub fn get_ref(&mut self, state: &S) -> &T
Get a reference to the inner value of this Val.
This will compute the value if it’s state dependent and not yet computed, so it will always return something.
Sourcepub fn get_mut(&mut self, state: &S) -> &mut T
pub fn get_mut(&mut self, state: &S) -> &mut T
Get a mutable reference to the inner value of this Val.
This will compute the value if it’s state dependent and not yet computed, so it will always return something.
Sourcepub fn compute(&mut self, state: &S)
pub fn compute(&mut self, state: &S)
If the inner value is state dependent, compute it using the given state.
Sourcepub fn invalid(&self) -> bool
pub fn invalid(&self) -> bool
Returns if the inner value is state dependent and not yet computed.
Sourcepub fn invalidate(&mut self)
pub fn invalidate(&mut self)
Invalidates the inner value, if it’s state dependent.
This makes Val::get and other methods re-compute the value (if it’s state dependent).
Sourcepub fn value(self) -> Option<T>
pub fn value(self) -> Option<T>
Returns the inner value or None if it’s state dependent and not yet computed.
Sourcepub fn value_ref(&self) -> Option<&T>
pub fn value_ref(&self) -> Option<&T>
Returns a reference to the inner value or None if it’s state dependent and not yet computed.
Sourcepub fn value_mut(&mut self) -> Option<&mut T>
pub fn value_mut(&mut self) -> Option<&mut T>
Returns a mutable reference to the inner value or None if it’s state dependent and not yet computed.
Sourcepub fn map<U, F: Fn(T) -> U + 'static>(self, f: F) -> Val<S, U>
pub fn map<U, F: Fn(T) -> U + 'static>(self, f: F) -> Val<S, U>
Applies a function to the inner value, transforming it into another value.
Similar to the Option::map method.
Trait Implementations§
Auto Trait Implementations§
impl<S, T> Freeze for Val<S, T>where
T: Freeze,
impl<S, T> !RefUnwindSafe for Val<S, T>
impl<S, T> !Send for Val<S, T>
impl<S, T> !Sync for Val<S, T>
impl<S, T> Unpin for Val<S, T>where
T: Unpin,
impl<S, T> !UnwindSafe for Val<S, 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
Source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
Source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
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.Source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be
further downcast into Rc<ConcreteType> where ConcreteType implements Trait.Source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &Any’s vtable from &Trait’s.Source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot
generate &mut Any’s vtable from &mut Trait’s.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.