use std::ops;
-use rustc::ty::layout::Size;
+use rustc_target::abi::Size;
#[derive(Clone, Debug)]
struct Elem<T> {
/// Provides read-only iteration over everything in the given range. This does
/// *not* split items if they overlap with the edges. Do not use this to mutate
/// through interior mutability.
- pub fn iter<'a>(&'a self, offset: Size, len: Size) -> impl Iterator<Item = &'a T> + 'a {
+ ///
+ /// The iterator also provides the offset of the given element.
+ 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| &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,
/// this will split entries in the map that are only partially hit by the given range,
/// to make sure that when they are mutated, the effect is constrained to the given range.
/// Moreover, this will opportunistically merge neighbouring equal blocks.
- pub fn iter_mut<'a>(
- &'a mut self,
- offset: Size,
- len: Size,
- ) -> impl Iterator<Item = &'a mut T> + 'a
+ ///
+ /// The iterator also provides the offset of the given element.
+ pub fn iter_mut(&mut self, offset: Size, len: Size) -> impl Iterator<Item = (Size, &mut T)>
where
T: Clone + PartialEq,
{
// Now we yield the slice. `end` is inclusive.
&mut self.v[first_idx..=end_idx]
};
- slice.iter_mut().map(|elem| &mut elem.data)
+ slice.iter_mut().map(|elem| (Size::from_bytes(elem.range.start), &mut elem.data))
}
}
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()
}
fn basic_insert() {
let mut map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
// Insert.
- for x in map.iter_mut(Size::from_bytes(10), Size::from_bytes(1)) {
+ for (_, x) in map.iter_mut(Size::from_bytes(10), Size::from_bytes(1)) {
*x = 42;
}
// Check.
assert_eq!(map.v.len(), 3);
// Insert with size 0.
- for x in map.iter_mut(Size::from_bytes(10), Size::from_bytes(0)) {
+ for (_, x) in map.iter_mut(Size::from_bytes(10), Size::from_bytes(0)) {
*x = 19;
}
- for x in map.iter_mut(Size::from_bytes(11), Size::from_bytes(0)) {
+ for (_, x) in map.iter_mut(Size::from_bytes(11), Size::from_bytes(0)) {
*x = 19;
}
assert_eq!(to_vec(&map, 10, 2), vec![42, -1]);
#[test]
fn gaps() {
let mut map = RangeMap::<i32>::new(Size::from_bytes(20), -1);
- for x in map.iter_mut(Size::from_bytes(11), Size::from_bytes(1)) {
+ for (_, x) in map.iter_mut(Size::from_bytes(11), Size::from_bytes(1)) {
*x = 42;
}
- for x in map.iter_mut(Size::from_bytes(15), Size::from_bytes(1)) {
+ for (_, x) in map.iter_mut(Size::from_bytes(15), Size::from_bytes(1)) {
*x = 43;
}
assert_eq!(map.v.len(), 5);
assert_eq!(to_vec(&map, 10, 10), vec![-1, 42, -1, -1, -1, 43, -1, -1, -1, -1]);
- for x in map.iter_mut(Size::from_bytes(10), Size::from_bytes(10)) {
+ for (_, x) in map.iter_mut(Size::from_bytes(10), Size::from_bytes(10)) {
if *x < 42 {
*x = 23;
}
assert_eq!(to_vec(&map, 10, 10), vec![23, 42, 23, 23, 23, 43, 23, 23, 23, 23]);
assert_eq!(to_vec(&map, 13, 5), vec![23, 23, 43, 23, 23]);
- for x in map.iter_mut(Size::from_bytes(15), Size::from_bytes(5)) {
+ for (_, x) in map.iter_mut(Size::from_bytes(15), Size::from_bytes(5)) {
*x = 19;
}
assert_eq!(map.v.len(), 6);
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));
+ }
}