I'm trying to use a vector of indexed containers, e.i. any type that implements std::ops::Index
, but I can't find a solution to assigning the type for type Output
in the implementation section.
From what I know it should look something like this:
pub struct Holder<C: Index<usize> + ExactSizeIterator> {
containers: Vec<C>
}
impl<C: Index<usize>, T> Index<usize> for Holder<C> {
type Output = T;
fn index(&self, index: usize) -> &Self::Output {
todo!();
}
}
But this obviously ends up with
the type parameter
T
is not constrained by the impl trait, self type, or predicates unconstrained type parameter rustc(E0207)
(For clarification, the error points to the T
in impl<C: Index<usize>, T>
)
However I do not know of a way of adding another generic type to the definition of Holder
that wouldn't just be an unused field.
The purpose of Holder
is to contain other containers that are indexable and the index function of Holder
should return an item from one of said containers.
Let me know if there anything is missing from the post. Any help or pointers would be much appreciated, Thank you!
You probably want to remove T
entirely and deduce the Output
type from C
directly:
impl<C: Index<usize> + ExactSizeIterator> Index<usize> for Holder<C> {
type Output = C::Output; // <--------
fn index(&self, index: usize) -> &Self::Output {
todo!();
}
}