1
- - Feature Name: replace-slice
1
+ - Feature Name: splice
2
2
- Start Date: 2015-12-28
3
3
- RFC PR:
4
4
- Rust Issue:
5
5
6
6
# Summary
7
7
[ summary ] : #summary
8
8
9
- Add a ` replace_slice ` method to ` Vec<T> ` and ` String ` removes a range of elements,
9
+ Add a ` splice ` method to ` Vec<T> ` and ` String ` removes a range of elements,
10
10
and replaces it in place with a given sequence of values.
11
11
The new sequence does not necessarily have the same length as the range it replaces.
12
12
@@ -50,12 +50,12 @@ use collections::range::RangeArgument;
50
50
use std :: ptr;
51
51
52
52
trait ReplaceVecSlice <T > {
53
- fn replace_slice <R , I >(& mut self , range : R , iterable : I )
53
+ fn splice <R , I >(& mut self , range : R , iterable : I )
54
54
where R : RangeArgument <usize >, I : IntoIterator <Item = T >, I :: IntoIter : ExactSizeIterator ;
55
55
}
56
56
57
57
impl <T > ReplaceVecSlice <T > for Vec <T > {
58
- fn replace_slice <R , I >(& mut self , range : R , iterable : I )
58
+ fn splice <R , I >(& mut self , range : R , iterable : I )
59
59
where R : RangeArgument <usize >, I : IntoIterator <Item = T >, I :: IntoIter : ExactSizeIterator
60
60
{
61
61
let len = self . len ();
@@ -117,11 +117,11 @@ impl<T> ReplaceVecSlice<T> for Vec<T> {
117
117
}
118
118
119
119
trait ReplaceStringSlice {
120
- fn replace_slice <R >(& mut self , range : R , s : & str ) where R : RangeArgument <usize >;
120
+ fn splice <R >(& mut self , range : R , s : & str ) where R : RangeArgument <usize >;
121
121
}
122
122
123
123
impl ReplaceStringSlice for String {
124
- fn replace_slice <R >(& mut self , range : R , s : & str ) where R : RangeArgument <usize > {
124
+ fn splice <R >(& mut self , range : R , s : & str ) where R : RangeArgument <usize > {
125
125
if let Some (& start ) = range . start () {
126
126
assert! (self . is_char_boundary (start ));
127
127
}
@@ -130,27 +130,27 @@ impl ReplaceStringSlice for String {
130
130
}
131
131
unsafe {
132
132
self . as_mut_vec ()
133
- }. replace_slice (range , s . bytes ())
133
+ }. splice (range , s . bytes ())
134
134
}
135
135
}
136
136
137
137
#[test]
138
138
fn it_works () {
139
139
let mut v = vec! [1 , 2 , 3 , 4 , 5 ];
140
- v . replace_slice (2 .. 4 , [10 , 11 , 12 ]. iter (). cloned ());
140
+ v . splice (2 .. 4 , [10 , 11 , 12 ]. iter (). cloned ());
141
141
assert_eq! (v , & [1 , 2 , 10 , 11 , 12 , 5 ]);
142
- v . replace_slice (1 .. 3 , Some (20 ));
142
+ v . splice (1 .. 3 , Some (20 ));
143
143
assert_eq! (v , & [1 , 20 , 11 , 12 , 5 ]);
144
144
let mut s = " Hello, world!" . to_owned ();
145
- s . replace_slice (7 .. , " 世界!" );
145
+ s . splice (7 .. , " 世界!" );
146
146
assert_eq! (s , " Hello, 世界!" );
147
147
}
148
148
149
149
#[test]
150
150
#[should_panic]
151
151
fn char_boundary () {
152
152
let mut s = " Hello, 世界!" . to_owned ();
153
- s . replace_slice (.. 8 , "" )
153
+ s . splice (.. 8 , "" )
154
154
}
155
155
```
156
156
@@ -184,20 +184,20 @@ not every program needs it, and standard library growth has a maintainance cost.
184
184
With ` ExactSizeIterator ` it only happens when ` ExactSizeIterator::len ` is incorrect
185
185
which means that someone is doing something wrong.
186
186
187
- * Alternatively, should ` replace_slice ` panic when ` ExactSizeIterator::len ` is incorrect?
187
+ * Alternatively, should ` splice ` panic when ` ExactSizeIterator::len ` is incorrect?
188
188
189
- * It would be nice to be able to ` Vec::replace_slice ` with a slice
189
+ * It would be nice to be able to ` Vec::splice ` with a slice
190
190
without writing ` .iter().cloned() ` explicitly.
191
191
This is possible with the same trick as for the ` Extend ` trait
192
192
([ RFC 839] ( https://github.com/rust-lang/rfcs/blob/master/text/0839-embrace-extend-extinguish.md ) ):
193
193
accept iterators of ` &T ` as well as iterators of ` T ` :
194
194
195
195
``` rust
196
196
impl <'a , T : 'a > ReplaceVecSlice <& 'a T > for Vec <T > where T : Copy {
197
- fn replace_slice <R , I >(& mut self , range : R , iterable : I )
197
+ fn splice <R , I >(& mut self , range : R , iterable : I )
198
198
where R : RangeArgument <usize >, I : IntoIterator <Item = & 'a T >, I :: IntoIter : ExactSizeIterator
199
199
{
200
- self . replace_slice (range , iterable . into_iter (). cloned ())
200
+ self . splice (range , iterable . into_iter (). cloned ())
201
201
}
202
202
}
203
203
```
@@ -206,11 +206,6 @@ not every program needs it, and standard library growth has a maintainance cost.
206
206
(By the way, what was the motivation for ` Extend ` being a trait rather than inherent methods,
207
207
before RFC 839?)
208
208
209
- * Naming.
210
- I accidentally typed ` replace_range ` instead of ` replace_slice ` several times
211
- while typing up this RFC.
212
- Update: I’m told ` splice ` is how this operation is called.
213
-
214
209
* The method could return an iterator of the replaced elements.
215
210
Nothing would happen when the method is called,
216
211
only when the returned iterator is advanced or dropped.
0 commit comments