Skip to content

Commit daf752d

Browse files
author
Ulrik Sverdrup
committed
drain range: Remove trait IntoCheckedRange
1 parent eecfd78 commit daf752d

File tree

1 file changed

+2
-87
lines changed

1 file changed

+2
-87
lines changed

text/0000-drain-range-2.md

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ elements, more efficently than any other safe API.
2828
- Implement `.drain()` for other collections. This is just like `.drain(..)` would be
2929
(drain the whole collection).
3030
- Ranged drain accepts all range types, currently .., a.., ..b, a..b,
31-
and drain will accept inclusive end ranges ("closed ranges") if they are implemented.
31+
and drain will accept inclusive end ranges ("closed ranges") when they are implemented.
3232
- Drain removes every element in the range.
3333
- Drain returns an iterator that produces the removed items by value.
3434
- Drain removes the whole range, regardless if you iterate the draining iterator
@@ -60,89 +60,6 @@ has other indexed methods (`.split_off()`).
6060
`BTreeMap` and `BTreeSet` should have arguments completely consistent the range
6161
method. This will be addressed separately.
6262

63-
## `IntoCheckedRange` trait
64-
65-
The existing trait `collections::range::RangeArgument` will be replaced by
66-
`IntoCheckedRange`, and will be used for `drain` methods that use a range
67-
parameter.
68-
69-
`IntoCheckedRange` is designed to allow bounds checking half-open and closed
70-
ranges. Bounds checking before conversion allows handling otherwise tricky
71-
extreme values correctly. It is an `unsafe trait` so that bounds checking can
72-
be trusted. Below is a sketched-out implementation.
73-
74-
```rust
75-
/// Convert `Self` into a half open `usize` range that slices
76-
/// a sequence indexed from 0 to `len`.
77-
/// Return `Err` with a faulty index if out of bounds.
78-
///
79-
/// Unsafe because: Implementation is trusted to bounds check correctly.
80-
pub unsafe trait IntoCheckedRange {
81-
fn into_checked_range(self, len: usize) -> Result<Range<usize>, usize>;
82-
}
83-
84-
unsafe impl IntoCheckedRange for RangeFull {
85-
#[inline]
86-
fn into_checked_range(self, len: usize) -> Result<Range<usize>, usize> {
87-
Ok(0..len)
88-
}
89-
}
90-
91-
unsafe impl IntoCheckedRange for RangeFrom<usize> {
92-
#[inline]
93-
fn into_checked_range(self, len: usize) -> Result<Range<usize>, usize> {
94-
if self.start <= len {
95-
Ok(self.start..len)
96-
} else { Err(self.start) }
97-
}
98-
}
99-
100-
unsafe impl IntoCheckedRange for RangeTo<usize> {
101-
#[inline]
102-
fn into_checked_range(self, len: usize) -> Result<Range<usize>, usize> {
103-
if self.end <= len {
104-
Ok(0..self.end)
105-
} else { Err(self.end) }
106-
}
107-
}
108-
109-
unsafe impl IntoCheckedRange for Range<usize> {
110-
#[inline]
111-
fn into_checked_range(self, len: usize) -> Result<Range<usize>, usize> {
112-
if self.start <= self.end && self.end <= len {
113-
Ok(self.start..self.end)
114-
} else { Err(cmp::max(self.start, self.end)) }
115-
}
116-
}
117-
118-
// For illustration, this is what a closed range impl would look like
119-
pub struct ClosedRangeSketch<T> {
120-
pub start: T,
121-
pub end: T,
122-
}
123-
124-
unsafe impl IntoCheckedRange for ClosedRangeSketch<usize> {
125-
fn into_checked_range(self, len: usize) -> Result<Range<usize>, usize> {
126-
if self.start <= self.end && self.end < len {
127-
Ok(self.start..self.end + 1)
128-
} else { Err(cmp::max(self.start, self.end)) }
129-
}
130-
}
131-
```
132-
133-
Example use of `IntoCheckedRange`:
134-
135-
```rust
136-
pub fn drain<R>(&mut self, range: R) -> Drain<A>
137-
where R: IntoCheckedRange
138-
{
139-
let remove_range = match range.into_checked_range(self.len()) {
140-
Err(i) => panic!("drain: Index {} is out of bounds", i),
141-
Ok(r) => r,
142-
};
143-
/* impl omitted */
144-
```
145-
14663
## Stabilization
14764

14865
The following can be stabilized as they are:
@@ -160,8 +77,6 @@ The following will be heading towards stabilization after changes:
16077

16178
- `VecDeque::drain`
16279

163-
The `IntoCheckedRange` trait will not be stabilized until we have closed ranges.
164-
16580
# Drawbacks
16681

16782
- Collections disagree on if they are drained with a range (`Vec`) or not (`HashMap`)
@@ -177,7 +92,7 @@ The `IntoCheckedRange` trait will not be stabilized until we have closed ranges.
17792

17893
```rust
17994
fn splice<R, I>(&mut self, range: R, iter: I) -> Splice<T>
180-
where R: IntoCheckedRange, I: IntoIterator<T>
95+
where R: RangeArgument, I: IntoIterator<T>
18196
```
18297

18398
if the method `.splice()` would both return an iterator of the replaced elements,

0 commit comments

Comments
 (0)