Skip to content
This repository was archived by the owner on Nov 27, 2020. It is now read-only.

Commit a7a17d0

Browse files
committed
Implement Clone for Vec
1 parent fbae9d9 commit a7a17d0

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

src/boxed.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ where
783783
}
784784
}
785785

786-
impl<T: Clone, A: Clone> Clone for Box<T, A>
786+
impl<T: Clone, A> Clone for Box<T, A>
787787
where
788788
A: AllocRef,
789789
A::BuildAlloc: Clone,

src/raw_vec.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -311,17 +311,23 @@ impl<T, A: DeallocRef> RawVec<T, A> {
311311
/// Returns the allocator used by this `RawVec` and the used layout, if any.
312312
/// The layout is `None` if the capacity of this `RawVec` is `0` or if `T` is a zero sized type.
313313
pub fn alloc_ref(&mut self) -> (A, Option<NonZeroLayout>) {
314-
let size = mem::size_of::<T>() * self.capacity;
314+
let layout = self.current_layout();
315315
unsafe {
316-
let layout = Layout::from_size_align_unchecked(size, mem::align_of::<T>())
317-
.try_into()
318-
.ok();
319316
let ptr = self.ptr.cast();
320317
let alloc = self.build_alloc_mut().build_alloc_ref(ptr.into(), layout);
321318
(alloc, layout)
322319
}
323320
}
324321

322+
pub fn current_layout(&self) -> Option<NonZeroLayout> {
323+
let size = mem::size_of::<T>() * self.capacity;
324+
unsafe {
325+
Layout::from_size_align_unchecked(size, mem::align_of::<T>())
326+
.try_into()
327+
.ok()
328+
}
329+
}
330+
325331
/// Converts a `Box<[T], A>` into a `RawVec<T, A>`.
326332
pub fn from_box(slice: Box<[mem::MaybeUninit<T>], A>) -> Self {
327333
unsafe {

src/vec.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,15 @@
6969
//! [`vec!`]: ../macro.vec.html
7070
7171
use crate::{
72-
alloc::{handle_alloc_error, AllocRef, DeallocRef, Global, ReallocRef},
72+
alloc::{
73+
handle_alloc_error,
74+
AllocRef,
75+
BuildAllocRef,
76+
DeallocRef,
77+
Global,
78+
NonZeroLayout,
79+
ReallocRef,
80+
},
7381
boxed::Box,
7482
clone::CloneIn,
7583
collections::CollectionAllocErr,
@@ -2177,11 +2185,22 @@ unsafe impl<T: ?Sized> IsZero for Option<Box<T>> {
21772185
// Common trait implementations for Vec
21782186
////////////////////////////////////////////////////////////////////////////////
21792187

2180-
impl<T: Clone> Clone for Vec<T> {
2188+
impl<T: Clone, A> Clone for Vec<T, A>
2189+
where
2190+
A: AllocRef,
2191+
A::BuildAlloc: Clone,
2192+
{
21812193
#[must_use]
21822194
#[inline]
21832195
fn clone(&self) -> Self {
2184-
self.clone_in(Global)
2196+
let mut b = self.buf.build_alloc().clone();
2197+
let old_layout = self.buf.current_layout();
2198+
2199+
unsafe {
2200+
let old_ptr = NonNull::new_unchecked(self.buf.ptr());
2201+
let a = b.build_alloc_ref(old_ptr.cast(), old_layout);
2202+
self.clone_in(a)
2203+
}
21852204
}
21862205
}
21872206

0 commit comments

Comments
 (0)