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}; 63ed03f4dSMiguel 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]; 21*89eed1abSMiguel 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> { fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result40753dece8SMiguel 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")] as_slice(&self) -> &[T]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] allocator(&self) -> &A67753dece8SMiguel Ojeda pub fn allocator(&self) -> &A { 68753dece8SMiguel Ojeda unsafe { self.vec.as_ref().allocator() } 69753dece8SMiguel Ojeda } 703ed03f4dSMiguel Ojeda 713ed03f4dSMiguel Ojeda /// Keep unyielded elements in the source `Vec`. 723ed03f4dSMiguel Ojeda /// 733ed03f4dSMiguel Ojeda /// # Examples 743ed03f4dSMiguel Ojeda /// 753ed03f4dSMiguel Ojeda /// ``` 763ed03f4dSMiguel Ojeda /// #![feature(drain_keep_rest)] 773ed03f4dSMiguel Ojeda /// 783ed03f4dSMiguel Ojeda /// let mut vec = vec!['a', 'b', 'c']; 793ed03f4dSMiguel Ojeda /// let mut drain = vec.drain(..); 803ed03f4dSMiguel Ojeda /// 813ed03f4dSMiguel Ojeda /// assert_eq!(drain.next().unwrap(), 'a'); 823ed03f4dSMiguel Ojeda /// 833ed03f4dSMiguel Ojeda /// // This call keeps 'b' and 'c' in the vec. 843ed03f4dSMiguel Ojeda /// drain.keep_rest(); 853ed03f4dSMiguel Ojeda /// 863ed03f4dSMiguel Ojeda /// // If we wouldn't call `keep_rest()`, 873ed03f4dSMiguel Ojeda /// // `vec` would be empty. 883ed03f4dSMiguel Ojeda /// assert_eq!(vec, ['b', 'c']); 893ed03f4dSMiguel Ojeda /// ``` 903ed03f4dSMiguel Ojeda #[unstable(feature = "drain_keep_rest", issue = "101122")] keep_rest(self)913ed03f4dSMiguel Ojeda pub fn keep_rest(self) { 923ed03f4dSMiguel Ojeda // At this moment layout looks like this: 933ed03f4dSMiguel Ojeda // 943ed03f4dSMiguel Ojeda // [head] [yielded by next] [unyielded] [yielded by next_back] [tail] 953ed03f4dSMiguel Ojeda // ^-- start \_________/-- unyielded_len \____/-- self.tail_len 963ed03f4dSMiguel Ojeda // ^-- unyielded_ptr ^-- tail 973ed03f4dSMiguel Ojeda // 983ed03f4dSMiguel Ojeda // Normally `Drop` impl would drop [unyielded] and then move [tail] to the `start`. 993ed03f4dSMiguel Ojeda // Here we want to 1003ed03f4dSMiguel Ojeda // 1. Move [unyielded] to `start` 1013ed03f4dSMiguel Ojeda // 2. Move [tail] to a new start at `start + len(unyielded)` 1023ed03f4dSMiguel Ojeda // 3. Update length of the original vec to `len(head) + len(unyielded) + len(tail)` 1033ed03f4dSMiguel Ojeda // a. In case of ZST, this is the only thing we want to do 1043ed03f4dSMiguel Ojeda // 4. Do *not* drop self, as everything is put in a consistent state already, there is nothing to do 1053ed03f4dSMiguel Ojeda let mut this = ManuallyDrop::new(self); 1063ed03f4dSMiguel Ojeda 1073ed03f4dSMiguel Ojeda unsafe { 1083ed03f4dSMiguel Ojeda let source_vec = this.vec.as_mut(); 1093ed03f4dSMiguel Ojeda 1103ed03f4dSMiguel Ojeda let start = source_vec.len(); 1113ed03f4dSMiguel Ojeda let tail = this.tail_start; 1123ed03f4dSMiguel Ojeda 1133ed03f4dSMiguel Ojeda let unyielded_len = this.iter.len(); 1143ed03f4dSMiguel Ojeda let unyielded_ptr = this.iter.as_slice().as_ptr(); 1153ed03f4dSMiguel Ojeda 1163ed03f4dSMiguel Ojeda // ZSTs have no identity, so we don't need to move them around. 117*89eed1abSMiguel Ojeda if !T::IS_ZST { 1183ed03f4dSMiguel Ojeda let start_ptr = source_vec.as_mut_ptr().add(start); 1193ed03f4dSMiguel Ojeda 1203ed03f4dSMiguel Ojeda // memmove back unyielded elements 1213ed03f4dSMiguel Ojeda if unyielded_ptr != start_ptr { 1223ed03f4dSMiguel Ojeda let src = unyielded_ptr; 1233ed03f4dSMiguel Ojeda let dst = start_ptr; 1243ed03f4dSMiguel Ojeda 1253ed03f4dSMiguel Ojeda ptr::copy(src, dst, unyielded_len); 1263ed03f4dSMiguel Ojeda } 1273ed03f4dSMiguel Ojeda 1283ed03f4dSMiguel Ojeda // memmove back untouched tail 1293ed03f4dSMiguel Ojeda if tail != (start + unyielded_len) { 1303ed03f4dSMiguel Ojeda let src = source_vec.as_ptr().add(tail); 1313ed03f4dSMiguel Ojeda let dst = start_ptr.add(unyielded_len); 1323ed03f4dSMiguel Ojeda ptr::copy(src, dst, this.tail_len); 1333ed03f4dSMiguel Ojeda } 1343ed03f4dSMiguel Ojeda } 1353ed03f4dSMiguel Ojeda 1363ed03f4dSMiguel Ojeda source_vec.set_len(start + unyielded_len + this.tail_len); 1373ed03f4dSMiguel Ojeda } 1383ed03f4dSMiguel Ojeda } 139753dece8SMiguel Ojeda } 140753dece8SMiguel Ojeda 141753dece8SMiguel Ojeda #[stable(feature = "vec_drain_as_slice", since = "1.46.0")] 142753dece8SMiguel Ojeda impl<'a, T, A: Allocator> AsRef<[T]> for Drain<'a, T, A> { as_ref(&self) -> &[T]143753dece8SMiguel Ojeda fn as_ref(&self) -> &[T] { 144753dece8SMiguel Ojeda self.as_slice() 145753dece8SMiguel Ojeda } 146753dece8SMiguel Ojeda } 147753dece8SMiguel Ojeda 148753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")] 149753dece8SMiguel Ojeda unsafe impl<T: Sync, A: Sync + Allocator> Sync for Drain<'_, T, A> {} 150753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")] 151753dece8SMiguel Ojeda unsafe impl<T: Send, A: Send + Allocator> Send for Drain<'_, T, A> {} 152753dece8SMiguel Ojeda 153753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")] 154753dece8SMiguel Ojeda impl<T, A: Allocator> Iterator for Drain<'_, T, A> { 155753dece8SMiguel Ojeda type Item = T; 156753dece8SMiguel Ojeda 157753dece8SMiguel Ojeda #[inline] next(&mut self) -> Option<T>158753dece8SMiguel Ojeda fn next(&mut self) -> Option<T> { 159753dece8SMiguel Ojeda self.iter.next().map(|elt| unsafe { ptr::read(elt as *const _) }) 160753dece8SMiguel Ojeda } 161753dece8SMiguel Ojeda size_hint(&self) -> (usize, Option<usize>)162753dece8SMiguel Ojeda fn size_hint(&self) -> (usize, Option<usize>) { 163753dece8SMiguel Ojeda self.iter.size_hint() 164753dece8SMiguel Ojeda } 165753dece8SMiguel Ojeda } 166753dece8SMiguel Ojeda 167753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")] 168753dece8SMiguel Ojeda impl<T, A: Allocator> DoubleEndedIterator for Drain<'_, T, A> { 169753dece8SMiguel Ojeda #[inline] next_back(&mut self) -> Option<T>170753dece8SMiguel Ojeda fn next_back(&mut self) -> Option<T> { 171753dece8SMiguel Ojeda self.iter.next_back().map(|elt| unsafe { ptr::read(elt as *const _) }) 172753dece8SMiguel Ojeda } 173753dece8SMiguel Ojeda } 174753dece8SMiguel Ojeda 175753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")] 176753dece8SMiguel Ojeda impl<T, A: Allocator> Drop for Drain<'_, T, A> { drop(&mut self)177753dece8SMiguel Ojeda fn drop(&mut self) { 178753dece8SMiguel Ojeda /// Moves back the un-`Drain`ed elements to restore the original `Vec`. 179753dece8SMiguel Ojeda struct DropGuard<'r, 'a, T, A: Allocator>(&'r mut Drain<'a, T, A>); 180753dece8SMiguel Ojeda 181753dece8SMiguel Ojeda impl<'r, 'a, T, A: Allocator> Drop for DropGuard<'r, 'a, T, A> { 182753dece8SMiguel Ojeda fn drop(&mut self) { 183753dece8SMiguel Ojeda if self.0.tail_len > 0 { 184753dece8SMiguel Ojeda unsafe { 185753dece8SMiguel Ojeda let source_vec = self.0.vec.as_mut(); 186753dece8SMiguel Ojeda // memmove back untouched tail, update to new length 187753dece8SMiguel Ojeda let start = source_vec.len(); 188753dece8SMiguel Ojeda let tail = self.0.tail_start; 189753dece8SMiguel Ojeda if tail != start { 190753dece8SMiguel Ojeda let src = source_vec.as_ptr().add(tail); 191753dece8SMiguel Ojeda let dst = source_vec.as_mut_ptr().add(start); 192753dece8SMiguel Ojeda ptr::copy(src, dst, self.0.tail_len); 193753dece8SMiguel Ojeda } 194753dece8SMiguel Ojeda source_vec.set_len(start + self.0.tail_len); 195753dece8SMiguel Ojeda } 196753dece8SMiguel Ojeda } 197753dece8SMiguel Ojeda } 198753dece8SMiguel Ojeda } 199753dece8SMiguel Ojeda 200*89eed1abSMiguel Ojeda let iter = mem::take(&mut self.iter); 201753dece8SMiguel Ojeda let drop_len = iter.len(); 202753dece8SMiguel Ojeda 203753dece8SMiguel Ojeda let mut vec = self.vec; 204753dece8SMiguel Ojeda 2053ed03f4dSMiguel Ojeda if T::IS_ZST { 206753dece8SMiguel Ojeda // ZSTs have no identity, so we don't need to move them around, we only need to drop the correct amount. 207753dece8SMiguel Ojeda // this can be achieved by manipulating the Vec length instead of moving values out from `iter`. 208753dece8SMiguel Ojeda unsafe { 209753dece8SMiguel Ojeda let vec = vec.as_mut(); 210753dece8SMiguel Ojeda let old_len = vec.len(); 211753dece8SMiguel Ojeda vec.set_len(old_len + drop_len + self.tail_len); 212753dece8SMiguel Ojeda vec.truncate(old_len + self.tail_len); 213753dece8SMiguel Ojeda } 214753dece8SMiguel Ojeda 215753dece8SMiguel Ojeda return; 216753dece8SMiguel Ojeda } 217753dece8SMiguel Ojeda 218753dece8SMiguel Ojeda // ensure elements are moved back into their appropriate places, even when drop_in_place panics 219753dece8SMiguel Ojeda let _guard = DropGuard(self); 220753dece8SMiguel Ojeda 221753dece8SMiguel Ojeda if drop_len == 0 { 222753dece8SMiguel Ojeda return; 223753dece8SMiguel Ojeda } 224753dece8SMiguel Ojeda 225753dece8SMiguel Ojeda // as_slice() must only be called when iter.len() is > 0 because 2263ed03f4dSMiguel Ojeda // it also gets touched by vec::Splice which may turn it into a dangling pointer 2273ed03f4dSMiguel Ojeda // which would make it and the vec pointer point to different allocations which would 2283ed03f4dSMiguel Ojeda // lead to invalid pointer arithmetic below. 229753dece8SMiguel Ojeda let drop_ptr = iter.as_slice().as_ptr(); 230753dece8SMiguel Ojeda 231753dece8SMiguel Ojeda unsafe { 232753dece8SMiguel Ojeda // drop_ptr comes from a slice::Iter which only gives us a &[T] but for drop_in_place 233753dece8SMiguel Ojeda // a pointer with mutable provenance is necessary. Therefore we must reconstruct 234753dece8SMiguel Ojeda // it from the original vec but also avoid creating a &mut to the front since that could 235753dece8SMiguel Ojeda // invalidate raw pointers to it which some unsafe code might rely on. 236753dece8SMiguel Ojeda let vec_ptr = vec.as_mut().as_mut_ptr(); 237753dece8SMiguel Ojeda let drop_offset = drop_ptr.sub_ptr(vec_ptr); 238753dece8SMiguel Ojeda let to_drop = ptr::slice_from_raw_parts_mut(vec_ptr.add(drop_offset), drop_len); 239753dece8SMiguel Ojeda ptr::drop_in_place(to_drop); 240753dece8SMiguel Ojeda } 241753dece8SMiguel Ojeda } 242753dece8SMiguel Ojeda } 243753dece8SMiguel Ojeda 244753dece8SMiguel Ojeda #[stable(feature = "drain", since = "1.6.0")] 245753dece8SMiguel Ojeda impl<T, A: Allocator> ExactSizeIterator for Drain<'_, T, A> { is_empty(&self) -> bool246753dece8SMiguel Ojeda fn is_empty(&self) -> bool { 247753dece8SMiguel Ojeda self.iter.is_empty() 248753dece8SMiguel Ojeda } 249753dece8SMiguel Ojeda } 250753dece8SMiguel Ojeda 251753dece8SMiguel Ojeda #[unstable(feature = "trusted_len", issue = "37572")] 252753dece8SMiguel Ojeda unsafe impl<T, A: Allocator> TrustedLen for Drain<'_, T, A> {} 253753dece8SMiguel Ojeda 254753dece8SMiguel Ojeda #[stable(feature = "fused", since = "1.26.0")] 255753dece8SMiguel Ojeda impl<T, A: Allocator> FusedIterator for Drain<'_, T, A> {} 256