Optional const
Optional constness on stable Rust.
This crate should be superseded by keyword genericity in the future.
Usage
use ;
Limitations
- Rust currently doesn't allow defining a type like
struct ConstType<T, const VAL: T>;because the type of const parameters must not depend on other generic parameters [E770]. Consequently, one can't provide a canonical "const type" for any const value. - The
const_type_instance!macro currently supports onlybooltype. However, it can be extended to support other types in the future. - Due to lack of support for negative trait bounds and [E770], it's impossible to implement
OptionallyConst<T>for all types that implementConst<T>. The current implementation only supportsbooltype. However, you can implement bothOptionallyConst<T>andConst<T>for your own types.
Optional constness for user-defined types
Enums
use Const;
;
;
;
// Ideally, OptionallyConst<T> should be implemented for
// all types that implement Const<T>.
//
// However, impl of OptionallyConst<T> for all `T` conflicts with the
// impl of OptionallyConst<T> for all `U: Const<T>` in the absence of
// negative trait bounds.
Note: this can be done even easier with the FieldlessEnumConstType derive macro, which is exposed when the derive feature is enabled.
Use cases
- More generic const parameters: at the moment of writing, only a handful of types can be used as generic const parameters. However, with this crate, you can accept
T: Const<U>whereUis an enum type.