Skip to content

Commit 316d54a

Browse files
committed
Implement some conversions using Box::clone_from_ref
1 parent 9e497b6 commit 316d54a

File tree

6 files changed

+7
-57
lines changed

6 files changed

+7
-57
lines changed

library/alloc/src/boxed/convert.rs

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
11
use core::any::Any;
2-
#[cfg(not(no_global_oom_handling))]
3-
use core::clone::TrivialClone;
42
use core::error::Error;
3+
#[cfg(not(no_global_oom_handling))]
4+
use core::fmt;
55
use core::mem;
66
use core::pin::Pin;
7-
#[cfg(not(no_global_oom_handling))]
8-
use core::{fmt, ptr};
97

108
use crate::alloc::Allocator;
119
#[cfg(not(no_global_oom_handling))]
1210
use crate::borrow::Cow;
1311
use crate::boxed::Box;
1412
#[cfg(not(no_global_oom_handling))]
15-
use crate::raw_vec::RawVec;
16-
#[cfg(not(no_global_oom_handling))]
17-
use crate::str::from_boxed_utf8_unchecked;
18-
#[cfg(not(no_global_oom_handling))]
1913
use crate::string::String;
2014
#[cfg(not(no_global_oom_handling))]
2115
use crate::vec::Vec;
@@ -62,35 +56,6 @@ where
6256
}
6357
}
6458

65-
/// Specialization trait used for `From<&[T]>`.
66-
#[cfg(not(no_global_oom_handling))]
67-
trait BoxFromSlice<T> {
68-
fn from_slice(slice: &[T]) -> Self;
69-
}
70-
71-
#[cfg(not(no_global_oom_handling))]
72-
impl<T: Clone> BoxFromSlice<T> for Box<[T]> {
73-
#[inline]
74-
default fn from_slice(slice: &[T]) -> Self {
75-
slice.to_vec().into_boxed_slice()
76-
}
77-
}
78-
79-
#[cfg(not(no_global_oom_handling))]
80-
impl<T: TrivialClone> BoxFromSlice<T> for Box<[T]> {
81-
#[inline]
82-
fn from_slice(slice: &[T]) -> Self {
83-
let len = slice.len();
84-
let buf = RawVec::with_capacity(len);
85-
// SAFETY: since `T` implements `TrivialClone`, this is sound and
86-
// equivalent to the above.
87-
unsafe {
88-
ptr::copy_nonoverlapping(slice.as_ptr(), buf.ptr(), len);
89-
buf.into_box(slice.len()).assume_init()
90-
}
91-
}
92-
}
93-
9459
#[cfg(not(no_global_oom_handling))]
9560
#[stable(feature = "box_from_slice", since = "1.17.0")]
9661
impl<T: Clone> From<&[T]> for Box<[T]> {
@@ -109,7 +74,7 @@ impl<T: Clone> From<&[T]> for Box<[T]> {
10974
/// ```
11075
#[inline]
11176
fn from(slice: &[T]) -> Box<[T]> {
112-
<Self as BoxFromSlice<T>>::from_slice(slice)
77+
Box::clone_from_ref(slice)
11378
}
11479
}
11580

@@ -170,7 +135,7 @@ impl From<&str> for Box<str> {
170135
/// ```
171136
#[inline]
172137
fn from(s: &str) -> Box<str> {
173-
unsafe { from_boxed_utf8_unchecked(Box::from(s.as_bytes())) }
138+
Box::clone_from_ref(s)
174139
}
175140
}
176141

library/alloc/src/ffi/c_str.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -766,8 +766,7 @@ impl From<&CStr> for Box<CStr> {
766766
/// Converts a `&CStr` into a `Box<CStr>`,
767767
/// by copying the contents into a newly allocated [`Box`].
768768
fn from(s: &CStr) -> Box<CStr> {
769-
let boxed: Box<[u8]> = Box::from(s.to_bytes_with_nul());
770-
unsafe { Box::from_raw(Box::into_raw(boxed) as *mut CStr) }
769+
Box::clone_from_ref(s)
771770
}
772771
}
773772

library/std/src/ffi/os_str.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,8 +1285,7 @@ impl From<&OsStr> for Box<OsStr> {
12851285
/// Copies the string into a newly allocated <code>[Box]&lt;[OsStr]&gt;</code>.
12861286
#[inline]
12871287
fn from(s: &OsStr) -> Box<OsStr> {
1288-
let rw = Box::into_raw(s.inner.into_box()) as *mut OsStr;
1289-
unsafe { Box::from_raw(rw) }
1288+
Box::clone_from_ref(s)
12901289
}
12911290
}
12921291

library/std/src/path.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1877,9 +1877,7 @@ impl From<&Path> for Box<Path> {
18771877
///
18781878
/// This will allocate and clone `path` to it.
18791879
fn from(path: &Path) -> Box<Path> {
1880-
let boxed: Box<OsStr> = path.inner.into();
1881-
let rw = Box::into_raw(boxed) as *mut Path;
1882-
unsafe { Box::from_raw(rw) }
1880+
Box::clone_from_ref(path)
18831881
}
18841882
}
18851883

library/std/src/sys/os_str/bytes.rs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -321,12 +321,6 @@ impl Slice {
321321
self.inner.clone_into(&mut buf.inner)
322322
}
323323

324-
#[inline]
325-
pub fn into_box(&self) -> Box<Slice> {
326-
let boxed: Box<[u8]> = self.inner.into();
327-
unsafe { mem::transmute(boxed) }
328-
}
329-
330324
#[inline]
331325
pub fn empty_box() -> Box<Slice> {
332326
let boxed: Box<[u8]> = Default::default();

library/std/src/sys/os_str/wtf8.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,11 +271,6 @@ impl Slice {
271271
self.inner.clone_into(&mut buf.inner)
272272
}
273273

274-
#[inline]
275-
pub fn into_box(&self) -> Box<Slice> {
276-
unsafe { mem::transmute(self.inner.into_box()) }
277-
}
278-
279274
#[inline]
280275
pub fn empty_box() -> Box<Slice> {
281276
unsafe { mem::transmute(Wtf8::empty_box()) }

0 commit comments

Comments
 (0)