]> git.lizzy.rs Git - rust.git/commitdiff
Rollup merge of #34890 - oconnor663:addassign, r=brson
authorGuillaume Gomez <guillaume1.gomez@gmail.com>
Thu, 21 Jul 2016 09:27:00 +0000 (11:27 +0200)
committerGitHub <noreply@github.com>
Thu, 21 Jul 2016 09:27:00 +0000 (11:27 +0200)
implement AddAssign for String

Currently `String` implements `Add` but not `AddAssign`. This PR fills in that gap.

I played around with having `AddAssign` (and `Add` and `push_str`) take `AsRef<str>` instead of `&str`, but it looks like that breaks arguments that implement `Deref<Target=str>` and not `AsRef<str>`. Comments in [`libcore/convert.rs`](https://github.com/rust-lang/rust/blob/master/src/libcore/convert.rs#L207-L213) make it sound like we could fix this with a blanket impl eventually. Does anyone know what's blocking that?

1  2 
src/libcollections/string.rs

index 8ba5c6ffbf2ebd2c13c5f43c4690849031d9ba0e,71c6a6c892eabe7917fc53b963b4f5f1098c16bb..a2d1f09b6a5aef08d425f02ebd4a3669597ad8ca
@@@ -59,7 -59,7 +59,7 @@@ use core::fmt
  use core::hash;
  use core::iter::FromIterator;
  use core::mem;
- use core::ops::{self, Add, Index, IndexMut};
+ use core::ops::{self, Add, AddAssign, Index, IndexMut};
  use core::ptr;
  use core::str::pattern::Pattern;
  use rustc_unicode::char::{decode_utf16, REPLACEMENT_CHARACTER};
@@@ -701,12 -701,6 +701,12 @@@ impl String 
      /// Violating these may cause problems like corrupting the allocator's
      /// internal datastructures.
      ///
 +    /// The ownership of `ptr` is effectively transferred to the
 +    /// `String` which may then deallocate, reallocate or change the
 +    /// contents of memory pointed to by the pointer at will. Ensure
 +    /// that nothing else uses the pointer after calling this
 +    /// function.
 +    ///
      /// # Examples
      ///
      /// Basic usage:
@@@ -1565,6 -1559,14 +1565,14 @@@ impl<'a> Add<&'a str> for String 
      }
  }
  
+ #[stable(feature = "stringaddassign", since = "1.12.0")]
+ impl<'a> AddAssign<&'a str> for String {
+     #[inline]
+     fn add_assign(&mut self, other: &str) {
+         self.push_str(other);
+     }
+ }
  #[stable(feature = "rust1", since = "1.0.0")]
  impl ops::Index<ops::Range<usize>> for String {
      type Output = str;