@@ -28,7 +28,7 @@ elements, more efficently than any other safe API.
28
28
- Implement ` .drain() ` for other collections. This is just like ` .drain(..) ` would be
29
29
(drain the whole collection).
30
30
- 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.
32
32
- Drain removes every element in the range.
33
33
- Drain returns an iterator that produces the removed items by value.
34
34
- Drain removes the whole range, regardless if you iterate the draining iterator
@@ -60,89 +60,6 @@ has other indexed methods (`.split_off()`).
60
60
` BTreeMap ` and ` BTreeSet ` should have arguments completely consistent the range
61
61
method. This will be addressed separately.
62
62
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
-
146
63
## Stabilization
147
64
148
65
The following can be stabilized as they are:
@@ -160,8 +77,6 @@ The following will be heading towards stabilization after changes:
160
77
161
78
- ` VecDeque::drain `
162
79
163
- The ` IntoCheckedRange ` trait will not be stabilized until we have closed ranges.
164
-
165
80
# Drawbacks
166
81
167
82
- 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.
177
92
178
93
``` rust
179
94
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 >
181
96
```
182
97
183
98
if the method `. splice ()` would both return an iterator of the replaced elements ,
0 commit comments