macro_rules! impl_from_and_into_var {
($($tt:tt)+) => { ... };
}Expand description
Implements T: IntoVar<U>, T: IntoValue<U> and optionally U: From<T> without boilerplate.
The macro syntax is one or more functions with signature fn from(t: T) -> U. The const_var
kind is used for variables. The syntax also supports generic types and constraints, but not where constraints.
You can also destructure the input if it is a tuple using the pattern fn from((a, b): (A, B)) -> U, but no other pattern
matching in the input is supported.
The U: From<T> implement is optional, you can use the syntax fn from(t: T) -> U; to only generate
the T: IntoVar<U> and T: IntoValue<U> implementations using an already implemented U: From<T>.
ยงExamples
The example declares an enum that represents the values possible in a property foo and
then implements conversions from literals the user may want to type in a widget:
#[derive(Debug, Clone, PartialEq)]
pub enum FooValue {
On,
Off,
NotSet,
}
impl_from_and_into_var! {
fn from(b: bool) -> FooValue {
if b { FooValue::On } else { FooValue::Off }
}
fn from(s: &str) -> FooValue {
match s {
"on" => FooValue::On,
"off" => FooValue::Off,
_ => FooValue::NotSet,
}
}
fn from(f: Foo) -> FooValue;
}
impl From<Foo> for FooValue {
fn from(foo: Foo) -> Self {
Self::On
}
}