Skip to content

Commit 0c3c43c

Browse files
committed
Remove Splice struct return value from String::splice
1 parent a24e0f2 commit 0c3c43c

File tree

3 files changed

+25
-109
lines changed

3 files changed

+25
-109
lines changed

src/doc/unstable-book/src/library-features/splice.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ let mut s = String::from("α is alpha, β is beta");
1818
let beta_offset = s.find('β').unwrap_or(s.len());
1919

2020
// Replace the range up until the β from the string
21-
let t: String = s.splice(..beta_offset, "Α is capital alpha; ").collect();
22-
assert_eq!(t, "α is alpha, ");
21+
s.splice(..beta_offset, "Α is capital alpha; ");
2322
assert_eq!(s, "Α is capital alpha; β is beta");
2423
```

src/liballoc/string.rs

+19-90
Original file line numberDiff line numberDiff line change
@@ -1391,19 +1391,19 @@ impl String {
13911391
}
13921392

13931393
/// Creates a splicing iterator that removes the specified range in the string,
1394-
/// replaces with the given string, and yields the removed chars.
1395-
/// The given string doesnt need to be the same length as the range.
1394+
/// and replaces it with the given string.
1395+
/// The given string doesn't need to be the same length as the range.
13961396
///
1397-
/// Note: The element range is removed when the [`Splice`] is dropped,
1398-
/// even if the iterator is not consumed until the end.
1397+
/// Note: Unlike [`Vec::splice`], the replacement happens eagerly, and this
1398+
/// method does not return the removed chars.
13991399
///
14001400
/// # Panics
14011401
///
14021402
/// Panics if the starting point or end point do not lie on a [`char`]
14031403
/// boundary, or if they're out of bounds.
14041404
///
14051405
/// [`char`]: ../../std/primitive.char.html
1406-
/// [`Splice`]: ../../std/string/struct.Splice.html
1406+
/// [`Vec::splice`]: ../../std/vec/struct.Vec.html#method.splice
14071407
///
14081408
/// # Examples
14091409
///
@@ -1415,45 +1415,32 @@ impl String {
14151415
/// let beta_offset = s.find('β').unwrap_or(s.len());
14161416
///
14171417
/// // Replace the range up until the β from the string
1418-
/// let t: String = s.splice(..beta_offset, "Α is capital alpha; ").collect();
1419-
/// assert_eq!(t, "α is alpha, ");
1418+
/// s.splice(..beta_offset, "Α is capital alpha; ");
14201419
/// assert_eq!(s, "Α is capital alpha; β is beta");
14211420
/// ```
14221421
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
1423-
pub fn splice<'a, 'b, R>(&'a mut self, range: R, replace_with: &'b str) -> Splice<'a, 'b>
1422+
pub fn splice<R>(&mut self, range: R, replace_with: &str)
14241423
where R: RangeArgument<usize>
14251424
{
14261425
// Memory safety
14271426
//
14281427
// The String version of Splice does not have the memory safety issues
14291428
// of the vector version. The data is just plain bytes.
1430-
// Because the range removal happens in Drop, if the Splice iterator is leaked,
1431-
// the removal will not happen.
1432-
let len = self.len();
1433-
let start = match range.start() {
1434-
Included(&n) => n,
1435-
Excluded(&n) => n + 1,
1436-
Unbounded => 0,
1429+
1430+
match range.start() {
1431+
Included(&n) => assert!(self.is_char_boundary(n)),
1432+
Excluded(&n) => assert!(self.is_char_boundary(n + 1)),
1433+
Unbounded => {},
14371434
};
1438-
let end = match range.end() {
1439-
Included(&n) => n + 1,
1440-
Excluded(&n) => n,
1441-
Unbounded => len,
1435+
match range.end() {
1436+
Included(&n) => assert!(self.is_char_boundary(n + 1)),
1437+
Excluded(&n) => assert!(self.is_char_boundary(n)),
1438+
Unbounded => {},
14421439
};
14431440

1444-
// Take out two simultaneous borrows. The &mut String won't be accessed
1445-
// until iteration is over, in Drop.
1446-
let self_ptr = self as *mut _;
1447-
// slicing does the appropriate bounds checks
1448-
let chars_iter = self[start..end].chars();
1449-
1450-
Splice {
1451-
start,
1452-
end,
1453-
iter: chars_iter,
1454-
string: self_ptr,
1455-
replace_with,
1456-
}
1441+
unsafe {
1442+
self.as_mut_vec()
1443+
}.splice(range, replace_with.bytes());
14571444
}
14581445

14591446
/// Converts this `String` into a [`Box`]`<`[`str`]`>`.
@@ -2240,61 +2227,3 @@ impl<'a> DoubleEndedIterator for Drain<'a> {
22402227

22412228
#[unstable(feature = "fused", issue = "35602")]
22422229
impl<'a> FusedIterator for Drain<'a> {}
2243-
2244-
/// A splicing iterator for `String`.
2245-
///
2246-
/// This struct is created by the [`splice()`] method on [`String`]. See its
2247-
/// documentation for more.
2248-
///
2249-
/// [`splice()`]: struct.String.html#method.splice
2250-
/// [`String`]: struct.String.html
2251-
#[derive(Debug)]
2252-
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
2253-
pub struct Splice<'a, 'b> {
2254-
/// Will be used as &'a mut String in the destructor
2255-
string: *mut String,
2256-
/// Start of part to remove
2257-
start: usize,
2258-
/// End of part to remove
2259-
end: usize,
2260-
/// Current remaining range to remove
2261-
iter: Chars<'a>,
2262-
replace_with: &'b str,
2263-
}
2264-
2265-
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
2266-
unsafe impl<'a, 'b> Sync for Splice<'a, 'b> {}
2267-
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
2268-
unsafe impl<'a, 'b> Send for Splice<'a, 'b> {}
2269-
2270-
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
2271-
impl<'a, 'b> Drop for Splice<'a, 'b> {
2272-
fn drop(&mut self) {
2273-
unsafe {
2274-
let vec = (*self.string).as_mut_vec();
2275-
vec.splice(self.start..self.end, self.replace_with.bytes());
2276-
}
2277-
}
2278-
}
2279-
2280-
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
2281-
impl<'a, 'b> Iterator for Splice<'a, 'b> {
2282-
type Item = char;
2283-
2284-
#[inline]
2285-
fn next(&mut self) -> Option<char> {
2286-
self.iter.next()
2287-
}
2288-
2289-
fn size_hint(&self) -> (usize, Option<usize>) {
2290-
self.iter.size_hint()
2291-
}
2292-
}
2293-
2294-
#[unstable(feature = "splice", reason = "recently added", issue = "32310")]
2295-
impl<'a, 'b> DoubleEndedIterator for Splice<'a, 'b> {
2296-
#[inline]
2297-
fn next_back(&mut self) -> Option<char> {
2298-
self.iter.next_back()
2299-
}
2300-
}

src/liballoc/tests/string.rs

+5-17
Original file line numberDiff line numberDiff line change
@@ -442,9 +442,8 @@ fn test_drain() {
442442
#[test]
443443
fn test_splice() {
444444
let mut s = "Hello, world!".to_owned();
445-
let t: String = s.splice(7..12, "世界").collect();
445+
s.splice(7..12, "世界");
446446
assert_eq!(s, "Hello, 世界!");
447-
assert_eq!(t, "world");
448447
}
449448

450449
#[test]
@@ -457,12 +456,10 @@ fn test_splice_char_boundary() {
457456
#[test]
458457
fn test_splice_inclusive_range() {
459458
let mut v = String::from("12345");
460-
let t: String = v.splice(2...3, "789").collect();
459+
v.splice(2...3, "789");
461460
assert_eq!(v, "127895");
462-
assert_eq!(t, "34");
463-
let t2: String = v.splice(1...2, "A").collect();
461+
v.splice(1...2, "A");
464462
assert_eq!(v, "1A895");
465-
assert_eq!(t2, "27");
466463
}
467464

468465
#[test]
@@ -482,24 +479,15 @@ fn test_splice_inclusive_out_of_bounds() {
482479
#[test]
483480
fn test_splice_empty() {
484481
let mut s = String::from("12345");
485-
let t: String = s.splice(1..2, "").collect();
482+
s.splice(1..2, "");
486483
assert_eq!(s, "1345");
487-
assert_eq!(t, "2");
488484
}
489485

490486
#[test]
491487
fn test_splice_unbounded() {
492488
let mut s = String::from("12345");
493-
let t: String = s.splice(.., "").collect();
489+
s.splice(.., "");
494490
assert_eq!(s, "");
495-
assert_eq!(t, "12345");
496-
}
497-
498-
#[test]
499-
fn test_splice_forget() {
500-
let mut s = String::from("12345");
501-
::std::mem::forget(s.splice(2..4, "789"));
502-
assert_eq!(s, "12345");
503491
}
504492

505493
#[test]

0 commit comments

Comments
 (0)