Closed glandium closed 5 years ago
Another example:
use std::marker::PhantomData;
pub trait AsIndex: Copy {
const MAX: usize;
fn cast_to(self) -> usize;
fn cast_from(idx: usize) -> Self;
}
impl AsIndex for usize {
const MAX: usize = !0usize >> 1;
fn cast_to(self) -> usize {self}
fn cast_from(idx: usize) -> usize {idx}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BiMap<L, R>
where
L: AsIndex,
R: AsIndex,
{
left: [Option<usize>; <L as AsIndex>::MAX + 1],
right: [Option<usize>; <R as AsIndex>::MAX + 1],
_marker: PhantomData<(L, R)>,
}
Results in:
error[E0277]: the trait bound `L: AsIndex` is not satisfied
--> src/main.rs:29:27
|
29 | left: [Option<usize>; <L as AsIndex>::MAX + 1],
| ^^^^^^^^^^^^^^^^^^^ the trait `AsIndex` is not implemented for `L`
|
= help: consider adding a `where L: AsIndex` bound
note: required by `AsIndex::MAX`
--> src/main.rs:5:5
|
5 | const MAX: usize;
| ^^^^^^^^^^^^^^^^^
error[E0277]: the trait bound `R: AsIndex` is not satisfied
--> src/main.rs:30:28
|
30 | right: [Option<usize>; <R as AsIndex>::MAX + 1],
| ^^^^^^^^^^^^^^^^^^^ the trait `AsIndex` is not implemented for `R`
|
= help: consider adding a `where R: AsIndex` bound
note: required by `AsIndex::MAX`
--> src/main.rs:5:5
|
5 | const MAX: usize;
| ^^^^^^^^^^^^^^^^^
error: aborting due to 2 previous errors
I was running into this as well. See also https://github.com/rust-lang/rust/issues/50439 -- it may not be the same issue, but it's also using this array indexing trick.
This is a symptom of https://github.com/rust-lang/rust/issues/43408
The following code fails to compile
The error is:
which makes no sense since there is an implicit T: Sized bound from the struct definition. Even with an explicit bound, the error occurs.
Combined with associated consts, this leads to weird error messages: