1:
pub struct S<A> {
array_like: A,
}
impl<A: ArrayLike<Item = u8>> S<A> {
pub fn index(&self, index: usize) -> &u8 {
self.array_like.index(index)
}
// ...
}
pub trait ArrayLike {
type Item;
fn index(&self, index: usize) -> &Self::Item;
// ...
}
impl<T: Clone> ArrayLike for Vec<T> {
type Item = T;
fn index(&self, index: usize) -> &Self::Item {
&self[index]
}
}
2:
use std::ops::Index;
pub struct S<A> {
array_like: A,
}
impl<A> S<A>
where
A: ArrayLike<Item = u8, Output = u8>,
{
// ...
}
impl<A> Index<usize> for S<A>
where
A: ArrayLike<Item = u8>,
{
type Output = A::Output;
fn index(&self, index: usize) -> &Self::Output {
&self.array_like[index]
}
}
pub trait ArrayLike: Index<usize> {
type Item;
// ...
}
One more question.
Is there any way to write something like:
impl<A> S<A>
where
A: ArrayLike<Item = u8, Output = u8>,
// A: ArrayLike<Item = u8, Output = Item>
// A: ArrayLike<Item = u8, Output = A::Item>
{}