Skip to content

Commit 087bb92

Browse files
committed
replace_range: rename to splice.
1 parent 5a44898 commit 087bb92

File tree

1 file changed

+15
-20
lines changed

1 file changed

+15
-20
lines changed

text/0000-replace-slice.md

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
- Feature Name: replace-slice
1+
- Feature Name: splice
22
- Start Date: 2015-12-28
33
- RFC PR:
44
- Rust Issue:
55

66
# Summary
77
[summary]: #summary
88

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,
1010
and replaces it in place with a given sequence of values.
1111
The new sequence does not necessarily have the same length as the range it replaces.
1212

@@ -50,12 +50,12 @@ use collections::range::RangeArgument;
5050
use std::ptr;
5151

5252
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)
5454
where R: RangeArgument<usize>, I: IntoIterator<Item=T>, I::IntoIter: ExactSizeIterator;
5555
}
5656

5757
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)
5959
where R: RangeArgument<usize>, I: IntoIterator<Item=T>, I::IntoIter: ExactSizeIterator
6060
{
6161
let len = self.len();
@@ -117,11 +117,11 @@ impl<T> ReplaceVecSlice<T> for Vec<T> {
117117
}
118118

119119
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>;
121121
}
122122

123123
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> {
125125
if let Some(&start) = range.start() {
126126
assert!(self.is_char_boundary(start));
127127
}
@@ -130,27 +130,27 @@ impl ReplaceStringSlice for String {
130130
}
131131
unsafe {
132132
self.as_mut_vec()
133-
}.replace_slice(range, s.bytes())
133+
}.splice(range, s.bytes())
134134
}
135135
}
136136

137137
#[test]
138138
fn it_works() {
139139
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());
141141
assert_eq!(v, &[1, 2, 10, 11, 12, 5]);
142-
v.replace_slice(1..3, Some(20));
142+
v.splice(1..3, Some(20));
143143
assert_eq!(v, &[1, 20, 11, 12, 5]);
144144
let mut s = "Hello, world!".to_owned();
145-
s.replace_slice(7.., "世界!");
145+
s.splice(7.., "世界!");
146146
assert_eq!(s, "Hello, 世界!");
147147
}
148148

149149
#[test]
150150
#[should_panic]
151151
fn char_boundary() {
152152
let mut s = "Hello, 世界!".to_owned();
153-
s.replace_slice(..8, "")
153+
s.splice(..8, "")
154154
}
155155
```
156156

@@ -184,20 +184,20 @@ not every program needs it, and standard library growth has a maintainance cost.
184184
With `ExactSizeIterator` it only happens when `ExactSizeIterator::len` is incorrect
185185
which means that someone is doing something wrong.
186186

187-
* Alternatively, should `replace_slice` panic when `ExactSizeIterator::len` is incorrect?
187+
* Alternatively, should `splice` panic when `ExactSizeIterator::len` is incorrect?
188188

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
190190
without writing `.iter().cloned()` explicitly.
191191
This is possible with the same trick as for the `Extend` trait
192192
([RFC 839](https://github.com/rust-lang/rfcs/blob/master/text/0839-embrace-extend-extinguish.md)):
193193
accept iterators of `&T` as well as iterators of `T`:
194194

195195
```rust
196196
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)
198198
where R: RangeArgument<usize>, I: IntoIterator<Item=&'a T>, I::IntoIter: ExactSizeIterator
199199
{
200-
self.replace_slice(range, iterable.into_iter().cloned())
200+
self.splice(range, iterable.into_iter().cloned())
201201
}
202202
}
203203
```
@@ -206,11 +206,6 @@ not every program needs it, and standard library growth has a maintainance cost.
206206
(By the way, what was the motivation for `Extend` being a trait rather than inherent methods,
207207
before RFC 839?)
208208

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-
214209
* The method could return an iterator of the replaced elements.
215210
Nothing would happen when the method is called,
216211
only when the returned iterator is advanced or dropped.

0 commit comments

Comments
 (0)