Skip to content

Commit 6c7c12b

Browse files
committed
Minimize diff between src/arrayvec{,_copy}.rs
1 parent 1740ea8 commit 6c7c12b

File tree

3 files changed

+35
-125
lines changed

3 files changed

+35
-125
lines changed

src/arrayvec.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
499499
let mut g = BackshiftOnDrop { v: self, processed_len: 0, deleted_cnt: 0, original_len };
500500

501501
#[inline(always)]
502-
fn process_one<F: FnMut(&mut T) -> bool, T, const CAP: usize, const DELETED: bool>(
502+
fn process_one<T, F: FnMut(&mut T) -> bool, const CAP: usize, const DELETED: bool>(
503503
f: &mut F,
504504
g: &mut BackshiftOnDrop<'_, T, CAP>
505505
) -> bool {
@@ -522,14 +522,14 @@ impl<T, const CAP: usize> ArrayVec<T, CAP> {
522522

523523
// Stage 1: Nothing was deleted.
524524
while g.processed_len != original_len {
525-
if !process_one::<F, T, CAP, false>(&mut f, &mut g) {
525+
if !process_one::<T, F, CAP, false>(&mut f, &mut g) {
526526
break;
527527
}
528528
}
529529

530530
// Stage 2: Some elements were deleted.
531531
while g.processed_len != original_len {
532-
process_one::<F, T, CAP, true>(&mut f, &mut g);
532+
process_one::<T, F, CAP, true>(&mut f, &mut g);
533533
}
534534

535535
drop(g);
@@ -1029,16 +1029,16 @@ impl<'a, T: 'a, const CAP: usize> Drop for Drain<'a, T, CAP> {
10291029
}
10301030
}
10311031

1032-
struct ScopeExitGuard<T, Data, F>
1033-
where F: FnMut(&Data, &mut T)
1032+
struct ScopeExitGuard<V, Data, F>
1033+
where F: FnMut(&Data, &mut V)
10341034
{
1035-
value: T,
1035+
value: V,
10361036
data: Data,
10371037
f: F,
10381038
}
10391039

1040-
impl<T, Data, F> Drop for ScopeExitGuard<T, Data, F>
1041-
where F: FnMut(&Data, &mut T)
1040+
impl<V, Data, F> Drop for ScopeExitGuard<V, Data, F>
1041+
where F: FnMut(&Data, &mut V)
10421042
{
10431043
fn drop(&mut self) {
10441044
(self.f)(&self.data, &mut self.value)

src/arrayvec_copy.patch

+9-107
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,18 @@
11
diff --git a/src/arrayvec_copy_generated.rs b/src/arrayvec_copy.rs
2-
index 2c32eed..ada0728 100644
2+
index c4be864..ec5224d 100644
33
--- a/src/arrayvec_copy_generated.rs
44
+++ b/src/arrayvec_copy.rs
5-
@@ -14,7 +14,6 @@ use std::fmt;
6-
#[cfg(feature="std")]
7-
use std::io;
5+
@@ -27,6 +27,9 @@ use crate::utils::MakeMaybeUninit;
86

9-
-use std::mem::ManuallyDrop;
10-
use std::mem::MaybeUninit;
11-
12-
#[cfg(feature="serde")]
13-
@@ -25,7 +24,7 @@ use crate::errors::CapacityError;
14-
use crate::arrayvec_impl::ArrayVecImpl;
15-
use crate::utils::MakeMaybeUninit;
16-
17-
-/// A vector with a fixed capacity.
18-
+/// A vector with a fixed capacity which implements `Copy` and its elemenents are constrained to also be `Copy`.
7+
/// A vector with a fixed capacity.
198
///
9+
+/// **Its only difference to [`ArrayVec`](crate::ArrayVec) is that its elements
10+
+/// are constrained to be `Copy` which allows it to be `Copy` itself.**
11+
+///
2012
/// The `ArrayVecCopy` is a vector backed by a fixed size array. It keeps track of
2113
/// the number of initialized elements. The `ArrayVecCopy<T, CAP>` is parameterized
22-
@@ -46,14 +45,6 @@ pub struct ArrayVecCopy<T: Copy, const CAP: usize> {
14+
/// by `T` for the element type and `CAP` for the maximum capacity.
15+
@@ -46,14 +49,6 @@ pub struct ArrayVecCopy<T: Copy, const CAP: usize> {
2316
xs: [MaybeUninit<T>; CAP],
2417
}
2518

@@ -34,80 +27,7 @@ index 2c32eed..ada0728 100644
3427
macro_rules! panic_oob {
3528
($method_name:expr, $index:expr, $len:expr) => {
3629
panic!(concat!("ArrayVecCopy::", $method_name, ": index {} is out of bounds in vector of length {}"),
37-
@@ -231,8 +222,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
38-
ArrayVecImpl::push_unchecked(self, element)
39-
}
40-
41-
- /// Shortens the vector, keeping the first `len` elements and dropping
42-
- /// the rest.
43-
+ /// Shortens the vector, keeping the first `len` elements.
44-
///
45-
/// If `len` is greater than the vector’s current length this has no
46-
/// effect.
47-
@@ -499,7 +489,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
48-
let mut g = BackshiftOnDrop { v: self, processed_len: 0, deleted_cnt: 0, original_len };
49-
50-
#[inline(always)]
51-
- fn process_one<F: FnMut(&mut T) -> bool, T, const CAP: usize, const DELETED: bool>(
52-
+ fn process_one<T: Copy, F: FnMut(&mut T) -> bool, const CAP: usize, const DELETED: bool>(
53-
f: &mut F,
54-
g: &mut BackshiftOnDrop<'_, T, CAP>
55-
) -> bool {
56-
@@ -507,7 +497,6 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
57-
if !f(unsafe { &mut *cur }) {
58-
g.processed_len += 1;
59-
g.deleted_cnt += 1;
60-
- unsafe { ptr::drop_in_place(cur) };
61-
return false;
62-
}
63-
if DELETED {
64-
@@ -522,20 +511,20 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
65-
66-
// Stage 1: Nothing was deleted.
67-
while g.processed_len != original_len {
68-
- if !process_one::<F, T, CAP, false>(&mut f, &mut g) {
69-
+ if !process_one::<T, F, CAP, false>(&mut f, &mut g) {
70-
break;
71-
}
72-
}
73-
74-
// Stage 2: Some elements were deleted.
75-
while g.processed_len != original_len {
76-
- process_one::<F, T, CAP, true>(&mut f, &mut g);
77-
+ process_one::<T, F, CAP, true>(&mut f, &mut g);
78-
}
79-
80-
drop(g);
81-
}
82-
83-
- /// Set the vector’s length without dropping or moving out elements
84-
+ /// Set the vector’s length without moving out elements
85-
///
86-
/// This method is `unsafe` because it changes the notion of the
87-
/// number of “valid” elements in the vector. Use with care.
88-
@@ -668,8 +657,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
89-
/// This operation is safe if and only if length equals capacity.
90-
pub unsafe fn into_inner_unchecked(self) -> [T; CAP] {
91-
debug_assert_eq!(self.len(), self.capacity());
92-
- let self_ = ManuallyDrop::new(self);
93-
- let array = ptr::read(self_.as_ptr() as *const [T; CAP]);
94-
+ let array = ptr::read(self.as_ptr() as *const [T; CAP]);
95-
array
96-
}
97-
98-
@@ -755,10 +743,9 @@ impl<T: Copy, const CAP: usize> DerefMut for ArrayVecCopy<T, CAP> {
99-
impl<T: Copy, const CAP: usize> From<[T; CAP]> for ArrayVecCopy<T, CAP> {
100-
#[track_caller]
101-
fn from(array: [T; CAP]) -> Self {
102-
- let array = ManuallyDrop::new(array);
103-
let mut vec = <ArrayVecCopy<T, CAP>>::new();
104-
unsafe {
105-
- (&*array as *const [T; CAP] as *const [MaybeUninit<T>; CAP])
106-
+ (&array as *const [T; CAP] as *const [MaybeUninit<T>; CAP])
107-
.copy_to_nonoverlapping(&mut vec.xs as *mut [MaybeUninit<T>; CAP], 1);
108-
vec.set_len(CAP);
109-
}
110-
@@ -929,21 +916,6 @@ impl<T: Copy, const CAP: usize> DoubleEndedIterator for IntoIter<T, CAP> {
30+
@@ -929,21 +924,6 @@ impl<T: Copy, const CAP: usize> DoubleEndedIterator for IntoIter<T, CAP> {
11131

11232
impl<T: Copy, const CAP: usize> ExactSizeIterator for IntoIter<T, CAP> { }
11333

@@ -129,21 +49,3 @@ index 2c32eed..ada0728 100644
12949
impl<T: Copy, const CAP: usize> Clone for IntoIter<T, CAP>
13050
where T: Clone,
13151
{
132-
@@ -1029,7 +1001,7 @@ impl<'a, T: Copy + 'a, const CAP: usize> Drop for Drain<'a, T, CAP> {
133-
}
134-
}
135-
136-
-struct ScopeExitGuard<T: Copy, Data, F>
137-
+struct ScopeExitGuard<T, Data, F>
138-
where F: FnMut(&Data, &mut T)
139-
{
140-
value: T,
141-
@@ -1037,7 +1009,7 @@ struct ScopeExitGuard<T: Copy, Data, F>
142-
f: F,
143-
}
144-
145-
-impl<T: Copy, Data, F> Drop for ScopeExitGuard<T, Data, F>
146-
+impl<T, Data, F> Drop for ScopeExitGuard<T, Data, F>
147-
where F: FnMut(&Data, &mut T)
148-
{
149-
fn drop(&mut self) {

src/arrayvec_copy.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use std::fmt;
1414
#[cfg(feature="std")]
1515
use std::io;
1616

17+
use std::mem::ManuallyDrop;
1718
use std::mem::MaybeUninit;
1819

1920
#[cfg(feature="serde")]
@@ -24,7 +25,10 @@ use crate::errors::CapacityError;
2425
use crate::arrayvec_impl::ArrayVecImpl;
2526
use crate::utils::MakeMaybeUninit;
2627

27-
/// A vector with a fixed capacity which implements `Copy` and its elemenents are constrained to also be `Copy`.
28+
/// A vector with a fixed capacity.
29+
///
30+
/// **Its only difference to [`ArrayVec`](crate::ArrayVec) is that its elements
31+
/// are constrained to be `Copy` which allows it to be `Copy` itself.**
2832
///
2933
/// The `ArrayVecCopy` is a vector backed by a fixed size array. It keeps track of
3034
/// the number of initialized elements. The `ArrayVecCopy<T, CAP>` is parameterized
@@ -222,7 +226,8 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
222226
ArrayVecImpl::push_unchecked(self, element)
223227
}
224228

225-
/// Shortens the vector, keeping the first `len` elements.
229+
/// Shortens the vector, keeping the first `len` elements and dropping
230+
/// the rest.
226231
///
227232
/// If `len` is greater than the vector’s current length this has no
228233
/// effect.
@@ -497,6 +502,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
497502
if !f(unsafe { &mut *cur }) {
498503
g.processed_len += 1;
499504
g.deleted_cnt += 1;
505+
unsafe { ptr::drop_in_place(cur) };
500506
return false;
501507
}
502508
if DELETED {
@@ -524,7 +530,7 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
524530
drop(g);
525531
}
526532

527-
/// Set the vector’s length without moving out elements
533+
/// Set the vector’s length without dropping or moving out elements
528534
///
529535
/// This method is `unsafe` because it changes the notion of the
530536
/// number of “valid” elements in the vector. Use with care.
@@ -657,7 +663,8 @@ impl<T: Copy, const CAP: usize> ArrayVecCopy<T, CAP> {
657663
/// This operation is safe if and only if length equals capacity.
658664
pub unsafe fn into_inner_unchecked(self) -> [T; CAP] {
659665
debug_assert_eq!(self.len(), self.capacity());
660-
let array = ptr::read(self.as_ptr() as *const [T; CAP]);
666+
let self_ = ManuallyDrop::new(self);
667+
let array = ptr::read(self_.as_ptr() as *const [T; CAP]);
661668
array
662669
}
663670

@@ -743,9 +750,10 @@ impl<T: Copy, const CAP: usize> DerefMut for ArrayVecCopy<T, CAP> {
743750
impl<T: Copy, const CAP: usize> From<[T; CAP]> for ArrayVecCopy<T, CAP> {
744751
#[track_caller]
745752
fn from(array: [T; CAP]) -> Self {
753+
let array = ManuallyDrop::new(array);
746754
let mut vec = <ArrayVecCopy<T, CAP>>::new();
747755
unsafe {
748-
(&array as *const [T; CAP] as *const [MaybeUninit<T>; CAP])
756+
(&*array as *const [T; CAP] as *const [MaybeUninit<T>; CAP])
749757
.copy_to_nonoverlapping(&mut vec.xs as *mut [MaybeUninit<T>; CAP], 1);
750758
vec.set_len(CAP);
751759
}
@@ -1001,16 +1009,16 @@ impl<'a, T: Copy + 'a, const CAP: usize> Drop for Drain<'a, T, CAP> {
10011009
}
10021010
}
10031011

1004-
struct ScopeExitGuard<T, Data, F>
1005-
where F: FnMut(&Data, &mut T)
1012+
struct ScopeExitGuard<V, Data, F>
1013+
where F: FnMut(&Data, &mut V)
10061014
{
1007-
value: T,
1015+
value: V,
10081016
data: Data,
10091017
f: F,
10101018
}
10111019

1012-
impl<T, Data, F> Drop for ScopeExitGuard<T, Data, F>
1013-
where F: FnMut(&Data, &mut T)
1020+
impl<V, Data, F> Drop for ScopeExitGuard<V, Data, F>
1021+
where F: FnMut(&Data, &mut V)
10141022
{
10151023
fn drop(&mut self) {
10161024
(self.f)(&self.data, &mut self.value)

0 commit comments

Comments
 (0)