@@ -1392,19 +1392,19 @@ impl String {
1392
1392
}
1393
1393
1394
1394
/// Creates a splicing iterator that removes the specified range in the string,
1395
- /// replaces with the given string, and yields the removed chars .
1396
- /// The given string doesn’ t need to be the same length as the range.
1395
+ /// and replaces it with the given string.
1396
+ /// The given string doesn' t need to be the same length as the range.
1397
1397
///
1398
- /// Note: The element range is removed when the [`Splice`] is dropped,
1399
- /// even if the iterator is not consumed until the end .
1398
+ /// Note: Unlike [`Vec::splice`], the replacement happens eagerly, and this
1399
+ /// method does not return the removed chars .
1400
1400
///
1401
1401
/// # Panics
1402
1402
///
1403
1403
/// Panics if the starting point or end point do not lie on a [`char`]
1404
1404
/// boundary, or if they're out of bounds.
1405
1405
///
1406
1406
/// [`char`]: ../../std/primitive.char.html
1407
- /// [`Splice `]: ../../std/string /struct.Splice .html
1407
+ /// [`Vec::splice `]: ../../std/vec /struct.Vec .html#method.splice
1408
1408
///
1409
1409
/// # Examples
1410
1410
///
@@ -1416,45 +1416,32 @@ impl String {
1416
1416
/// let beta_offset = s.find('β').unwrap_or(s.len());
1417
1417
///
1418
1418
/// // Replace the range up until the β from the string
1419
- /// let t: String = s.splice(..beta_offset, "Α is capital alpha; ").collect();
1420
- /// assert_eq!(t, "α is alpha, ");
1419
+ /// s.splice(..beta_offset, "Α is capital alpha; ");
1421
1420
/// assert_eq!(s, "Α is capital alpha; β is beta");
1422
1421
/// ```
1423
1422
#[ unstable( feature = "splice" , reason = "recently added" , issue = "32310" ) ]
1424
- pub fn splice < ' a , ' b , R > ( & ' a mut self , range : R , replace_with : & ' b str ) -> Splice < ' a , ' b >
1423
+ pub fn splice < R > ( & mut self , range : R , replace_with : & str )
1425
1424
where R : RangeArgument < usize >
1426
1425
{
1427
1426
// Memory safety
1428
1427
//
1429
1428
// The String version of Splice does not have the memory safety issues
1430
1429
// of the vector version. The data is just plain bytes.
1431
- // Because the range removal happens in Drop, if the Splice iterator is leaked,
1432
- // the removal will not happen.
1433
- let len = self . len ( ) ;
1434
- let start = match range. start ( ) {
1435
- Included ( & n) => n,
1436
- Excluded ( & n) => n + 1 ,
1437
- Unbounded => 0 ,
1430
+
1431
+ match range. start ( ) {
1432
+ Included ( & n) => assert ! ( self . is_char_boundary( n) ) ,
1433
+ Excluded ( & n) => assert ! ( self . is_char_boundary( n + 1 ) ) ,
1434
+ Unbounded => { } ,
1438
1435
} ;
1439
- let end = match range. end ( ) {
1440
- Included ( & n) => n + 1 ,
1441
- Excluded ( & n) => n ,
1442
- Unbounded => len ,
1436
+ match range. end ( ) {
1437
+ Included ( & n) => assert ! ( self . is_char_boundary ( n + 1 ) ) ,
1438
+ Excluded ( & n) => assert ! ( self . is_char_boundary ( n ) ) ,
1439
+ Unbounded => { } ,
1443
1440
} ;
1444
1441
1445
- // Take out two simultaneous borrows. The &mut String won't be accessed
1446
- // until iteration is over, in Drop.
1447
- let self_ptr = self as * mut _ ;
1448
- // slicing does the appropriate bounds checks
1449
- let chars_iter = self [ start..end] . chars ( ) ;
1450
-
1451
- Splice {
1452
- start,
1453
- end,
1454
- iter : chars_iter,
1455
- string : self_ptr,
1456
- replace_with,
1457
- }
1442
+ unsafe {
1443
+ self . as_mut_vec ( )
1444
+ } . splice ( range, replace_with. bytes ( ) ) ;
1458
1445
}
1459
1446
1460
1447
/// Converts this `String` into a [`Box`]`<`[`str`]`>`.
@@ -2241,61 +2228,3 @@ impl<'a> DoubleEndedIterator for Drain<'a> {
2241
2228
2242
2229
#[ unstable( feature = "fused" , issue = "35602" ) ]
2243
2230
impl < ' a > FusedIterator for Drain < ' a > { }
2244
-
2245
- /// A splicing iterator for `String`.
2246
- ///
2247
- /// This struct is created by the [`splice()`] method on [`String`]. See its
2248
- /// documentation for more.
2249
- ///
2250
- /// [`splice()`]: struct.String.html#method.splice
2251
- /// [`String`]: struct.String.html
2252
- #[ derive( Debug ) ]
2253
- #[ unstable( feature = "splice" , reason = "recently added" , issue = "32310" ) ]
2254
- pub struct Splice < ' a , ' b > {
2255
- /// Will be used as &'a mut String in the destructor
2256
- string : * mut String ,
2257
- /// Start of part to remove
2258
- start : usize ,
2259
- /// End of part to remove
2260
- end : usize ,
2261
- /// Current remaining range to remove
2262
- iter : Chars < ' a > ,
2263
- replace_with : & ' b str ,
2264
- }
2265
-
2266
- #[ unstable( feature = "splice" , reason = "recently added" , issue = "32310" ) ]
2267
- unsafe impl < ' a , ' b > Sync for Splice < ' a , ' b > { }
2268
- #[ unstable( feature = "splice" , reason = "recently added" , issue = "32310" ) ]
2269
- unsafe impl < ' a , ' b > Send for Splice < ' a , ' b > { }
2270
-
2271
- #[ unstable( feature = "splice" , reason = "recently added" , issue = "32310" ) ]
2272
- impl < ' a , ' b > Drop for Splice < ' a , ' b > {
2273
- fn drop ( & mut self ) {
2274
- unsafe {
2275
- let vec = ( * self . string ) . as_mut_vec ( ) ;
2276
- vec. splice ( self . start ..self . end , self . replace_with . bytes ( ) ) ;
2277
- }
2278
- }
2279
- }
2280
-
2281
- #[ unstable( feature = "splice" , reason = "recently added" , issue = "32310" ) ]
2282
- impl < ' a , ' b > Iterator for Splice < ' a , ' b > {
2283
- type Item = char ;
2284
-
2285
- #[ inline]
2286
- fn next ( & mut self ) -> Option < char > {
2287
- self . iter . next ( )
2288
- }
2289
-
2290
- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2291
- self . iter . size_hint ( )
2292
- }
2293
- }
2294
-
2295
- #[ unstable( feature = "splice" , reason = "recently added" , issue = "32310" ) ]
2296
- impl < ' a , ' b > DoubleEndedIterator for Splice < ' a , ' b > {
2297
- #[ inline]
2298
- fn next_back ( & mut self ) -> Option < char > {
2299
- self . iter . next_back ( )
2300
- }
2301
- }
0 commit comments