Skip to content

Commit 1f1a3b4

Browse files
committed
move WriteCloneIntoRaw into alloc::alloc
1 parent f89f30f commit 1f1a3b4

File tree

4 files changed

+31
-27
lines changed

4 files changed

+31
-27
lines changed

library/alloc/src/alloc.rs

+23
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,26 @@ pub mod __alloc_error_handler {
397397
unsafe { oom_impl(layout) }
398398
}
399399
}
400+
401+
/// Specialize clones into pre-allocated, uninitialized memory.
402+
/// Used by `Box::clone` and `Rc`/`Arc::make_mut`.
403+
pub(crate) trait WriteCloneIntoRaw: Sized {
404+
unsafe fn write_clone_into_raw(&self, target: *mut Self);
405+
}
406+
407+
impl<T: Clone> WriteCloneIntoRaw for T {
408+
#[inline]
409+
default unsafe fn write_clone_into_raw(&self, target: *mut Self) {
410+
// Having allocated *first* may allow the optimizer to create
411+
// the cloned value in-place, skipping the local and move.
412+
unsafe { target.write(self.clone()) };
413+
}
414+
}
415+
416+
impl<T: Copy> WriteCloneIntoRaw for T {
417+
#[inline]
418+
unsafe fn write_clone_into_raw(&self, target: *mut Self) {
419+
// We can always copy in-place, without ever involving a local value.
420+
unsafe { target.copy_from_nonoverlapping(self, 1) };
421+
}
422+
}

library/alloc/src/boxed.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ use core::pin::Pin;
151151
use core::ptr::{self, Unique};
152152
use core::task::{Context, Poll};
153153

154-
use crate::alloc::{handle_alloc_error, AllocError, Allocator, Global, Layout};
154+
use crate::alloc::{handle_alloc_error, AllocError, Allocator, Global, Layout, WriteCloneIntoRaw};
155155
use crate::borrow::Cow;
156156
use crate::raw_vec::RawVec;
157157
use crate::str::from_boxed_utf8_unchecked;
@@ -1047,28 +1047,6 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> {
10471047
}
10481048
}
10491049

1050-
/// Specialize clones into pre-allocated, uninitialized memory.
1051-
pub(crate) trait WriteCloneIntoRaw: Sized {
1052-
unsafe fn write_clone_into_raw(&self, target: *mut Self);
1053-
}
1054-
1055-
impl<T: Clone> WriteCloneIntoRaw for T {
1056-
#[inline]
1057-
default unsafe fn write_clone_into_raw(&self, target: *mut Self) {
1058-
// Having allocated *first* may allow the optimizer to create
1059-
// the cloned value in-place, skipping the local and move.
1060-
unsafe { target.write(self.clone()) };
1061-
}
1062-
}
1063-
1064-
impl<T: Copy> WriteCloneIntoRaw for T {
1065-
#[inline]
1066-
unsafe fn write_clone_into_raw(&self, target: *mut Self) {
1067-
// We can always copy in-place, without ever involving a local value.
1068-
unsafe { target.copy_from_nonoverlapping(self, 1) };
1069-
}
1070-
}
1071-
10721050
#[stable(feature = "box_slice_clone", since = "1.3.0")]
10731051
impl Clone for Box<str> {
10741052
fn clone(&self) -> Self {

library/alloc/src/rc.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,10 @@ use core::pin::Pin;
263263
use core::ptr::{self, NonNull};
264264
use core::slice::from_raw_parts_mut;
265265

266-
use crate::alloc::{box_free, handle_alloc_error, AllocError, Allocator, Global, Layout};
266+
use crate::alloc::{
267+
box_free, handle_alloc_error, AllocError, Allocator, Global, Layout, WriteCloneIntoRaw,
268+
};
267269
use crate::borrow::{Cow, ToOwned};
268-
use crate::boxed::WriteCloneIntoRaw;
269270
use crate::string::String;
270271
use crate::vec::Vec;
271272

library/alloc/src/sync.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ use core::slice::from_raw_parts_mut;
2222
use core::sync::atomic;
2323
use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst};
2424

25-
use crate::alloc::{box_free, handle_alloc_error, AllocError, Allocator, Global, Layout};
25+
use crate::alloc::{
26+
box_free, handle_alloc_error, AllocError, Allocator, Global, Layout, WriteCloneIntoRaw,
27+
};
2628
use crate::borrow::{Cow, ToOwned};
27-
use crate::boxed::{Box, WriteCloneIntoRaw};
29+
use crate::boxed::Box;
2830
use crate::rc::is_dangling;
2931
use crate::string::String;
3032
use crate::vec::Vec;

0 commit comments

Comments
 (0)