Skip to content

Commit

Permalink
Rollup merge of rust-lang#126970 - DaniPopes:simplify-str-clone_into,…
Browse files Browse the repository at this point in the history
… r=cuviper

Simplify `str::clone_into`

Removes an `unsafe` in favor of just using `String` methods.
  • Loading branch information
matthiaskrgr authored Jun 26, 2024
2 parents edb0168 + d5ff4f4 commit aa47a8e
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions library/alloc/src/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,15 +206,16 @@ impl BorrowMut<str> for String {
#[stable(feature = "rust1", since = "1.0.0")]
impl ToOwned for str {
type Owned = String;

#[inline]
fn to_owned(&self) -> String {
unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
}

#[inline]
fn clone_into(&self, target: &mut String) {
let mut b = mem::take(target).into_bytes();
self.as_bytes().clone_into(&mut b);
*target = unsafe { String::from_utf8_unchecked(b) }
target.clear();
target.push_str(self);
}
}

Expand Down

0 comments on commit aa47a8e

Please sign in to comment.