pub trait IntoVar<T: VarValue> {
type Var: Var<T>;
// Required method
fn into_var(self) -> Self::Var;
// Provided method
fn into_boxed_var(self) -> BoxedVar<T>
where Self: Sized { ... }
}
Expand description
A value-to-var conversion that consumes the value.
Every Var<T>
implements this to convert to itself, every VarValue
implements this to
convert to a LocalVar<T>
.
This trait is used by most properties, it allows then to accept literal values, variables and context variables
all with a single signature. Together with Var<T>
this gives properties great flexibility of usage, at zero-cost. Widget
when
blocks also use IntoVar<T>
to support changing the property value depending on the widget state.
Value types can also manually implement this to support a shorthand literal syntax for when they are used in properties,
this converts the shorthand value like a tuple into the actual value type and wraps it into a variable, usually LocalVar
too. They can implement the trait multiple times to support different shorthand syntaxes or different types in the shorthand
value.
Required Associated Types§
Required Methods§
Provided Methods§
sourcefn into_boxed_var(self) -> BoxedVar<T>where
Self: Sized,
fn into_boxed_var(self) -> BoxedVar<T>where
Self: Sized,
Converts into BoxedVar<T>
.
This method exists to help the type system infer the type in this scenario:
fn foo(foo: impl IntoVar<bool>) { }
foo(if bar {
BAR_VAR.map(|b| !*b).boxed()
} else {
true.into_boxed_var()
});
We need a BoxedVar<bool>
to unify the input types that can be a map
var or a LocalVar<bool>
. Writing true.into_var().boxed()
causes the type inference to fail, requiring us to write IntoVar::<bool>::into_var(true).boxed()
.