]> git.lizzy.rs Git - rust.git/blobdiff - src/range_map.rs
Auto merge of #2426 - saethlin:unix-exec, r=RalfJung
[rust.git] / src / range_map.rs
index 607c830530e1f15f63fb6a093c6b01fe8466f34d..c77ea63b0873f1eb3f83c338e10e158129ca6cca 100644 (file)
@@ -63,7 +63,7 @@ fn find_offset(&self, offset: u64) -> usize {
     /// 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.
@@ -77,10 +77,17 @@ pub fn iter<'a>(&'a self, offset: Size, len: Size) -> impl Iterator<Item = (Size
         };
         // 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)
     }
 
@@ -107,7 +114,7 @@ fn split_index(&mut self, index: usize, split_offset: u64) -> bool
         // 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,
@@ -116,11 +123,7 @@ fn split_index(&mut self, index: usize, split_offset: u64) -> bool
     /// 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,
     {
@@ -213,7 +216,9 @@ mod tests {
     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()
     }
 
@@ -267,7 +272,9 @@ fn gaps() {
         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]
         );
 
@@ -276,4 +283,18 @@ fn gaps() {
         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));
+    }
 }