From 79a93e7b03589374bed92985fec866fb72267099 Mon Sep 17 00:00:00 2001 From: Max Dexheimer Date: Thu, 10 Sep 2026 17:31:15 +0200 Subject: [PATCH] Make the last non-breaking box impl generalizations --- library/alloc/src/boxed/convert.rs | 12 ++++++------ library/alloc/src/bstr.rs | 5 +++-- library/alloc/src/ffi/c_str.rs | 5 +++-- library/std/src/ffi/os_str.rs | 5 +++-- library/std/src/path.rs | 5 +++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/library/alloc/src/boxed/convert.rs b/library/alloc/src/boxed/convert.rs index f5c16dc7e7040..526e8b18a8031 100644 --- a/library/alloc/src/boxed/convert.rs +++ b/library/alloc/src/boxed/convert.rs @@ -257,8 +257,8 @@ unsafe fn boxed_slice_as_array_unchecked( } #[stable(feature = "boxed_slice_try_from", since = "1.43.0")] -impl TryFrom> for Box<[T; N]> { - type Error = Box<[T]>; +impl TryFrom> for Box<[T; N], A> { + type Error = Box<[T], A>; /// Attempts to convert a `Box<[T]>` into a `Box<[T; N]>`. /// @@ -269,7 +269,7 @@ impl TryFrom> for Box<[T; N]> { /// /// Returns the old `Box<[T]>` in the `Err` variant if /// `boxed_slice.len()` does not equal `N`. - fn try_from(boxed_slice: Box<[T]>) -> Result { + fn try_from(boxed_slice: Box<[T], A>) -> Result { if boxed_slice.len() == N { // SAFETY: Checked length. Ok(unsafe { boxed_slice_as_array_unchecked(boxed_slice) }) @@ -281,8 +281,8 @@ impl TryFrom> for Box<[T; N]> { #[cfg(not(no_global_oom_handling))] #[stable(feature = "boxed_array_try_from_vec", since = "1.66.0")] -impl TryFrom> for Box<[T; N]> { - type Error = Vec; +impl TryFrom> for Box<[T; N], A> { + type Error = Vec; /// Attempts to convert a `Vec` into a `Box<[T; N]>`. /// @@ -302,7 +302,7 @@ impl TryFrom> for Box<[T; N]> { /// let state: Box<[f32; 100]> = vec![1.0; 100].try_into().unwrap(); /// assert_eq!(state.len(), 100); /// ``` - fn try_from(vec: Vec) -> Result { + fn try_from(vec: Vec) -> Result { if vec.len() == N { let boxed_slice = vec.into_boxed_slice(); // SAFETY: Checked length. diff --git a/library/alloc/src/bstr.rs b/library/alloc/src/bstr.rs index 8d993f6758a52..59909ed1db9b2 100644 --- a/library/alloc/src/bstr.rs +++ b/library/alloc/src/bstr.rs @@ -15,6 +15,7 @@ use core::ops::{ use core::str::{FromStr, Utf8Error}; use core::{fmt, hash}; +use crate::alloc::Allocator; use crate::borrow::{Cow, ToOwned}; use crate::boxed::Box; #[cfg(not(no_rc))] @@ -597,10 +598,10 @@ impl<'a> TryFrom<&'a ByteString> for &'a str { // Additional impls for `ByteStr` that require types from `alloc`: #[unstable(feature = "bstr", issue = "134915")] -impl Clone for Box { +impl Clone for Box { #[inline] fn clone(&self) -> Self { - Self::from(Box::<[u8]>::from(&self.0)) + Box::clone_from_ref_in(&**self, Self::allocator(self).clone()) } } diff --git a/library/alloc/src/ffi/c_str.rs b/library/alloc/src/ffi/c_str.rs index 78158cad394c6..d13808ee3d21c 100644 --- a/library/alloc/src/ffi/c_str.rs +++ b/library/alloc/src/ffi/c_str.rs @@ -7,6 +7,7 @@ use core::slice::memchr; use core::str::{self, FromStr, Utf8Error}; use core::{fmt, mem, ops, ptr, slice}; +use crate::alloc::Allocator; use crate::borrow::{Cow, ToOwned}; use crate::boxed::Box; use crate::rc::Rc; @@ -863,10 +864,10 @@ impl TryFrom for String { } #[stable(feature = "more_box_slice_clone", since = "1.29.0")] -impl Clone for Box { +impl Clone for Box { #[inline] fn clone(&self) -> Self { - (**self).into() + Box::clone_from_ref_in(&**self, Self::allocator(self).clone()) } } diff --git a/library/std/src/ffi/os_str.rs b/library/std/src/ffi/os_str.rs index 6be8f775b441d..b9d344284734b 100644 --- a/library/std/src/ffi/os_str.rs +++ b/library/std/src/ffi/os_str.rs @@ -5,6 +5,7 @@ mod tests; use core::clone::CloneToUninit; +use crate::alloc::Allocator; use crate::borrow::{Borrow, Cow}; use crate::collections::TryReserveError; use crate::hash::{Hash, Hasher}; @@ -1395,10 +1396,10 @@ impl From for Box { } #[stable(feature = "more_box_slice_clone", since = "1.29.0")] -impl Clone for Box { +impl Clone for Box { #[inline] fn clone(&self) -> Self { - self.to_os_string().into_boxed_os_str() + Box::clone_from_ref_in(&**self, Self::allocator(self).clone()) } } diff --git a/library/std/src/path.rs b/library/std/src/path.rs index 141bc72fb2e42..08d14b70638a8 100644 --- a/library/std/src/path.rs +++ b/library/std/src/path.rs @@ -83,6 +83,7 @@ use core::clone::CloneToUninit; +use crate::alloc::Allocator; use crate::borrow::{Borrow, Cow}; use crate::collections::TryReserveError; use crate::error::Error; @@ -1972,10 +1973,10 @@ impl From for Box { } #[stable(feature = "more_box_slice_clone", since = "1.29.0")] -impl Clone for Box { +impl Clone for Box { #[inline] fn clone(&self) -> Self { - self.to_path_buf().into_boxed_path() + Box::clone_from_ref_in(&**self, Self::allocator(self).clone()) } }