diff --git a/src/machine.rs b/src/machine.rs index 480156b045..fc936fa8b1 100644 --- a/src/machine.rs +++ b/src/machine.rs @@ -320,19 +320,20 @@ impl Mappings { self.v.iter() } + fn binary_search(&self, addr: Size) -> Result { + self.v.binary_search_by(|map| { + if map.range.start > addr { + Ordering::Greater + } else if map.range.end < addr { + Ordering::Less + } else { + Ordering::Equal + } + }) + } + pub fn insert(&mut self, new: Mapping) { - let idx = self - .v - .binary_search_by(|map| { - if map.range.start > new.range.start { - Ordering::Greater - } else if map.range.end < new.range.start { - Ordering::Less - } else { - Ordering::Equal - } - }) - .unwrap_err(); + let idx = self.binary_search(new.range.start).unwrap_err(); self.v.insert(idx, new); } @@ -340,18 +341,7 @@ impl Mappings { if !self.v.get(0)?.contains(addr) && !self.v.last()?.contains(addr) { return None; } - let idx = self - .v - .binary_search_by(|map| { - if map.range.start > addr { - Ordering::Greater - } else if map.range.end < addr { - Ordering::Less - } else { - Ordering::Equal - } - }) - .ok()?; + let idx = self.binary_search(addr).ok()?; Some(self.v.remove(idx)) } @@ -359,18 +349,7 @@ impl Mappings { if !self.v.get(0)?.contains(addr) && !self.v.last()?.contains(addr) { return None; } - let idx = self - .v - .binary_search_by(|map| { - if map.range.start > addr { - Ordering::Greater - } else if map.range.end < addr { - Ordering::Less - } else { - Ordering::Equal - } - }) - .ok()?; + let idx = self.binary_search(addr).ok()?; self.v.get(idx) } }