Are there any problems with this (or missing API), or should I draft a PR? It seems only right that Box<[T]> should gain the "owned slice" powers of Vec<T> that don't require resizing. I was trying to rewrite a function taking Box<[T]> to work with both Vec and Box and noticed that Box<[T]> doesn't have any of the iterators.
Perhaps the IntoIterator for &Box<[T]> could be generalized further to IntoIterator for Box<I> where for<'a> &'a I: IntoIterator.
I assumed that it wouldn't be reusable, but actually, thinking about it, you're right. We can use vec::IntoIter and just tell it we're a Vec with capacity == len. (Because the on-heap representation of the two is identical.)
So the added API surface would be:
impl<T> IntoIterator for Box<[T]> {
type Item = T;
type IntoIter = vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
Vec::from(self).into_iter()
}
}
impl<'a, I> IntoIterator for &'a Box<I>
where
&'a I: IntoIterator,
{
type Item = <&I>::Item;
type IntoIter = <&I>::IntoIter;
fn into_iter(self) -> {
(&**self).into_iter()
}
}
impl<'a, I> IntoIterator for &'a mut Box<I>
where
&'a mut I: IntoIterator
{
type Item = <&mut I>::Item;
type IntoIter = <&mut I>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
(&mut **self).into_iter()
}
}
Box<[T]>::iter and Box<[T]>::iter_mut aren't needed because for inherent methods you can just deref to slice like normal.
The "RefIntoIter" latter two implementations can be debated the utility of, but the first one at least seems nonproblematic.
Damn And what about a an associated .into_iter() function on Box<[T]> ? This at least could allow someone to use Box::into_iter(my_boxed_slice) on generic functions expecting a impl IntoIterator, and in non-generic context could also be quite useful.
Agreed: given that semantically a Vec<T> is
struct Vec<T> {
raw_buffer: Box<[MaybeUninit<T>]>, // ptr, cap
initialized_count: usize, // len
}
factoring the iteration logic between Box<[T]> and Vec<T> looks like the right thing to do.
I don't think that's substantially more of an "implementation detail" than <[T]>::Owned being Vec<T>. I think Vec::from(the_box).into_iter() is acceptable.