Function-like macro that "unconsts" trait implementations
unconst_trait_impl::unconst_trait_impl
turns the Nightly syntax for constant trait implementations into analogous non-const syntax that is accepted on stable toolchain.
The list of features taken into account:
In a vaccum, unconst_trait_impl
procedural function-like macro is fairly useless because its call on constant trait implementation yields the same result as writing the non-const implementation in the first place.
However, with cfg_attr
and remove_macro_call
attributes, unconst_trait_impl
macro allows one to conditionally remove the macro call thus providing support for stable toolchain while also providing functionality relying on Nightly features.
Example
use unconst_trait_impl;
use ;
use remove_macro_call;
// Since ZST is both Eq and and PartialEq, it has structural match
// https://github.com/rust-lang/rust/issues/63438
;
unconst_trait_impl!
// With `cargo build --features const_trait_impl, const_default_impls, const_fn_trait_bound`
// or with `cargo build --all-features, the code below is expanded as is. Otherwise,
// it gets "unconsted" to be supported by stable toolchain.
unconst_trait_impl!
Note: In the real code, the example above could be replaced with a simpler version relying on cfg_aliases
crate.
You can learn more about remove_macro_call
here:
Known limitations
Currently, type parameters (like T
in T: ~const TraitName + ?Sized
) get "unconsted" only when
- they belong to the trait implementation, i.e. when they are in
<..>
or in thewhere
clause of the trait implementation; - they belong to the signatures of methods and associated functions;
Other Items in the trait implementation currently don't get "unconsted".