From 0351c5316c0b2627a388d87bb76acdf33a0f1654 Mon Sep 17 00:00:00 2001 From: DaniPopes <57450786+DaniPopes@users.noreply.github.com> Date: Tue, 25 Jun 2024 22:34:40 +0200 Subject: [PATCH] Simplify `str::clone_into` Removes an `unsafe` in favor of just using `String` methods. --- alloc/src/str.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/alloc/src/str.rs b/alloc/src/str.rs index 3e23612d0c13c..3bb808a6c73ab 100644 --- a/alloc/src/str.rs +++ b/alloc/src/str.rs @@ -206,15 +206,16 @@ impl BorrowMut 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); } }