Skip to content

Commit a56b09a

Browse files
CoAlloc: Fixed my buggy rebase. Added slice::SpecCloneIntoVecCo and fixed Vec::clone_from.
1 parent 023f14a commit a56b09a

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

library/alloc/src/slice.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,6 +1000,44 @@ impl<T: Copy, A: Allocator> SpecCloneIntoVec<T, A> for [T] {
10001000
}
10011001
}
10021002

1003+
/// Coallocation-aware version of `SpecCloneIntoVec`.
1004+
#[cfg(not(no_global_oom_handling))]
1005+
#[allow(unused_braces)]
1006+
pub(crate) trait SpecCloneIntoVecCo<T, A: Allocator, const CO_ALLOC_PREF: CoAllocPref>
1007+
where [(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
1008+
{
1009+
fn clone_into_co(&self, target: &mut Vec<T, A, CO_ALLOC_PREF>);
1010+
}
1011+
1012+
#[cfg(not(no_global_oom_handling))]
1013+
#[allow(unused_braces)]
1014+
impl<T: Clone, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> SpecCloneIntoVecCo<T, A, CO_ALLOC_PREF> for [T]
1015+
where [(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:, {
1016+
default fn clone_into_co(&self, target: &mut Vec<T, A, CO_ALLOC_PREF>) {
1017+
// drop anything in target that will not be overwritten
1018+
target.truncate(self.len());
1019+
1020+
// target.len <= self.len due to the truncate above, so the
1021+
// slices here are always in-bounds.
1022+
let (init, tail) = self.split_at(target.len());
1023+
1024+
// reuse the contained values' allocations/resources.
1025+
target.clone_from_slice(init);
1026+
target.extend_from_slice(tail);
1027+
}
1028+
}
1029+
1030+
#[cfg(not(no_global_oom_handling))]
1031+
#[allow(unused_braces)]
1032+
impl<T: Copy, A: Allocator, const CO_ALLOC_PREF: CoAllocPref> SpecCloneIntoVecCo<T, A, CO_ALLOC_PREF> for [T]
1033+
where [(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:, {
1034+
fn clone_into_co(&self, target: &mut Vec<T, A, CO_ALLOC_PREF>) {
1035+
target.clear();
1036+
target.extend_from_slice(self);
1037+
}
1038+
}
1039+
1040+
10031041
#[cfg(not(no_global_oom_handling))]
10041042
#[stable(feature = "rust1", since = "1.0.0")]
10051043
impl<T: Clone> ToOwned for [T] {

library/alloc/src/vec/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,8 +1879,6 @@ where
18791879
where
18801880
[(); { crate::meta_num_slots!(A, CO_ALLOC_PREF) }]:,
18811881
{
1882-
crate::meta_num_slots!(A, CO_ALLOC_PREF)}]:,
1883-
{
18841882
/* Offset of the element we want to check if it is duplicate */
18851883
read: usize,
18861884

@@ -2869,7 +2867,7 @@ where
28692867
}
28702868

28712869
fn clone_from(&mut self, other: &Self) {
2872-
crate::slice::SpecCloneIntoVec::clone_into(other.as_slice(), self);
2870+
crate::slice::SpecCloneIntoVecCo::clone_into_co(other.as_slice(), self);
28732871
}
28742872
}
28752873

0 commit comments

Comments
 (0)