xref: /openbmc/linux/rust/alloc/vec/drain.rs (revision 3ed03f4da06ede71ac53cf25b9441a372e9f2487)
1057b8d25SMiguel Ojeda // SPDX-License-Identifier: Apache-2.0 OR MIT
2057b8d25SMiguel Ojeda 
3753dece8SMiguel Ojeda use crate::alloc::{Allocator, Global};
4753dece8SMiguel Ojeda use core::fmt;
5753dece8SMiguel Ojeda use core::iter::{FusedIterator, TrustedLen};
6*3ed03f4dSMiguel Ojeda use core::mem::{self, ManuallyDrop, SizedTypeProperties};
7753dece8SMiguel Ojeda use core::ptr::{self, NonNull};
8753dece8SMiguel Ojeda use core::slice::{self};
9753dece8SMiguel Ojeda 
10753dece8SMiguel Ojeda use super::Vec;
11753dece8SMiguel Ojeda 
12753dece8SMiguel Ojeda /// A draining iterator for `Vec<T>`.
13753dece8SMiguel Ojeda ///
14753dece8SMiguel Ojeda /// This `struct` is created by [`Vec::drain`].
15753dece8SMiguel Ojeda /// See its documentation for more.
16753dece8SMiguel Ojeda ///
17753dece8SMiguel Ojeda /// # Example
18753dece8SMiguel Ojeda ///
19753dece8SMiguel Ojeda /// ```
20753dece8SMiguel Ojeda /// let mut v = vec![0, 1, 2];
21753dece8SMiguel Ojeda /// let iter: std::vec::Drain<_> = v.drain(..);
22753dece8SMiguel Ojeda /// ```
23753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")]
24753dece8SMiguel Ojeda pub struct Drain<
25753dece8SMiguel Ojeda     'a,
26753dece8SMiguel Ojeda     T: 'a,
27753dece8SMiguel Ojeda     #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator + 'a = Global,
28753dece8SMiguel Ojeda > {
29753dece8SMiguel Ojeda     /// Index of tail to preserve
30753dece8SMiguel Ojeda     pub(super) tail_start: usize,
31753dece8SMiguel Ojeda     /// Length of tail
32753dece8SMiguel Ojeda     pub(super) tail_len: usize,
33753dece8SMiguel Ojeda     /// Current remaining range to remove
34753dece8SMiguel Ojeda     pub(super) iter: slice::Iter<'a, T>,
35753dece8SMiguel Ojeda     pub(super) vec: NonNull<Vec<T, A>>,
36753dece8SMiguel Ojeda }
37753dece8SMiguel Ojeda 
38753dece8SMiguel Ojeda #[stable(feature = "collection_debug", since = "1.17.0")]
39753dece8SMiguel Ojeda impl<T: fmt::Debug, A: Allocator> fmt::Debug for Drain<'_, T, A> {
40753dece8SMiguel Ojeda     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
41753dece8SMiguel Ojeda         f.debug_tuple("Drain").field(&self.iter.as_slice()).finish()
42753dece8SMiguel Ojeda     }
43753dece8SMiguel Ojeda }
44753dece8SMiguel Ojeda 
45753dece8SMiguel Ojeda impl<'a, T, A: Allocator> Drain<'a, T, A> {
46753dece8SMiguel Ojeda     /// Returns the remaining items of this iterator as a slice.
47753dece8SMiguel Ojeda     ///
48753dece8SMiguel Ojeda     /// # Examples
49753dece8SMiguel Ojeda     ///
50753dece8SMiguel Ojeda     /// ```
51753dece8SMiguel Ojeda     /// let mut vec = vec!['a', 'b', 'c'];
52753dece8SMiguel Ojeda     /// let mut drain = vec.drain(..);
53753dece8SMiguel Ojeda     /// assert_eq!(drain.as_slice(), &['a', 'b', 'c']);
54753dece8SMiguel Ojeda     /// let _ = drain.next().unwrap();
55753dece8SMiguel Ojeda     /// assert_eq!(drain.as_slice(), &['b', 'c']);
56753dece8SMiguel Ojeda     /// ```
57753dece8SMiguel Ojeda     #[must_use]
58753dece8SMiguel Ojeda     #[stable(feature = "vec_drain_as_slice", since = "1.46.0")]
59753dece8SMiguel Ojeda     pub fn as_slice(&self) -> &[T] {
60753dece8SMiguel Ojeda         self.iter.as_slice()
61753dece8SMiguel Ojeda     }
62753dece8SMiguel Ojeda 
63753dece8SMiguel Ojeda     /// Returns a reference to the underlying allocator.
64753dece8SMiguel Ojeda     #[unstable(feature = "allocator_api", issue = "32838")]
65753dece8SMiguel Ojeda     #[must_use]
66753dece8SMiguel Ojeda     #[inline]
67753dece8SMiguel Ojeda     pub fn allocator(&self) -> &A {
68753dece8SMiguel Ojeda         unsafe { self.vec.as_ref().allocator() }
69753dece8SMiguel Ojeda     }
70*3ed03f4dSMiguel Ojeda 
71*3ed03f4dSMiguel Ojeda     /// Keep unyielded elements in the source `Vec`.
72*3ed03f4dSMiguel Ojeda     ///
73*3ed03f4dSMiguel Ojeda     /// # Examples
74*3ed03f4dSMiguel Ojeda     ///
75*3ed03f4dSMiguel Ojeda     /// ```
76*3ed03f4dSMiguel Ojeda     /// #![feature(drain_keep_rest)]
77*3ed03f4dSMiguel Ojeda     ///
78*3ed03f4dSMiguel Ojeda     /// let mut vec = vec!['a', 'b', 'c'];
79*3ed03f4dSMiguel Ojeda     /// let mut drain = vec.drain(..);
80*3ed03f4dSMiguel Ojeda     ///
81*3ed03f4dSMiguel Ojeda     /// assert_eq!(drain.next().unwrap(), 'a');
82*3ed03f4dSMiguel Ojeda     ///
83*3ed03f4dSMiguel Ojeda     /// // This call keeps 'b' and 'c' in the vec.
84*3ed03f4dSMiguel Ojeda     /// drain.keep_rest();
85*3ed03f4dSMiguel Ojeda     ///
86*3ed03f4dSMiguel Ojeda     /// // If we wouldn't call `keep_rest()`,
87*3ed03f4dSMiguel Ojeda     /// // `vec` would be empty.
88*3ed03f4dSMiguel Ojeda     /// assert_eq!(vec, ['b', 'c']);
89*3ed03f4dSMiguel Ojeda     /// ```
90*3ed03f4dSMiguel Ojeda     #[unstable(feature = "drain_keep_rest", issue = "101122")]
91*3ed03f4dSMiguel Ojeda     pub fn keep_rest(self) {
92*3ed03f4dSMiguel Ojeda         // At this moment layout looks like this:
93*3ed03f4dSMiguel Ojeda         //
94*3ed03f4dSMiguel Ojeda         // [head] [yielded by next] [unyielded] [yielded by next_back] [tail]
95*3ed03f4dSMiguel Ojeda         //        ^-- start         \_________/-- unyielded_len        \____/-- self.tail_len
96*3ed03f4dSMiguel Ojeda         //                          ^-- unyielded_ptr                  ^-- tail
97*3ed03f4dSMiguel Ojeda         //
98*3ed03f4dSMiguel Ojeda         // Normally `Drop` impl would drop [unyielded] and then move [tail] to the `start`.
99*3ed03f4dSMiguel Ojeda         // Here we want to
100*3ed03f4dSMiguel Ojeda         // 1. Move [unyielded] to `start`
101*3ed03f4dSMiguel Ojeda         // 2. Move [tail] to a new start at `start + len(unyielded)`
102*3ed03f4dSMiguel Ojeda         // 3. Update length of the original vec to `len(head) + len(unyielded) + len(tail)`
103*3ed03f4dSMiguel Ojeda         //    a. In case of ZST, this is the only thing we want to do
104*3ed03f4dSMiguel Ojeda         // 4. Do *not* drop self, as everything is put in a consistent state already, there is nothing to do
105*3ed03f4dSMiguel Ojeda         let mut this = ManuallyDrop::new(self);
106*3ed03f4dSMiguel Ojeda 
107*3ed03f4dSMiguel Ojeda         unsafe {
108*3ed03f4dSMiguel Ojeda             let source_vec = this.vec.as_mut();
109*3ed03f4dSMiguel Ojeda 
110*3ed03f4dSMiguel Ojeda             let start = source_vec.len();
111*3ed03f4dSMiguel Ojeda             let tail = this.tail_start;
112*3ed03f4dSMiguel Ojeda 
113*3ed03f4dSMiguel Ojeda             let unyielded_len = this.iter.len();
114*3ed03f4dSMiguel Ojeda             let unyielded_ptr = this.iter.as_slice().as_ptr();
115*3ed03f4dSMiguel Ojeda 
116*3ed03f4dSMiguel Ojeda             // ZSTs have no identity, so we don't need to move them around.
117*3ed03f4dSMiguel Ojeda             let needs_move = mem::size_of::<T>() != 0;
118*3ed03f4dSMiguel Ojeda 
119*3ed03f4dSMiguel Ojeda             if needs_move {
120*3ed03f4dSMiguel Ojeda                 let start_ptr = source_vec.as_mut_ptr().add(start);
121*3ed03f4dSMiguel Ojeda 
122*3ed03f4dSMiguel Ojeda                 // memmove back unyielded elements
123*3ed03f4dSMiguel Ojeda                 if unyielded_ptr != start_ptr {
124*3ed03f4dSMiguel Ojeda                     let src = unyielded_ptr;
125*3ed03f4dSMiguel Ojeda                     let dst = start_ptr;
126*3ed03f4dSMiguel Ojeda 
127*3ed03f4dSMiguel Ojeda                     ptr::copy(src, dst, unyielded_len);
128*3ed03f4dSMiguel Ojeda                 }
129*3ed03f4dSMiguel Ojeda 
130*3ed03f4dSMiguel Ojeda                 // memmove back untouched tail
131*3ed03f4dSMiguel Ojeda                 if tail != (start + unyielded_len) {
132*3ed03f4dSMiguel Ojeda                     let src = source_vec.as_ptr().add(tail);
133*3ed03f4dSMiguel Ojeda                     let dst = start_ptr.add(unyielded_len);
134*3ed03f4dSMiguel Ojeda                     ptr::copy(src, dst, this.tail_len);
135*3ed03f4dSMiguel Ojeda                 }
136*3ed03f4dSMiguel Ojeda             }
137*3ed03f4dSMiguel Ojeda 
138*3ed03f4dSMiguel Ojeda             source_vec.set_len(start + unyielded_len + this.tail_len);
139*3ed03f4dSMiguel Ojeda         }
140*3ed03f4dSMiguel Ojeda     }
141753dece8SMiguel Ojeda }
142753dece8SMiguel Ojeda 
143753dece8SMiguel Ojeda #[stable(feature = "vec_drain_as_slice", since = "1.46.0")]
144753dece8SMiguel Ojeda impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A> {
145753dece8SMiguel Ojeda     fn as_ref(&self) -> &[T] {
146753dece8SMiguel Ojeda         self.as_slice()
147753dece8SMiguel Ojeda     }
148753dece8SMiguel Ojeda }
149753dece8SMiguel Ojeda 
150753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")]
151753dece8SMiguel Ojeda unsafe impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A> {}
152753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")]
153753dece8SMiguel Ojeda unsafe impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A> {}
154753dece8SMiguel Ojeda 
155753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")]
156753dece8SMiguel Ojeda impl<T, A: Allocator> Iterator for Drain<'_, T, A> {
157753dece8SMiguel Ojeda     type Item = T;
158753dece8SMiguel Ojeda 
159753dece8SMiguel Ojeda     #[inline]
160753dece8SMiguel Ojeda     fn next(&mut self) -> Option<T> {
161753dece8SMiguel Ojeda         self.iter.next().map(|elt| unsafe { ptr::read(elt as *const _) })
162753dece8SMiguel Ojeda     }
163753dece8SMiguel Ojeda 
164753dece8SMiguel Ojeda     fn size_hint(&self) -> (usize, Option<usize>) {
165753dece8SMiguel Ojeda         self.iter.size_hint()
166753dece8SMiguel Ojeda     }
167753dece8SMiguel Ojeda }
168753dece8SMiguel Ojeda 
169753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")]
170753dece8SMiguel Ojeda impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> {
171753dece8SMiguel Ojeda     #[inline]
172753dece8SMiguel Ojeda     fn next_back(&mut self) -> Option<T> {
173753dece8SMiguel Ojeda         self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) })
174753dece8SMiguel Ojeda     }
175753dece8SMiguel Ojeda }
176753dece8SMiguel Ojeda 
177753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")]
178753dece8SMiguel Ojeda impl<T, A: Allocator> Drop for Drain<'_, T, A> {
179753dece8SMiguel Ojeda     fn drop(&mut self) {
180753dece8SMiguel Ojeda         /// Moves back the un-`Drain`ed elements to restore the original `Vec`.
181753dece8SMiguel Ojeda         struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>);
182753dece8SMiguel Ojeda 
183753dece8SMiguel Ojeda         impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> {
184753dece8SMiguel Ojeda             fn drop(&mut self) {
185753dece8SMiguel Ojeda                 if self.0.tail_len > 0 {
186753dece8SMiguel Ojeda                     unsafe {
187753dece8SMiguel Ojeda                         let source_vec = self.0.vec.as_mut();
188753dece8SMiguel Ojeda                         // memmove back untouched tail, update to new length
189753dece8SMiguel Ojeda                         let start = source_vec.len();
190753dece8SMiguel Ojeda                         let tail = self.0.tail_start;
191753dece8SMiguel Ojeda                         if tail != start {
192753dece8SMiguel Ojeda                             let src = source_vec.as_ptr().add(tail);
193753dece8SMiguel Ojeda                             let dst = source_vec.as_mut_ptr().add(start);
194753dece8SMiguel Ojeda                             ptr::copy(src, dst, self.0.tail_len);
195753dece8SMiguel Ojeda                         }
196753dece8SMiguel Ojeda                         source_vec.set_len(start + self.0.tail_len);
197753dece8SMiguel Ojeda                     }
198753dece8SMiguel Ojeda                 }
199753dece8SMiguel Ojeda             }
200753dece8SMiguel Ojeda         }
201753dece8SMiguel Ojeda 
202753dece8SMiguel Ojeda         let iter = mem::replace(&mut self.iter, (&mut []).iter());
203753dece8SMiguel Ojeda         let drop_len = iter.len();
204753dece8SMiguel Ojeda 
205753dece8SMiguel Ojeda         let mut vec = self.vec;
206753dece8SMiguel Ojeda 
207*3ed03f4dSMiguel Ojeda         if T::IS_ZST {
208753dece8SMiguel Ojeda             // ZSTs have no identity, so we don't need to move them around, we only need to drop the correct amount.
209753dece8SMiguel Ojeda             // this can be achieved by manipulating the Vec length instead of moving values out from `iter`.
210753dece8SMiguel Ojeda             unsafe {
211753dece8SMiguel Ojeda                 let vec = vec.as_mut();
212753dece8SMiguel Ojeda                 let old_len = vec.len();
213753dece8SMiguel Ojeda                 vec.set_len(old_len + drop_len + self.tail_len);
214753dece8SMiguel Ojeda                 vec.truncate(old_len + self.tail_len);
215753dece8SMiguel Ojeda             }
216753dece8SMiguel Ojeda 
217753dece8SMiguel Ojeda             return;
218753dece8SMiguel Ojeda         }
219753dece8SMiguel Ojeda 
220753dece8SMiguel Ojeda         // ensure elements are moved back into their appropriate places, even when drop_in_place panics
221753dece8SMiguel Ojeda         let _guard = DropGuard(self);
222753dece8SMiguel Ojeda 
223753dece8SMiguel Ojeda         if drop_len == 0 {
224753dece8SMiguel Ojeda             return;
225753dece8SMiguel Ojeda         }
226753dece8SMiguel Ojeda 
227753dece8SMiguel Ojeda         // as_slice() must only be called when iter.len() is > 0 because
228*3ed03f4dSMiguel Ojeda         // it also gets touched by vec::Splice which may turn it into a dangling pointer
229*3ed03f4dSMiguel Ojeda         // which would make it and the vec pointer point to different allocations which would
230*3ed03f4dSMiguel Ojeda         // lead to invalid pointer arithmetic below.
231753dece8SMiguel Ojeda         let drop_ptr = iter.as_slice().as_ptr();
232753dece8SMiguel Ojeda 
233753dece8SMiguel Ojeda         unsafe {
234753dece8SMiguel Ojeda             // drop_ptr comes from a slice::Iter which only gives us a &[T] but for drop_in_place
235753dece8SMiguel Ojeda             // a pointer with mutable provenance is necessary. Therefore we must reconstruct
236753dece8SMiguel Ojeda             // it from the original vec but also avoid creating a &mut to the front since that could
237753dece8SMiguel Ojeda             // invalidate raw pointers to it which some unsafe code might rely on.
238753dece8SMiguel Ojeda             let vec_ptr = vec.as_mut().as_mut_ptr();
239753dece8SMiguel Ojeda             let drop_offset = drop_ptr.sub_ptr(vec_ptr);
240753dece8SMiguel Ojeda             let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len);
241753dece8SMiguel Ojeda             ptr::drop_in_place(to_drop);
242753dece8SMiguel Ojeda         }
243753dece8SMiguel Ojeda     }
244753dece8SMiguel Ojeda }
245753dece8SMiguel Ojeda 
246753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")]
247753dece8SMiguel Ojeda impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> {
248753dece8SMiguel Ojeda     fn is_empty(&self) -> bool {
249753dece8SMiguel Ojeda         self.iter.is_empty()
250753dece8SMiguel Ojeda     }
251753dece8SMiguel Ojeda }
252753dece8SMiguel Ojeda 
253753dece8SMiguel Ojeda #[unstable(feature = "trusted_len", issue = "37572")]
254753dece8SMiguel Ojeda unsafe impl<T, A: Allocator> TrustedLen for Drain<'_, T, A> {}
255753dece8SMiguel Ojeda 
256753dece8SMiguel Ojeda #[stable(feature = "fused", since = "1.26.0")]
257753dece8SMiguel Ojeda impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {}
258