如何使用关联的常量来定义数组的长度?



我有一个特征,它表示可以通过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`

我可以在此上下文中使用关联的常量吗?

在撰写本文时,这是不可能的。但是,让我们首先修复您的代码:

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

这只是问题的错误错误消息。在GitHub上看到这条评论:

[U]将关联的常量作为数组大小似乎不起作用:

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];
}

有错误消息显然是错误的,因此这要么是rustc中的错误,要么是导致虚假错误消息的未实现功能。

直接在结构中定义const NET_SIZE时,可以解决此问题。

您可以在此特定错误的 GitHub 问题中阅读有关此内容的更多信息:数组长度不支持泛型类型参数 (#43408(。

要访问关联的常量,您需要使用以下稍微繁琐的语法明确说明它来自哪个特征:<T as ToNetEnt>::NET_SIZE.

最新更新