如何使用关联常量来定义数组的长度?
How to use associated constants to define the length of an array?
我有一个特征,它表示可以通过 UDP 套接字发送的实体:
pub trait ToNetEnt {
const NET_SIZE: usize;
fn from_net(data: &[u8]) -> Self;
fn to_net(&self) -> &[u8];
}
虽然有一个关联常量 NET_SIZE
,但我无法在方法中使用它:
pub fn req<T: ToNetEnt>(&self) -> T {
let buf: [u8; T::NET_SIZE];
}
因为它导致了这个错误:
error[E0599]: no associated item named `NET_SIZE` found for type `T` in the current scope
--> src/main.rs:10:23
|
10 | let buf: [u8; T::NET_SIZE];
| ^^^^^^^^^^^ associated item not found in `T`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `NET_SIZE`, perhaps you need to implement it:
candidate #1: `ToNetEnt`
我可以在此上下文中使用关联常量吗?
要访问关联常量,您需要明确说明它来自哪个特征,使用这种稍微麻烦的语法:<T as ToNetEnt>::NET_SIZE
.
在撰写本文时,这是不可能的。但让我们先修复您的代码:
pub fn req<T: ToNetEnt>(&self) -> T {
let buf: [u8; <T as ToNetEnt>::NET_SIZE];
...
}
这应该是正确的语法,但目前无法编译:
error[E0277]: the trait bound `T: ToNetEnt` is not satisfied
--> src/main.rs:6:19
|
6 | let buf: [u8; <T as ToNetEnt>::NET_SIZE];
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ToNetEnt` is not implemented for `T`
|
= help: consider adding a `where T: ToNetEnt` bound
这只是针对问题的错误错误消息。见 this comment on GitHub:
[U]sing an associated const as an array size does not appear to work:
pub trait Can {
const SIZE: usize;
}
fn f<T: Can>(t: T) {
// error[E0277]: the trait bound `T: Can` is not satisfied
let x = [0u8; <T as Can>::SIZE];
}
There error message is clearly wrong, so this is either a bug in rustc
or unimplemented functionality resulting in a bogus error message.
直接在结构中定义 const NET_SIZE
时可以解决此问题。
您可以在针对此特定错误的 GitHub 问题中阅读更多相关信息:Array lengths don't support generic type parameters (#43408)。
我有一个特征,它表示可以通过 UDP 套接字发送的实体:
pub trait ToNetEnt {
const NET_SIZE: usize;
fn from_net(data: &[u8]) -> Self;
fn to_net(&self) -> &[u8];
}
虽然有一个关联常量 NET_SIZE
,但我无法在方法中使用它:
pub fn req<T: ToNetEnt>(&self) -> T {
let buf: [u8; T::NET_SIZE];
}
因为它导致了这个错误:
error[E0599]: no associated item named `NET_SIZE` found for type `T` in the current scope
--> src/main.rs:10:23
|
10 | let buf: [u8; T::NET_SIZE];
| ^^^^^^^^^^^ associated item not found in `T`
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `NET_SIZE`, perhaps you need to implement it:
candidate #1: `ToNetEnt`
我可以在此上下文中使用关联常量吗?
要访问关联常量,您需要明确说明它来自哪个特征,使用这种稍微麻烦的语法:<T as ToNetEnt>::NET_SIZE
.
在撰写本文时,这是不可能的。但让我们先修复您的代码:
pub fn req<T: ToNetEnt>(&self) -> T {
let buf: [u8; <T as ToNetEnt>::NET_SIZE];
...
}
这应该是正确的语法,但目前无法编译:
error[E0277]: the trait bound `T: ToNetEnt` is not satisfied
--> src/main.rs:6:19
|
6 | let buf: [u8; <T as ToNetEnt>::NET_SIZE];
| ^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `ToNetEnt` is not implemented for `T`
|
= help: consider adding a `where T: ToNetEnt` bound
这只是针对问题的错误错误消息。见 this comment on GitHub:
[U]sing an associated const as an array size does not appear to work:
pub trait Can { const SIZE: usize; } fn f<T: Can>(t: T) { // error[E0277]: the trait bound `T: Can` is not satisfied let x = [0u8; <T as Can>::SIZE]; }
There error message is clearly wrong, so this is either a bug in
rustc
or unimplemented functionality resulting in a bogus error message.
直接在结构中定义 const NET_SIZE
时可以解决此问题。
您可以在针对此特定错误的 GitHub 问题中阅读更多相关信息:Array lengths don't support generic type parameters (#43408)。