Function memory_range::merge_adjacent_ranges

source ·
pub fn merge_adjacent_ranges<T: PartialEq>(
    ranges: impl IntoIterator<Item = (MemoryRange, T)>,
) -> impl Iterator<Item = (MemoryRange, T)>
Expand description

Similar to flatten_ranges, but considers ranges non-equivalent if their associated tags differ.

Panics if the input ranges are not sorted by their start address, or if ranges overlap.

§Example


#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Color {
   Red,
   Blue,
}

let ranges = [
    (MemoryRange::new(0x1000..0x2000), Color::Red),
    (MemoryRange::new(0x2000..0x5000), Color::Red),
    (MemoryRange::new(0x5000..0x6000), Color::Blue),
    (MemoryRange::new(0x8000..0x9000), Color::Red),
];
let flattened = [
    (MemoryRange::new(0x1000..0x5000), Color::Red),
    (MemoryRange::new(0x5000..0x6000), Color::Blue),
    (MemoryRange::new(0x8000..0x9000), Color::Red),
];
assert!(merge_adjacent_ranges(ranges).eq(flattened));