如何在where子句中使用关联常量?

时间:2017-09-22 15:20:34

标签: rust

以下对我来说似乎是个错误。

struct Seq([u8; 8]);

impl From<[u8; 8]> for Seq {
    fn from(data: [u8; 8]) -> Seq {
        Seq(data)
    }
}

trait Sequence {
    const LEN: usize;
}

impl Sequence for Seq {
    const LEN: usize = 8;
}

trait ByteSequence {
    fn check();
}

impl<S> ByteSequence for S
where
    S: Sequence + From<[u8; <S as Sequence>::LEN]>,
{
    fn check() {}
}

导致以下错误:playground

error[E0277]: the trait bound `S: Sequence` is not satisfied
  --> src/main.rs:23:29
   |
23 |     S: Sequence + From<[u8; <S as Sequence>::LEN]>,
   |                             ^^^^^^^^^^^^^^^^^^^^ the trait `Sequence` is not implemented for `S`
   |
   = help: consider adding a `where S: Sequence` bound
   = note: required by `Sequence::LEN`

我也尝试在额外的行中添加绑定或直接在impl声明中添加。

1 个答案:

答案 0 :(得分:4)

这不是 bug ,它还没有实现。 Generic associated consts can't currently be used to parameterize fixed array lengths。您可能会为不那么有用的错误消息提交另一个问题(至少有one other poor error message)。