TyLift
Lift enum variants to the type-level by simply adding the attribute tylift
.
This comes in handy for type-level programming. The crate you are looking at is
brand new and far from polished!
Important note: This library provides mechanisms nearly identical to the experimental feature const generics which has not been fully implemented yet. See respective section below on why this crate stays relevant nonetheless.
The attribute promotes variants to their own types which will not be namespaced
by current design. The enum type becomes a kind emulated by a trait. In the
process, the original type gets replaced. In Rust, the syntax of trait bounds (:
) beautifully
mirror the syntax of type annotations. Thus, the snippet B: Bool
can also be
read as "type parameter B
of kind Bool
".
As of right now, there is no automated way to reify the lifted variants. Variants can hold unnamed fields of types of given kind. Lifted enum types cannot be generic over kinds. The promoted variants inherit the visibility of the lifted enum. Traits representing kinds are sealed, which means nobody is able to add new types to the kind.
Attributes (notably documentation comments) applied to the item itself and its variants will be preserved. On the other hand, attributes placed in front of fields of a variant (constructor arguments) will not be translated and thus have no effect. Explicit discriminants are ignored, too.
Expanded code works in #![no_std]
-environments.
First Example
use tylift;
use PhantomData;
Installation
Works with rustc
version 1.32 (stable) or above, Rust 2018 edition. Add these lines to your Cargo.toml
:
[]
= "0.3.2"
More Examples
Code before the macro expansion:
use tylift;
pub
And after:
use tylift;
pub use *;
pub use *;
use *;
TyLift Versus Const Generics
Advantages of this crate over const generics:
- recursive kinds which cannot be represented with const generics right now. The latter would also require explicit boxing
- once tyfns (type-lifted functions) are implemented, TyLift features pattern matching on types [Todo: Add source on restrictions of const generics]
- [Todo]
Future Plans
- replacing the introductery example with something more reasonable
- creating tests
- enhancing the generated names for friendlier error messages
- adding additional features like
- type-level functions
- generation of a reification function
- removing the feature-gate
span_errors
onceproc_macro_diagnostic
becomes stable