From 9b0380f268b6e6a7d01022683ee2b27ee1250a9d Mon Sep 17 00:00:00 2001 From: Serial <69764315+Serial-ATA@users.noreply.github.com> Date: Sat, 9 Apr 2022 12:01:04 -0400 Subject: [PATCH] Simplify `String::from_utf16` --- library/alloc/src/string.rs | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/library/alloc/src/string.rs b/library/alloc/src/string.rs index 71b6b9b41f5c5..8ef25b86cdb4c 100644 --- a/library/alloc/src/string.rs +++ b/library/alloc/src/string.rs @@ -607,17 +607,7 @@ impl String { #[cfg(not(no_global_oom_handling))] #[stable(feature = "rust1", since = "1.0.0")] pub fn from_utf16(v: &[u16]) -> Result { - // This isn't done via collect::>() for performance reasons. - // FIXME: the function can be simplified again when #48994 is closed. - let mut ret = String::with_capacity(v.len()); - for c in decode_utf16(v.iter().cloned()) { - if let Ok(c) = c { - ret.push(c); - } else { - return Err(FromUtf16Error(())); - } - } - Ok(ret) + decode_utf16(v.iter().cloned()).collect::>().map_err(|_| FromUtf16Error(())) } /// Decode a UTF-16–encoded slice `v` into a `String`, replacing