Skip to content

Commit 8c3dfbc

Browse files
authored
Merge pull request #132 from bluss/range-bounds
Use RangeBounds for .drain()'s range argument
2 parents 75a6fcd + aece292 commit 8c3dfbc

File tree

3 files changed

+43
-51
lines changed

3 files changed

+43
-51
lines changed

src/lib.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ extern crate core as std;
3030
use std::cmp;
3131
use std::iter;
3232
use std::mem;
33+
use std::ops::{Bound, Deref, DerefMut, RangeBounds};
3334
use std::ptr;
34-
use std::ops::{
35-
Deref,
36-
DerefMut,
37-
};
3835
use std::slice;
3936

4037
// extra traits
@@ -55,11 +52,9 @@ use serde::{Serialize, Deserialize, Serializer, Deserializer};
5552
mod array;
5653
mod array_string;
5754
mod char;
58-
mod range;
5955
mod errors;
6056

6157
pub use crate::array::Array;
62-
pub use crate::range::RangeArgument;
6358
use crate::array::Index;
6459
pub use crate::array_string::ArrayString;
6560
pub use crate::errors::CapacityError;
@@ -572,7 +567,9 @@ impl<A: Array> ArrayVec<A> {
572567
/// assert_eq!(&v[..], &[3]);
573568
/// assert_eq!(&u[..], &[1, 2]);
574569
/// ```
575-
pub fn drain<R: RangeArgument>(&mut self, range: R) -> Drain<A> {
570+
pub fn drain<R>(&mut self, range: R) -> Drain<A>
571+
where R: RangeBounds<usize>
572+
{
576573
// Memory safety
577574
//
578575
// When the Drain is first created, it shortens the length of
@@ -584,8 +581,22 @@ impl<A: Array> ArrayVec<A> {
584581
// the hole, and the vector length is restored to the new length.
585582
//
586583
let len = self.len();
587-
let start = range.start().unwrap_or(0);
588-
let end = range.end().unwrap_or(len);
584+
let start = match range.start_bound() {
585+
Bound::Unbounded => 0,
586+
Bound::Included(&i) => i,
587+
Bound::Excluded(&i) => i.saturating_add(1),
588+
};
589+
let end = match range.end_bound() {
590+
Bound::Excluded(&j) => j,
591+
Bound::Included(&j) => j.saturating_add(1),
592+
Bound::Unbounded => len,
593+
};
594+
self.drain_range(start, end)
595+
}
596+
597+
fn drain_range(&mut self, start: usize, end: usize) -> Drain<A>
598+
{
599+
let len = self.len();
589600
// bounds check happens here
590601
let range_slice: *const _ = &self[start..end];
591602

src/range.rs

Lines changed: 0 additions & 42 deletions
This file was deleted.

tests/tests.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,29 @@ fn test_drain() {
306306
assert_eq!(&v[..], &[]);
307307
}
308308

309+
#[test]
310+
fn test_drain_range_inclusive() {
311+
let mut v = ArrayVec::from([0; 8]);
312+
v.drain(0..=7);
313+
assert_eq!(&v[..], &[]);
314+
315+
v.extend(0..);
316+
v.drain(1..=4);
317+
assert_eq!(&v[..], &[0, 5, 6, 7]);
318+
let u: ArrayVec<[_; 3]> = v.drain(1..=2).rev().collect();
319+
assert_eq!(&u[..], &[6, 5]);
320+
assert_eq!(&v[..], &[0, 7]);
321+
v.drain(..);
322+
assert_eq!(&v[..], &[]);
323+
}
324+
325+
#[test]
326+
#[should_panic]
327+
fn test_drain_range_inclusive_oob() {
328+
let mut v = ArrayVec::from([0; 0]);
329+
v.drain(0..=0);
330+
}
331+
309332
#[test]
310333
fn test_retain() {
311334
let mut v = ArrayVec::from([0; 8]);

0 commit comments

Comments
 (0)