#[derive(IntoAncestors)]
{
// Attributes available to this derive:
#[ancestors]
}
Expand description
Derive From<Self> for every enum mentioned in an
#[ancestors(Parent::Variant)] attribute.
For each #[ancestors(Parent::Variant)] attribute on the deriving type,
the macro emits:
impl ::core::convert::From<Self> for Parent {
fn from(child: Self) -> Parent {
Parent::Variant(::core::convert::Into::into(child))
}
}The body wraps the child via Into::into, so any compatible
From/Into chain already in scope is reused. List every parent the
type needs to reach directly; the macro does not transitively walk
the #[ancestors(...)] attributes on intermediate enums.
§Example
use enum_tree::{EnumFrom, IntoAncestors};
#[derive(EnumFrom)]
enum Fermion
{
Quark(#[enum_from(from)] Quark),
Charged(#[enum_from(from)] ChargedLepton),
Neutrino(#[enum_from(from)] Neutrino),
}
#[derive(EnumFrom)]
enum Quark
{
Up(#[enum_from(from)] UpQuark),
Charm(#[enum_from(from)] CharmQuark),
}
struct ChargedLepton;
struct Neutrino;
// Each leaf lists every ancestor it must reach directly.
#[derive(IntoAncestors)]
#[ancestors(Quark::Charm)]
#[ancestors(Fermion::Quark)]
struct CharmQuark;
#[derive(IntoAncestors)]
#[ancestors(Quark::Up)]
#[ancestors(Fermion::Quark)]
struct UpQuark;
let _: Quark = CharmQuark.into();
let _: Fermion = CharmQuark.into();
let _: Fermion = UpQuark.into();§Limitations
The macro does not have access to the parent enum’s definition, so it
cannot emit a where Self: ::core::convert::Into<...> bound: the
payload type of Parent::Variant is unknown at derive time. If the
caller has not supplied a compatible From/Into chain, the
resulting type error appears inside the generated function body
instead of on the impl header.