enum-tree 0.1.0

Derives for hierarchical enums: const variant enumeration, From conversions, and ancestor wrapping
Documentation
//! Hygiene tests for enum-tree-derive: the derives must produce tokens
//! that compile even when the standard prelude is disabled and primitive
//! type names are shadowed in the surrounding scope.

#![no_implicit_prelude]
#![allow(dead_code, reason = "fields exist solely to exercise the derive")]
#![allow(
    non_camel_case_types,
    reason = "shadowing primitives is the point of this test"
)]

trait bool {}
trait char {}
trait f32 {}
trait f64 {}
trait i8 {}
trait i16 {}
trait i32 {}
trait i64 {}
trait i128 {}
trait isize {}
trait str {}
trait u8 {}
trait u16 {}
trait u32 {}
trait u64 {}
trait u128 {}
trait usize {}

#[derive(::enum_tree::DeepVariants)]
enum UnitEnum
{
    A,
    B,
    C,
}

#[derive(::enum_tree::DeepVariants)]
enum NestedEnum
{
    Plain,
    Nested(UnitEnum),
}

#[derive(::enum_tree::EnumFrom)]
enum WithFrom
{
    Int(#[enum_from(from)] ::core::primitive::i32),
    Text(#[enum_from(from)] ::std::string::String),
}

#[derive(::enum_tree::EnumFrom)]
enum AncestorEnum
{
    Branch(WithFrom),
}

#[derive(::enum_tree::IntoAncestors)]
#[ancestors(AncestorEnum::Branch)]
struct Leaf;

impl ::core::convert::From<Leaf> for WithFrom
{
    fn from(_: Leaf) -> Self
    {
        Self::Int(0)
    }
}

#[test]
fn derives_compile_without_prelude()
{
    let _ = UnitEnum::A;
    let _ = NestedEnum::Nested(UnitEnum::B);
    let _: WithFrom = <WithFrom as ::core::convert::From<::core::primitive::i32>>::from(42);
    let _: WithFrom = <WithFrom as ::core::convert::From<::std::string::String>>::from(
        ::std::string::String::new(),
    );
    let _: AncestorEnum = <AncestorEnum as ::core::convert::From<Leaf>>::from(Leaf);
}

mod shadow_core
{
    pub mod core
    {}
    pub mod std
    {}

    #[derive(::enum_tree::DeepVariants)]
    pub enum Inner
    {
        A,
        B,
    }

    #[derive(::enum_tree::DeepVariants)]
    pub enum Outer
    {
        Unit,
        Nested(Inner),
    }

    #[derive(::enum_tree::EnumFrom)]
    pub enum Conv
    {
        Wrap(#[enum_from(from)] ::core::primitive::u32),
    }
}

#[test]
fn derives_compile_when_core_is_shadowed()
{
    let _ = shadow_core::Outer::Nested(shadow_core::Inner::A);
    let _: shadow_core::Conv =
        <shadow_core::Conv as ::core::convert::From<::core::primitive::u32>>::from(7);
}

mod renamed_crate
{
    pub use ::enum_tree as renamed;

    #[derive(renamed::DeepVariants, PartialEq, Eq, Debug)]
    #[enum_tree(crate = renamed)]
    pub enum Inner
    {
        A,
        B,
    }

    #[derive(renamed::DeepVariants, PartialEq, Eq, Debug)]
    #[enum_tree(crate = renamed)]
    pub enum Outer
    {
        Unit,
        Nested(Inner),
    }
}

#[test]
fn deep_variants_compiles_when_crate_is_renamed()
{
    use renamed_crate::renamed::DeepVariants as _;

    ::core::assert_eq!(renamed_crate::Inner::DEEP_VARIANTS.len(), 2);
    ::core::assert_eq!(renamed_crate::Outer::DEEP_VARIANTS.len(), 3);
}