into_iter.rs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. // SPDX-License-Identifier: Apache-2.0 OR MIT
  2. #[cfg(not(no_global_oom_handling))]
  3. use super::AsVecIntoIter;
  4. use crate::alloc::{Allocator, Global};
  5. use crate::raw_vec::RawVec;
  6. use core::fmt;
  7. use core::intrinsics::arith_offset;
  8. use core::iter::{
  9. FusedIterator, InPlaceIterable, SourceIter, TrustedLen, TrustedRandomAccessNoCoerce,
  10. };
  11. use core::marker::PhantomData;
  12. use core::mem::{self, ManuallyDrop};
  13. #[cfg(not(no_global_oom_handling))]
  14. use core::ops::Deref;
  15. use core::ptr::{self, NonNull};
  16. use core::slice::{self};
  17. /// An iterator that moves out of a vector.
  18. ///
  19. /// This `struct` is created by the `into_iter` method on [`Vec`](super::Vec)
  20. /// (provided by the [`IntoIterator`] trait).
  21. ///
  22. /// # Example
  23. ///
  24. /// ```
  25. /// let v = vec![0, 1, 2];
  26. /// let iter: std::vec::IntoIter<_> = v.into_iter();
  27. /// ```
  28. #[stable(feature = "rust1", since = "1.0.0")]
  29. #[rustc_insignificant_dtor]
  30. pub struct IntoIter<
  31. T,
  32. #[unstable(feature = "allocator_api", issue = "32838")] A: Allocator = Global,
  33. > {
  34. pub(super) buf: NonNull<T>,
  35. pub(super) phantom: PhantomData<T>,
  36. pub(super) cap: usize,
  37. // the drop impl reconstructs a RawVec from buf, cap and alloc
  38. // to avoid dropping the allocator twice we need to wrap it into ManuallyDrop
  39. pub(super) alloc: ManuallyDrop<A>,
  40. pub(super) ptr: *const T,
  41. pub(super) end: *const T,
  42. }
  43. #[stable(feature = "vec_intoiter_debug", since = "1.13.0")]
  44. impl<T: fmt::Debug, A: Allocator> fmt::Debug for IntoIter<T, A> {
  45. fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
  46. f.debug_tuple("IntoIter").field(&self.as_slice()).finish()
  47. }
  48. }
  49. impl<T, A: Allocator> IntoIter<T, A> {
  50. /// Returns the remaining items of this iterator as a slice.
  51. ///
  52. /// # Examples
  53. ///
  54. /// ```
  55. /// let vec = vec!['a', 'b', 'c'];
  56. /// let mut into_iter = vec.into_iter();
  57. /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
  58. /// let _ = into_iter.next().unwrap();
  59. /// assert_eq!(into_iter.as_slice(), &['b', 'c']);
  60. /// ```
  61. #[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")]
  62. pub fn as_slice(&self) -> &[T] {
  63. unsafe { slice::from_raw_parts(self.ptr, self.len()) }
  64. }
  65. /// Returns the remaining items of this iterator as a mutable slice.
  66. ///
  67. /// # Examples
  68. ///
  69. /// ```
  70. /// let vec = vec!['a', 'b', 'c'];
  71. /// let mut into_iter = vec.into_iter();
  72. /// assert_eq!(into_iter.as_slice(), &['a', 'b', 'c']);
  73. /// into_iter.as_mut_slice()[2] = 'z';
  74. /// assert_eq!(into_iter.next().unwrap(), 'a');
  75. /// assert_eq!(into_iter.next().unwrap(), 'b');
  76. /// assert_eq!(into_iter.next().unwrap(), 'z');
  77. /// ```
  78. #[stable(feature = "vec_into_iter_as_slice", since = "1.15.0")]
  79. pub fn as_mut_slice(&mut self) -> &mut [T] {
  80. unsafe { &mut *self.as_raw_mut_slice() }
  81. }
  82. /// Returns a reference to the underlying allocator.
  83. #[unstable(feature = "allocator_api", issue = "32838")]
  84. #[inline]
  85. pub fn allocator(&self) -> &A {
  86. &self.alloc
  87. }
  88. fn as_raw_mut_slice(&mut self) -> *mut [T] {
  89. ptr::slice_from_raw_parts_mut(self.ptr as *mut T, self.len())
  90. }
  91. /// Drops remaining elements and relinquishes the backing allocation.
  92. ///
  93. /// This is roughly equivalent to the following, but more efficient
  94. ///
  95. /// ```
  96. /// # let mut into_iter = Vec::<u8>::with_capacity(10).into_iter();
  97. /// (&mut into_iter).for_each(core::mem::drop);
  98. /// unsafe { core::ptr::write(&mut into_iter, Vec::new().into_iter()); }
  99. /// ```
  100. ///
  101. /// This method is used by in-place iteration, refer to the vec::in_place_collect
  102. /// documentation for an overview.
  103. #[cfg(not(no_global_oom_handling))]
  104. pub(super) fn forget_allocation_drop_remaining(&mut self) {
  105. let remaining = self.as_raw_mut_slice();
  106. // overwrite the individual fields instead of creating a new
  107. // struct and then overwriting &mut self.
  108. // this creates less assembly
  109. self.cap = 0;
  110. self.buf = unsafe { NonNull::new_unchecked(RawVec::NEW.ptr()) };
  111. self.ptr = self.buf.as_ptr();
  112. self.end = self.buf.as_ptr();
  113. unsafe {
  114. ptr::drop_in_place(remaining);
  115. }
  116. }
  117. /// Forgets to Drop the remaining elements while still allowing the backing allocation to be freed.
  118. #[allow(dead_code)]
  119. pub(crate) fn forget_remaining_elements(&mut self) {
  120. self.ptr = self.end;
  121. }
  122. }
  123. #[stable(feature = "vec_intoiter_as_ref", since = "1.46.0")]
  124. impl<T, A: Allocator> AsRef<[T]> for IntoIter<T, A> {
  125. fn as_ref(&self) -> &[T] {
  126. self.as_slice()
  127. }
  128. }
  129. #[stable(feature = "rust1", since = "1.0.0")]
  130. unsafe impl<T: Send, A: Allocator + Send> Send for IntoIter<T, A> {}
  131. #[stable(feature = "rust1", since = "1.0.0")]
  132. unsafe impl<T: Sync, A: Allocator + Sync> Sync for IntoIter<T, A> {}
  133. #[stable(feature = "rust1", since = "1.0.0")]
  134. impl<T, A: Allocator> Iterator for IntoIter<T, A> {
  135. type Item = T;
  136. #[inline]
  137. fn next(&mut self) -> Option<T> {
  138. if self.ptr as *const _ == self.end {
  139. None
  140. } else if mem::size_of::<T>() == 0 {
  141. // purposefully don't use 'ptr.offset' because for
  142. // vectors with 0-size elements this would return the
  143. // same pointer.
  144. self.ptr = unsafe { arith_offset(self.ptr as *const i8, 1) as *mut T };
  145. // Make up a value of this ZST.
  146. Some(unsafe { mem::zeroed() })
  147. } else {
  148. let old = self.ptr;
  149. self.ptr = unsafe { self.ptr.offset(1) };
  150. Some(unsafe { ptr::read(old) })
  151. }
  152. }
  153. #[inline]
  154. fn size_hint(&self) -> (usize, Option<usize>) {
  155. let exact = if mem::size_of::<T>() == 0 {
  156. self.end.addr().wrapping_sub(self.ptr.addr())
  157. } else {
  158. unsafe { self.end.sub_ptr(self.ptr) }
  159. };
  160. (exact, Some(exact))
  161. }
  162. #[inline]
  163. fn advance_by(&mut self, n: usize) -> Result<(), usize> {
  164. let step_size = self.len().min(n);
  165. let to_drop = ptr::slice_from_raw_parts_mut(self.ptr as *mut T, step_size);
  166. if mem::size_of::<T>() == 0 {
  167. // SAFETY: due to unchecked casts of unsigned amounts to signed offsets the wraparound
  168. // effectively results in unsigned pointers representing positions 0..usize::MAX,
  169. // which is valid for ZSTs.
  170. self.ptr = unsafe { arith_offset(self.ptr as *const i8, step_size as isize) as *mut T }
  171. } else {
  172. // SAFETY: the min() above ensures that step_size is in bounds
  173. self.ptr = unsafe { self.ptr.add(step_size) };
  174. }
  175. // SAFETY: the min() above ensures that step_size is in bounds
  176. unsafe {
  177. ptr::drop_in_place(to_drop);
  178. }
  179. if step_size < n {
  180. return Err(step_size);
  181. }
  182. Ok(())
  183. }
  184. #[inline]
  185. fn count(self) -> usize {
  186. self.len()
  187. }
  188. unsafe fn __iterator_get_unchecked(&mut self, i: usize) -> Self::Item
  189. where
  190. Self: TrustedRandomAccessNoCoerce,
  191. {
  192. // SAFETY: the caller must guarantee that `i` is in bounds of the
  193. // `Vec<T>`, so `i` cannot overflow an `isize`, and the `self.ptr.add(i)`
  194. // is guaranteed to pointer to an element of the `Vec<T>` and
  195. // thus guaranteed to be valid to dereference.
  196. //
  197. // Also note the implementation of `Self: TrustedRandomAccess` requires
  198. // that `T: Copy` so reading elements from the buffer doesn't invalidate
  199. // them for `Drop`.
  200. unsafe {
  201. if mem::size_of::<T>() == 0 { mem::zeroed() } else { ptr::read(self.ptr.add(i)) }
  202. }
  203. }
  204. }
  205. #[stable(feature = "rust1", since = "1.0.0")]
  206. impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
  207. #[inline]
  208. fn next_back(&mut self) -> Option<T> {
  209. if self.end == self.ptr {
  210. None
  211. } else if mem::size_of::<T>() == 0 {
  212. // See above for why 'ptr.offset' isn't used
  213. self.end = unsafe { arith_offset(self.end as *const i8, -1) as *mut T };
  214. // Make up a value of this ZST.
  215. Some(unsafe { mem::zeroed() })
  216. } else {
  217. self.end = unsafe { self.end.offset(-1) };
  218. Some(unsafe { ptr::read(self.end) })
  219. }
  220. }
  221. #[inline]
  222. fn advance_back_by(&mut self, n: usize) -> Result<(), usize> {
  223. let step_size = self.len().min(n);
  224. if mem::size_of::<T>() == 0 {
  225. // SAFETY: same as for advance_by()
  226. self.end = unsafe {
  227. arith_offset(self.end as *const i8, step_size.wrapping_neg() as isize) as *mut T
  228. }
  229. } else {
  230. // SAFETY: same as for advance_by()
  231. self.end = unsafe { self.end.offset(step_size.wrapping_neg() as isize) };
  232. }
  233. let to_drop = ptr::slice_from_raw_parts_mut(self.end as *mut T, step_size);
  234. // SAFETY: same as for advance_by()
  235. unsafe {
  236. ptr::drop_in_place(to_drop);
  237. }
  238. if step_size < n {
  239. return Err(step_size);
  240. }
  241. Ok(())
  242. }
  243. }
  244. #[stable(feature = "rust1", since = "1.0.0")]
  245. impl<T, A: Allocator> ExactSizeIterator for IntoIter<T, A> {
  246. fn is_empty(&self) -> bool {
  247. self.ptr == self.end
  248. }
  249. }
  250. #[stable(feature = "fused", since = "1.26.0")]
  251. impl<T, A: Allocator> FusedIterator for IntoIter<T, A> {}
  252. #[unstable(feature = "trusted_len", issue = "37572")]
  253. unsafe impl<T, A: Allocator> TrustedLen for IntoIter<T, A> {}
  254. #[doc(hidden)]
  255. #[unstable(issue = "none", feature = "std_internals")]
  256. #[rustc_unsafe_specialization_marker]
  257. pub trait NonDrop {}
  258. // T: Copy as approximation for !Drop since get_unchecked does not advance self.ptr
  259. // and thus we can't implement drop-handling
  260. #[unstable(issue = "none", feature = "std_internals")]
  261. impl<T: Copy> NonDrop for T {}
  262. #[doc(hidden)]
  263. #[unstable(issue = "none", feature = "std_internals")]
  264. // TrustedRandomAccess (without NoCoerce) must not be implemented because
  265. // subtypes/supertypes of `T` might not be `NonDrop`
  266. unsafe impl<T, A: Allocator> TrustedRandomAccessNoCoerce for IntoIter<T, A>
  267. where
  268. T: NonDrop,
  269. {
  270. const MAY_HAVE_SIDE_EFFECT: bool = false;
  271. }
  272. #[cfg(not(no_global_oom_handling))]
  273. #[stable(feature = "vec_into_iter_clone", since = "1.8.0")]
  274. impl<T: Clone, A: Allocator + Clone> Clone for IntoIter<T, A> {
  275. #[cfg(not(test))]
  276. fn clone(&self) -> Self {
  277. self.as_slice().to_vec_in(self.alloc.deref().clone()).into_iter()
  278. }
  279. #[cfg(test)]
  280. fn clone(&self) -> Self {
  281. crate::slice::to_vec(self.as_slice(), self.alloc.deref().clone()).into_iter()
  282. }
  283. }
  284. #[stable(feature = "rust1", since = "1.0.0")]
  285. unsafe impl<#[may_dangle] T, A: Allocator> Drop for IntoIter<T, A> {
  286. fn drop(&mut self) {
  287. struct DropGuard<'a, T, A: Allocator>(&'a mut IntoIter<T, A>);
  288. impl<T, A: Allocator> Drop for DropGuard<'_, T, A> {
  289. fn drop(&mut self) {
  290. unsafe {
  291. // `IntoIter::alloc` is not used anymore after this and will be dropped by RawVec
  292. let alloc = ManuallyDrop::take(&mut self.0.alloc);
  293. // RawVec handles deallocation
  294. let _ = RawVec::from_raw_parts_in(self.0.buf.as_ptr(), self.0.cap, alloc);
  295. }
  296. }
  297. }
  298. let guard = DropGuard(self);
  299. // destroy the remaining elements
  300. unsafe {
  301. ptr::drop_in_place(guard.0.as_raw_mut_slice());
  302. }
  303. // now `guard` will be dropped and do the rest
  304. }
  305. }
  306. // In addition to the SAFETY invariants of the following three unsafe traits
  307. // also refer to the vec::in_place_collect module documentation to get an overview
  308. #[unstable(issue = "none", feature = "inplace_iteration")]
  309. #[doc(hidden)]
  310. unsafe impl<T, A: Allocator> InPlaceIterable for IntoIter<T, A> {}
  311. #[unstable(issue = "none", feature = "inplace_iteration")]
  312. #[doc(hidden)]
  313. unsafe impl<T, A: Allocator> SourceIter for IntoIter<T, A> {
  314. type Source = Self;
  315. #[inline]
  316. unsafe fn as_inner(&mut self) -> &mut Self::Source {
  317. self
  318. }
  319. }
  320. #[cfg(not(no_global_oom_handling))]
  321. unsafe impl<T> AsVecIntoIter for IntoIter<T> {
  322. type Item = T;
  323. fn as_into_iter(&mut self) -> &mut IntoIter<Self::Item> {
  324. self
  325. }
  326. }