diff options
Diffstat (limited to 'library')
| -rw-r--r-- | library/alloc/src/alloc.rs | 23 | ||||
| -rw-r--r-- | library/alloc/src/boxed.rs | 24 | ||||
| -rw-r--r-- | library/alloc/src/rc.rs | 5 | ||||
| -rw-r--r-- | library/alloc/src/sync.rs | 6 |
4 files changed, 31 insertions, 27 deletions
diff --git a/library/alloc/src/alloc.rs b/library/alloc/src/alloc.rs index 4fbcc4590f1..cb9daaea000 100644 --- a/library/alloc/src/alloc.rs +++ b/library/alloc/src/alloc.rs @@ -397,3 +397,26 @@ pub mod __alloc_error_handler { unsafe { oom_impl(layout) } } } + +/// Specialize clones into pre-allocated, uninitialized memory. +/// Used by `Box::clone` and `Rc`/`Arc::make_mut`. +pub(crate) trait WriteCloneIntoRaw: Sized { + unsafe fn write_clone_into_raw(&self, target: *mut Self); +} + +impl<T: Clone> WriteCloneIntoRaw for T { + #[inline] + default unsafe fn write_clone_into_raw(&self, target: *mut Self) { + // Having allocated *first* may allow the optimizer to create + // the cloned value in-place, skipping the local and move. + unsafe { target.write(self.clone()) }; + } +} + +impl<T: Copy> WriteCloneIntoRaw for T { + #[inline] + unsafe fn write_clone_into_raw(&self, target: *mut Self) { + // We can always copy in-place, without ever involving a local value. + unsafe { target.copy_from_nonoverlapping(self, 1) }; + } +} diff --git a/library/alloc/src/boxed.rs b/library/alloc/src/boxed.rs index 7a9cf1948ea..0aa52b35ced 100644 --- a/library/alloc/src/boxed.rs +++ b/library/alloc/src/boxed.rs @@ -151,7 +151,7 @@ use core::pin::Pin; use core::ptr::{self, Unique}; use core::task::{Context, Poll}; -use crate::alloc::{handle_alloc_error, AllocError, Allocator, Global, Layout}; +use crate::alloc::{handle_alloc_error, AllocError, Allocator, Global, Layout, WriteCloneIntoRaw}; use crate::borrow::Cow; use crate::raw_vec::RawVec; use crate::str::from_boxed_utf8_unchecked; @@ -1047,28 +1047,6 @@ impl<T: Clone, A: Allocator + Clone> Clone for Box<T, A> { } } -/// Specialize clones into pre-allocated, uninitialized memory. -pub(crate) trait WriteCloneIntoRaw: Sized { - unsafe fn write_clone_into_raw(&self, target: *mut Self); -} - -impl<T: Clone> WriteCloneIntoRaw for T { - #[inline] - default unsafe fn write_clone_into_raw(&self, target: *mut Self) { - // Having allocated *first* may allow the optimizer to create - // the cloned value in-place, skipping the local and move. - unsafe { target.write(self.clone()) }; - } -} - -impl<T: Copy> WriteCloneIntoRaw for T { - #[inline] - unsafe fn write_clone_into_raw(&self, target: *mut Self) { - // We can always copy in-place, without ever involving a local value. - unsafe { target.copy_from_nonoverlapping(self, 1) }; - } -} - #[stable(feature = "box_slice_clone", since = "1.3.0")] impl Clone for Box<str> { fn clone(&self) -> Self { diff --git a/library/alloc/src/rc.rs b/library/alloc/src/rc.rs index c81405d6119..0973a6e362b 100644 --- a/library/alloc/src/rc.rs +++ b/library/alloc/src/rc.rs @@ -263,9 +263,10 @@ use core::pin::Pin; use core::ptr::{self, NonNull}; use core::slice::from_raw_parts_mut; -use crate::alloc::{box_free, handle_alloc_error, AllocError, Allocator, Global, Layout}; +use crate::alloc::{ + box_free, handle_alloc_error, AllocError, Allocator, Global, Layout, WriteCloneIntoRaw, +}; use crate::borrow::{Cow, ToOwned}; -use crate::boxed::WriteCloneIntoRaw; use crate::string::String; use crate::vec::Vec; diff --git a/library/alloc/src/sync.rs b/library/alloc/src/sync.rs index 5bfcbeb82a1..05bfeccbda1 100644 --- a/library/alloc/src/sync.rs +++ b/library/alloc/src/sync.rs @@ -22,9 +22,11 @@ use core::slice::from_raw_parts_mut; use core::sync::atomic; use core::sync::atomic::Ordering::{Acquire, Relaxed, Release, SeqCst}; -use crate::alloc::{box_free, handle_alloc_error, AllocError, Allocator, Global, Layout}; +use crate::alloc::{ + box_free, handle_alloc_error, AllocError, Allocator, Global, Layout, WriteCloneIntoRaw, +}; use crate::borrow::{Cow, ToOwned}; -use crate::boxed::{Box, WriteCloneIntoRaw}; +use crate::boxed::Box; use crate::rc::is_dangling; use crate::string::String; use crate::vec::Vec; |
