Skip to content

Commit

Permalink
Simplify str::clone_into
Browse files Browse the repository at this point in the history
Removes an `unsafe` in favor of just using `String` methods.
  • Loading branch information
DaniPopes committed Jun 25, 2024
1 parent a451b2a commit 0351c53
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions 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 0351c53

Please sign in to comment.