#const-generics #no-std #wrap #solve

no-std const-generic-wrap

const generics 的简单封装

2 个不稳定版本

0.3.0 2021年6月25日
0.2.0 2021年6月25日
0.1.0 2021年4月26日

#22 in #solve


lattice-graph 中使用

MIT 许可证

12KB
234 行代码(不包括注释)

const_generic_wrap

const generics 的简单封装。 Doc Crate

使用方法

当前情况下,'const 参数的类型不能依赖于其他泛型参数'(E0770)。

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

使用这个包,我们可以通过封装 cosnt generics 来解决这个问题。

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>; // or it can be C : Into<N>
fn add_b<N, C>(v : B<N, C>) -> N where N : Add<Output = N>, 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));

此包与 no_std 一起工作。

依赖项

~39KB