/// through interior mutability.
///
/// The iterator also provides the offset of the given element.
- pub fn iter<'a>(&'a self, offset: Size, len: Size) -> impl Iterator<Item = (Size, &'a T)> + 'a {
+ pub fn iter(&self, offset: Size, len: Size) -> impl Iterator<Item = (Size, &T)> {
let offset = offset.bytes();
let len = len.bytes();
// Compute a slice starting with the elements we care about.
};
// The first offset that is not included any more.
let end = offset + len;
- slice.iter().take_while(move |elem| elem.range.start < end).map(|elem| (Size::from_bytes(elem.range.start), &elem.data))
+ assert!(
+ end <= self.v.last().unwrap().range.end,
+ "iterating beyond the bounds of this RangeMap"
+ );
+ slice
+ .iter()
+ .take_while(move |elem| elem.range.start < end)
+ .map(|elem| (Size::from_bytes(elem.range.start), &elem.data))
}
- pub fn iter_mut_all<'a>(&'a mut self) -> impl Iterator<Item = &'a mut T> + 'a {
+ pub fn iter_mut_all(&mut self) -> impl Iterator<Item = &mut T> {
self.v.iter_mut().map(|elem| &mut elem.data)
}
// Copy the data, and insert second element.
let second = Elem { range: second_range, data: elem.data.clone() };
self.v.insert(index + 1, second);
- return true;
+ true
}
/// Provides mutable iteration over everything in the given range. As a side-effect,
/// Moreover, this will opportunistically merge neighbouring equal blocks.
///
/// The iterator also provides the offset of the given element.
- pub fn iter_mut<'a>(
- &'a mut self,
- offset: Size,
- len: Size,
- ) -> impl Iterator<Item = (Size, &'a mut T)> + 'a
+ pub fn iter_mut(&mut self, offset: Size, len: Size) -> impl Iterator<Item = (Size, &mut T)>
where
T: Clone + PartialEq,
{
fn to_vec<T: Copy>(map: &RangeMap<T>, offset: u64, len: u64) -> Vec<T> {
(offset..offset + len)
.into_iter()
- .map(|i| map.iter(Size::from_bytes(i), Size::from_bytes(1)).next().map(|(_, &t)| t).unwrap())
+ .map(|i| {
+ map.iter(Size::from_bytes(i), Size::from_bytes(1)).next().map(|(_, &t)| t).unwrap()
+ })
.collect()
}
assert_eq!(to_vec(&map, 10, 10), vec![23, 42, 23, 23, 23, 19, 19, 19, 19, 19]);
// Should be seeing two blocks with 19.
assert_eq!(
- map.iter(Size::from_bytes(15), Size::from_bytes(2)).map(|(_, &t)| t).collect::<Vec<_>>(),
+ map.iter(Size::from_bytes(15), Size::from_bytes(2))
+ .map(|(_, &t)| t)
+ .collect::<Vec<_>>(),
vec![19, 19]
);
assert_eq!(map.v.len(), 5);
assert_eq!(to_vec(&map, 10, 10), vec![23, 42, 23, 23, 23, 19, 19, 19, 19, 19]);
}
+
+ #[test]
+ #[should_panic]
+ fn out_of_range_iter_mut() {
+ let mut map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
+ let _ = map.iter_mut(Size::from_bytes(11), Size::from_bytes(11));
+ }
+
+ #[test]
+ #[should_panic]
+ fn out_of_range_iter() {
+ let map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
+ let _ = map.iter(Size::from_bytes(11), Size::from_bytes(11));
+ }
}