Crate const_generic_wrap

Source
Expand description

Simple wrapper for const generics.

§Usage

Currently ‘the type of const parameters must not depend on other generic parameters’ (E0770).

struct A<N, const C : N>(N);

With this crate we can solve this by wrapping cosnt generic.

use const_generic_wrap::*;
struct A<N, C>(N, C) where C : ConstWrap<BaseType = N>;
// WrapU32 is ZST, so the size of A is as same as u32.
assert_eq!(mem::size_of::<WrapU32<12>>(), 0);
assert_eq!(mem::size_of::<A::<u32, WrapU32<12>>>(), mem::size_of::<u32>());

// you can selectively use const or non const
struct B<N, C>(N, C) where C : ConstOrValue<N>, N : Constable; // or it can be C : Into<N>
fn add_b<N, C>(v : B<N, C>) -> N where N : Add<Output = N> + Constable, C : ConstOrValue<N>{
    v.0 + v.1.into()
}
let b_non_const = B(31, 11);
let b_const = B(31, WrapI32::<11>);
assert_eq!(add_b(b_non_const), add_b(b_const));

Structs§

MismatchConstError
Failed to convert to const wrap type.
WrapBOOL
Const generic wrapper.
WrapCHAR
Const generic wrapper.
WrapI8
Const generic wrapper.
WrapI16
Const generic wrapper.
WrapI32
Const generic wrapper.
WrapI64
Const generic wrapper.
WrapISIZE
Const generic wrapper.
WrapU8
Const generic wrapper.
WrapU16
Const generic wrapper.
WrapU32
Const generic wrapper.
WrapU64
Const generic wrapper.
WrapUSIZE
Const generic wrapper.

Traits§

ConstIntTypes
A lists of types for a specified values.
ConstOrValue
Trait that can be a wrapped const generic or a owned value.
ConstWrap
Marker that shows it wraps const generic.
Constable
Marker that shows it can be used in const generic.