Skip to content

Commit 5c2795e

Browse files
authored
Allow slicing our OOM-handling Vec with ranges (#12582)
Not just `usize`s.
1 parent ce09e2a commit 5c2795e

1 file changed

Lines changed: 17 additions & 9 deletions

File tree

crates/core/src/alloc/vec.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
use crate::alloc::{TryClone, try_realloc};
22
use crate::error::OutOfMemory;
3-
use core::cmp::Ordering;
4-
use core::marker::PhantomData;
5-
use core::num::NonZeroUsize;
63
use core::{
7-
fmt, mem,
4+
cmp::Ordering,
5+
fmt,
6+
marker::PhantomData,
7+
mem,
8+
num::NonZeroUsize,
89
ops::{Deref, DerefMut, Index, IndexMut},
10+
slice::SliceIndex,
911
};
1012
use serde::ser::SerializeSeq;
1113
use std_alloc::alloc::Layout;
@@ -302,16 +304,22 @@ impl<T> DerefMut for Vec<T> {
302304
}
303305
}
304306

305-
impl<T> Index<usize> for Vec<T> {
306-
type Output = T;
307+
impl<T, I> Index<I> for Vec<T>
308+
where
309+
I: SliceIndex<[T]>,
310+
{
311+
type Output = <I as SliceIndex<[T]>>::Output;
307312

308-
fn index(&self, index: usize) -> &Self::Output {
313+
fn index(&self, index: I) -> &Self::Output {
309314
&self.inner[index]
310315
}
311316
}
312317

313-
impl<T> IndexMut<usize> for Vec<T> {
314-
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
318+
impl<T, I> IndexMut<I> for Vec<T>
319+
where
320+
I: SliceIndex<[T]>,
321+
{
322+
fn index_mut(&mut self, index: I) -> &mut Self::Output {
315323
&mut self.inner[index]
316324
}
317325
}

0 commit comments

Comments
 (0)