On the code below, I'm trying to cast the interior of the Box
to something that can be indexable, both mutably and not.
use core::ops::IndexMut;
use std::slice::SliceIndex;
use core::ops::Index;
pub trait M<T, I: SliceIndex<[T], Output = T>>:
AsRef<[T]> + AsMut<[T]> + Index<I, Output = T> + IndexMut<I>
{}
impl<T, I, V> M<T, I> for V
where
V: AsRef<[T]> + AsMut<[T]> + Index<I, Output = T> + IndexMut<I>,
I: SliceIndex<[T], Output = T>,
{}
fn allocate_default<T: Default + Clone, I>(size: usize) -> Box<dyn M<T, I> + 'static>
where
I: SliceIndex<[T], Output = T>,
{
let s = vec![T::default(); size];
let s = s.into_boxed_slice();
Box::new(s)
}
Why is it complaining that Box
does not implement IndexMut
? Shouldn't it complain about the interior not implementing it?
I think it may have something to do with interior mutability of Box
maybe, so I tried fn allocate_default<T: Default + Clone, I>(size: usize) -> RefCell<dyn M<T, I> + 'static>
but then I get
error[E0277]: the size for values of type `(dyn M<T, I, Output = T> + 'static)` cannot be known at compilation time
I want to return something that implement M
, not the actual vector.