Using associated constants within struct defintion

I was writing some simple RingBuffer for my needs, and wanted to try to avoid using generic array hacks using simple trait and its associated constant

Interestingly enough when attempting to refer to the trait constant, despite trait bounds in struct itself, it fails to resolve.
Anyone knows if there is associated issue to such problem in rustlang?

pub trait Capacity {
    const CAPACITY: usize;
}

pub struct MyStruct<T, C> where C: Capacity {
    inner: [T; C::CAPACITY],
}
error[E0599]: no associated item named `CAPACITY` found for type `C` in the current scope
 --> src/lib.rs:7:19
  |
7 |     inner: [T; C::CAPACITY],
  |                   ^^^^^^^^ associated item not found in `C`
  |
  = help: items from traits can only be used if the trait is implemented and in scope
  = note: the following trait defines an item `CAPACITY`, perhaps you need to implement it:
          candidate #1: `Capacity`

This is a compiler bug. https://github.com/rust-lang/rust/issues/43408

5 Likes