Skip to content

Commit 101498c

Browse files
committed
Tweak VecDeque's IterMut implementation
So it is symmetric to its `Iter` implementation. Also kills an FIXME.
1 parent 610d169 commit 101498c

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

src/libcollections/vec_deque.rs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ use core::cmp::Ordering;
2424
use core::default::Default;
2525
use core::fmt;
2626
use core::iter::{self, repeat, FromIterator, IntoIterator, RandomAccessIterator};
27-
use core::marker;
2827
use core::mem;
2928
use core::num::{Int, UnsignedInt};
3029
use core::ops::{Index, IndexMut};
@@ -545,9 +544,7 @@ impl<T> VecDeque<T> {
545544
IterMut {
546545
tail: self.tail,
547546
head: self.head,
548-
cap: self.cap,
549-
ptr: *self.ptr,
550-
marker: marker::PhantomData,
547+
ring: unsafe { self.buffer_as_mut_slice() },
551548
}
552549
}
553550

@@ -1515,17 +1512,12 @@ impl<'a, T> RandomAccessIterator for Iter<'a, T> {
15151512
}
15161513
}
15171514

1518-
// FIXME This was implemented differently from Iter because of a problem
1519-
// with returning the mutable reference. I couldn't find a way to
1520-
// make the lifetime checker happy so, but there should be a way.
15211515
/// `VecDeque` mutable iterator.
15221516
#[stable(feature = "rust1", since = "1.0.0")]
15231517
pub struct IterMut<'a, T:'a> {
1524-
ptr: *mut T,
1518+
ring: &'a mut [T],
15251519
tail: usize,
15261520
head: usize,
1527-
cap: usize,
1528-
marker: marker::PhantomData<&'a mut T>,
15291521
}
15301522

15311523
#[stable(feature = "rust1", since = "1.0.0")]
@@ -1538,16 +1530,17 @@ impl<'a, T> Iterator for IterMut<'a, T> {
15381530
return None;
15391531
}
15401532
let tail = self.tail;
1541-
self.tail = wrap_index(self.tail + 1, self.cap);
1533+
self.tail = wrap_index(self.tail + 1, self.ring.len());
15421534

15431535
unsafe {
1544-
Some(&mut *self.ptr.offset(tail as isize))
1536+
let elem = self.ring.get_unchecked_mut(tail);
1537+
Some(&mut *(elem as *mut _))
15451538
}
15461539
}
15471540

15481541
#[inline]
15491542
fn size_hint(&self) -> (usize, Option<usize>) {
1550-
let len = count(self.tail, self.head, self.cap);
1543+
let len = count(self.tail, self.head, self.ring.len());
15511544
(len, Some(len))
15521545
}
15531546
}
@@ -1559,10 +1552,11 @@ impl<'a, T> DoubleEndedIterator for IterMut<'a, T> {
15591552
if self.tail == self.head {
15601553
return None;
15611554
}
1562-
self.head = wrap_index(self.head - 1, self.cap);
1555+
self.head = wrap_index(self.head - 1, self.ring.len());
15631556

15641557
unsafe {
1565-
Some(&mut *self.ptr.offset(self.head as isize))
1558+
let elem = self.ring.get_unchecked_mut(self.head);
1559+
Some(&mut *(elem as *mut _))
15661560
}
15671561
}
15681562
}

0 commit comments

Comments
 (0)