2 use core::iter::{FusedIterator, TrustedLen, TrustedRandomAccess};
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.
16 pub(crate) ring: *mut [T],
17 pub(crate) tail: usize,
18 pub(crate) head: usize,
19 pub(crate) phantom: PhantomData<&'a mut [T]>,
22 // SAFETY: we do nothing thread-local and there is no interior mutability,
23 // so the usual structural `Send`/`Sync` apply.
24 #[stable(feature = "rust1", since = "1.0.0")]
25 unsafe impl<T: Send> Send for IterMut<'_, T> {}
26 #[stable(feature = "rust1", since = "1.0.0")]
27 unsafe impl<T: Sync> Sync for IterMut<'_, T> {}
29 #[stable(feature = "collection_debug", since = "1.17.0")]
30 impl<T: fmt::Debug> fmt::Debug for IterMut<'_, T> {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
33 // SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
34 // The `IterMut` invariant also ensures everything is dereferencable.
35 let (front, back) = unsafe { (&*front, &*back) };
36 f.debug_tuple("IterMut").field(&front).field(&back).finish()
40 #[stable(feature = "rust1", since = "1.0.0")]
41 impl<'a, T> Iterator for IterMut<'a, T> {
42 type Item = &'a mut T;
45 fn next(&mut self) -> Option<&'a mut T> {
46 if self.tail == self.head {
50 self.tail = wrap_index(self.tail.wrapping_add(1), self.ring.len());
53 let elem = self.ring.get_unchecked_mut(tail);
59 fn size_hint(&self) -> (usize, Option<usize>) {
60 let len = count(self.tail, self.head, self.ring.len());
64 fn fold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc
66 F: FnMut(Acc, Self::Item) -> Acc,
68 let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
69 // SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
70 // The `IterMut` invariant also ensures everything is dereferencable.
71 let (front, back) = unsafe { (&mut *front, &mut *back) };
72 accum = front.iter_mut().fold(accum, &mut f);
73 back.iter_mut().fold(accum, &mut f)
76 fn nth(&mut self, n: usize) -> Option<Self::Item> {
77 if n >= count(self.tail, self.head, self.ring.len()) {
78 self.tail = self.head;
81 self.tail = wrap_index(self.tail.wrapping_add(n), self.ring.len());
87 fn last(mut self) -> Option<&'a mut T> {
93 unsafe fn __iterator_get_unchecked(&mut self, idx: usize) -> Self::Item
95 Self: TrustedRandomAccess,
97 // Safety: The TrustedRandomAccess contract requires that callers only pass an index
100 let idx = wrap_index(self.tail.wrapping_add(idx), self.ring.len());
101 &mut *self.ring.get_unchecked_mut(idx)
106 #[stable(feature = "rust1", since = "1.0.0")]
107 impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
109 fn next_back(&mut self) -> Option<&'a mut T> {
110 if self.tail == self.head {
113 self.head = wrap_index(self.head.wrapping_sub(1), self.ring.len());
116 let elem = self.ring.get_unchecked_mut(self.head);
121 fn rfold<Acc, F>(self, mut accum: Acc, mut f: F) -> Acc
123 F: FnMut(Acc, Self::Item) -> Acc,
125 let (front, back) = RingSlices::ring_slices(self.ring, self.head, self.tail);
126 // SAFETY: these are the elements we have not handed out yet, so aliasing is fine.
127 // The `IterMut` invariant also ensures everything is dereferencable.
128 let (front, back) = unsafe { (&mut *front, &mut *back) };
129 accum = back.iter_mut().rfold(accum, &mut f);
130 front.iter_mut().rfold(accum, &mut f)
134 #[stable(feature = "rust1", since = "1.0.0")]
135 impl<T> ExactSizeIterator for IterMut<'_, T> {
136 fn is_empty(&self) -> bool {
137 self.head == self.tail
141 #[stable(feature = "fused", since = "1.26.0")]
142 impl<T> FusedIterator for IterMut<'_, T> {}
144 #[unstable(feature = "trusted_len", issue = "37572")]
145 unsafe impl<T> TrustedLen for IterMut<'_, T> {}
148 #[unstable(feature = "trusted_random_access", issue = "none")]
149 unsafe impl<T> TrustedRandomAccess for IterMut<'_, T> {
150 const MAY_HAVE_SIDE_EFFECT: bool = false;