/// A type-level boolean suitable to select conditional trait implementations. /// Implementors are [True] and [False] pub trait TLBool {} /// [TLBool] value of `true`. The opposite is [False] pub struct TLTrue; impl TLBool for TLTrue {} /// [TLBool] value of `false`. The opposite is [True] pub struct TLFalse; impl TLBool for TLFalse {} /// A type that implements [Hierarchy]. Used to select implementations of traits /// on the hierarchy pub trait InHierarchy: Clone { /// Indicates that this hierarchy element is a leaf. Leaves can never have /// children type IsLeaf: TLBool; /// Indicates that this hierarchy element is a root. Roots can never have /// parents type IsRoot: TLBool; } /// A type that derives from a parent type. pub trait Extends: InHierarchy + Into { /// Specify the immediate parent of this type. This guides the type Parent: InHierarchy + TryInto + UnderRootImpl<::IsRoot>; } pub trait UnderRootImpl: Sized { type __Root: UnderRoot; fn __into_root(self) -> Self::__Root; fn __try_from_root(root: Self::__Root) -> Result; } pub trait UnderRoot: InHierarchy { type Root: UnderRoot; fn into_root(self) -> Self::Root; fn try_from_root(root: Self::Root) -> Result; } impl> UnderRoot for T { type Root = ::IsRoot>>::__Root; fn into_root(self) -> Self::Root { self.__into_root() } fn try_from_root(root: Self::Root) -> Result { Self::__try_from_root(root) } } impl> UnderRootImpl for T { type __Root = Self; fn __into_root(self) -> Self::__Root { self } fn __try_from_root(root: Self::__Root) -> Result { Ok(root) } } impl + Extends> UnderRootImpl for T { type __Root = <::Parent as UnderRootImpl< <::Parent as InHierarchy>::IsRoot, >>::__Root; fn __into_root(self) -> Self::__Root { ::Parent>>::into(self).into_root() } fn __try_from_root(root: Self::__Root) -> Result { let parent = ::Parent::try_from_root(root)?; parent.clone().try_into().map_err(|_| parent.into_root()) } }