2 use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess, TrustedRandomAccessNoCoerce};
3 use core::marker::PhantomData;
5 use super::{count, wrap_index, RingSlices};
7 /// A mutable iterator over the elements of a `VecDeque`.
9 /// This `struct` is created by the [`iter_mut`] method on [`super::VecDeque`]. See its
10 /// documentation for more.
12 /// [`iter_mut`]: super::VecDeque::iter_mut
13 #[stable(feature = "rust1", since = "1.0.0")]
14 pub struct IterMut<'a, T: 'a> {
15 // Internal safety invariant: the entire slice is dereferencable.
19 phantom: PhantomData<&'a mut [T]>,
22 impl<'a, T> IterMut<'a, T> {
23 pub(super) unsafe fn new(
27 phantom: PhantomData<&'a mut [T]>,
29 IterMut { ring, tail, head, phantom }
33 // SAFETY: we do nothing thread-local and there is no interior mutability,
34 // so the usual structural `Send`/`Sync` apply.
35 #[stable(feature = "rust1", since = "1.0.0")]
36 unsafe impl<T: Send> Send for IterMut<'_, T> {}
37 #[stable(feature = "rust1", since = "1.0.0")]
38 unsafe impl<T: Sync> Sync for IterMut<'_, T> {}
40 #[stable(feature = "collection_debug", since = "1.17.0")]
41 impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
42 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43 let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
44 // SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
45 // The `IterMut` invariant also ensures everything is dereferencable.
46 let (front, back) = unsafe { (&*front, &*back) };
47 f.debug_tuple("IterMut").field(&front).field(&back).finish()
51 #[stable(feature = "rust1", since = "1.0.0")]
52 impl<'a, T> Iterator for IterMut<'a, T> {
53 type Item = &'a mut T;
56 fn next(&mut self) -> Option<&'a mut T> {
57 if self.tail == self.head {
61 self.tail = wrap_index(self.tail.wrapping_add(1), self.ring.len());
64 let elem = self.ring.get_unchecked_mut(tail);
70 fn size_hint(&self) -> (usize, Option<usize>) {
71 let len = count(self.tail, self.head, self.ring.len());
75 fn fold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc
77 F: FnMut(Acc, Self::Item) -> Acc,
79 let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
80 // SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
81 // The `IterMut` invariant also ensures everything is dereferencable.
82 let (front, back) = unsafe { (&mut *front, &mut *back) };
83 accum = front.iter_mut().fold(accum, &mut f);
84 back.iter_mut().fold(accum, &mut f)
87 fn nth(&mut self, n: usize) -> Option<Self::Item> {
88 if n >= count(self.tail, self.head, self.ring.len()) {
89 self.tail = self.head;
92 self.tail = wrap_index(self.tail.wrapping_add(n), self.ring.len());
98 fn last(mut self) -> Option<&'a mut T> {
104 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item {
105 // Safety: The TrustedRandomAccess contract requires that callers only pass an index
106 // that is in bounds.
108 let idx = wrap_index(self.tail.wrapping_add(idx), self.ring.len());
109 &mut *self.ring.get_unchecked_mut(idx)
114 #[stable(feature = "rust1", since = "1.0.0")]
115 impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
117 fn next_back(&mut self) -> Option<&'a mut T> {
118 if self.tail == self.head {
121 self.head = wrap_index(self.head.wrapping_sub(1), self.ring.len());
124 let elem = self.ring.get_unchecked_mut(self.head);
129 fn rfold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc
131 F: FnMut(Acc, Self::Item) -> Acc,
133 let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
134 // SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
135 // The `IterMut` invariant also ensures everything is dereferencable.
136 let (front, back) = unsafe { (&mut *front, &mut *back) };
137 accum = back.iter_mut().rfold(accum, &mut f);
138 front.iter_mut().rfold(accum, &mut f)
142 #[stable(feature = "rust1", since = "1.0.0")]
143 impl<T> ExactSizeIterator for IterMut<'_, T> {
144 fn is_empty(&self) -> bool {
145 self.head == self.tail
149 #[stable(feature = "fused", since = "1.26.0")]
150 impl<T> FusedIterator for IterMut<'_, T> {}
152 #[unstable(feature = "trusted_len", issue = "37572")]
153 unsafe impl<T> TrustedLen for IterMut<'_, T> {}
156 #[unstable(feature = "trusted_random_access", issue = "none")]
157 unsafe impl<T> TrustedRandomAccess for IterMut<'_, T> {}
160 #[unstable(feature = "trusted_random_access", issue = "none")]
161 unsafe impl<T> TrustedRandomAccessNoCoerce for IterMut<'_, T> {
162 const MAY_HAVE_SIDE_EFFECT: bool = false;