我怎么有一个仅用于内部常量的结构? [英] How can I have a struct which is only used for its internal constant?
问题描述
我需要在Rust类型系统中封装常量。理想情况下, RFC2000 应该已经准备就绪,但是在没有它的情况下,因为我只是需要一组受限制的常量,我可以实现一些与我需要的接近的东西:
trait U32Const {
const VAL:u32;
}
结构U32Const10;
impl U32Const for U32Const10 {
const VAL:u32 = 10;
}
struct MyType< X:U32Const> {
val:u32,
模板:X,
}
impl< X:U32Const> MyType< X> {
fn do_something(& self)-> u32 {
self.val * X :: VAL
}
}
fn main(){
let a = MyType ::< U32Const10> ; {
val:20,
模板:U32Const10 {},
};
println!( {},a.do_something());
}
这将打印出 200
根据需要-即常量值来自 main
内部实例化时传递的类型。
现在,这有点谨慎,因为它需要在结构中创建 X
的实例,我称之为 template
,然后未使用它,所以我得到了编译器警告。
如果删除了 template
字段,如果是理想的API,则编译器会抱怨 struct MyType<上的未使用参数
。如果我摆脱了 X
。 X:U32Const> struct MyType
上的 X
参数,则得到了一个意外类型
。 impl
块中 MyType
上的参数
有什么办法可以使编译器满意吗?实际上,我想要一个仅用于其内部 const
的结构。
特性U32Const {
type U32;
const VAL:Self :: U32;
}
结构U32Const10;
表示U32Const10的U32Const {
类型U32 = u32;
const VAL:Self :: U32 = 10;
}
struct MyType< X:U32Const> {
val:X :: U32,
}
impl< X:U32Const< U32 = u32> MyType< X> {
fn do_something(& self)-> X :: U32 {
self.val * X :: VAL
}
}
fn main(){
let a = MyType :: < U32Const10> {val:20};
println!( {},a.do_something());
}
游乐场(具有多个const实现)
I need to encapsulate constants in the Rust type system. Ideally, RFC2000 would be ready, but in its absence, and since I only need a restricted set of constants, I can implement something close to what I need:
trait U32Const {
const VAL: u32;
}
struct U32Const10;
impl U32Const for U32Const10 {
const VAL: u32 = 10;
}
struct MyType<X: U32Const> {
val: u32,
template: X,
}
impl<X: U32Const> MyType<X> {
fn do_something(&self) -> u32 {
self.val * X::VAL
}
}
fn main() {
let a = MyType::<U32Const10> {
val: 20,
template: U32Const10 {},
};
println!("{}", a.do_something());
}
This prints out 200
as desired - that is, the constant value comes from the type that is passed in at instantiation inside main
.
Now, this is a bit warty as it requires an instance of X
to be created in the struct, which I call template
, which is then unused so I get a compiler warning.
If one removes the template
field, which is the ideal API, then the compiler complains about an unused parameter X
on struct MyType < X: U32Const >
. If I get rid of the X
parameter on struct MyType
, I then get an unexpected type argument
on MyType
in the the impl
block.
Is there some way I can do what I'm trying to do that keeps the compiler happy? Effectively I want a struct which is only used for its internal const
.
Instead of giving a phantom variable into a struct, using associated types might do the trick,
trait U32Const {
type U32;
const VAL: Self::U32;
}
struct U32Const10;
impl U32Const for U32Const10 {
type U32 = u32;
const VAL: Self::U32 = 10;
}
struct MyType<X: U32Const> {
val: X::U32,
}
impl<X: U32Const<U32 = u32>> MyType<X> {
fn do_something(&self) -> X::U32 {
self.val * X::VAL
}
}
fn main() {
let a = MyType::<U32Const10> { val: 20 };
println!("{}", a.do_something());
}
Playground (with multiple const implementations)
这篇关于我怎么有一个仅用于内部常量的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!