The core issue here lies in Rust's orphan rule (and coherence rules) combined with trait implementation overlap due to how Rust treats blanket implementations.
You tried to do:
impl<T, C> ValueModel<T> for C
where
T: Clone,
C: std::ops::Index<usize, Output = T> + ?Sized,
and
impl<T> ValueModel<T> for &[T]
where
T: Clone,
But &[T] satisfies both constraints (it implements Index<usize, Output=T>), and Rust can't choose between them safely, because someone in the future could implement Index differently. Hence, conflicting implementations.
So you're right: [T] implements Index<usize, Output = T>, and &[T] doesn't directly implement Index with output T, but rather delegates to [T], so the compiler sees overlap.
✅ Solutions / Workarounds
✅ 1. Use a Newtype Wrapper
Wrap the slice (or any other type) inside a new struct. This way, you avoid conflicting implementations, because the wrapper is clearly distinct.
pub struct SliceModel<'a, T>(pub &'a [T]);
impl<'a, T> ValueModel<T> for SliceModel<'a, T>
where
T: Clone,
{
fn data(&self, index: usize) -> T {
self.0[index].clone()
}
}
Then in your test:
#[test]
fn with_slice() {
let sequence = [11, 22, 33];
let slice_model = SliceModel(sequence.as_slice());
assert_eq!(slice_model.data(1), 22);
let model = &slice_model as &dyn ValueModel<i32>;
assert_eq!(model.data(1), 22);
}
This avoids any overlap and uses orthogonal implementations.
✅ 2. Specialize with More Specific Traits — Use Index only for owned types
You can limit the generic Index implementation to only apply to types that are not references — for example, by adding C: Sized (or even better, C: 'static), and remove the ?Sized.
But this is still not enough, because even with Sized, the compiler says &[T] might still implement Index in future, so it won’t let you proceed.
✅ 3. Use SliceModel Internally — Auto-adapter helper