Rollup merge of #126970 - DaniPopes:simplify-str-clone_into, r=cuviper

Simplify `str::clone_into`

Removes an `unsafe` in favor of just using `String` methods.
This commit is contained in:
Matthias Krüger 2024-06-28 08:34:09 +02:00 committed by GitHub
commit 2c228260dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -206,15 +206,16 @@ fn borrow_mut(&mut self) -> &mut str {
#[stable(feature = "rust1", since = "1.0.0")] #[stable(feature = "rust1", since = "1.0.0")]
impl ToOwned for str { impl ToOwned for str {
type Owned = String; type Owned = String;
#[inline] #[inline]
fn to_owned(&self) -> String { fn to_owned(&self) -> String {
unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) } unsafe { String::from_utf8_unchecked(self.as_bytes().to_owned()) }
} }
#[inline]
fn clone_into(&self, target: &mut String) { fn clone_into(&self, target: &mut String) {
let mut b = mem::take(target).into_bytes(); target.clear();
self.as_bytes().clone_into(&mut b); target.push_str(self);
*target = unsafe { String::from_utf8_unchecked(b) }
} }
} }