Derive Macro TryInto

Source
#[derive(TryInto)]
{
    // Attributes available to this derive:
    #[enumcapsulate]
}
Expand description

Derive macro generating an impl of the trait TryFrom<T>.

It generates an impl for each of the enum’s newtype variants, where Inner is the variant’s field type and Outer is the enclosing enum’s type.

struct Inner;

enum Outer {
    Inner(Inner),
    // ...
}

// The generated impls look something along these lines:

impl TryFrom<Outer> for Inner {
    type Error = Outer;

    fn try_from(outer: Outer) -> Result<Self, Self::Error> {
        match outer {
            Outer::Inner(inner) => Ok(inner),
            err => Err(err),
        }
    }
}

// ...

Note: Despite the derive macro’s name it’s actually TryFrom<T> that’s being derives, not TryInto<T>. But since the macro is derived on the enum the latter feels more appropriate as the derive’s name.