[vmbase] Move RangeExt to vmbase for reuse

in both pvmfw and rialto.

No behavior change in this cl.

Bug: 284462758
Test: m pvmfw_img
Change-Id: Ifda7f4a2a08ebb6646c3c7e189a77c5ff3fe74a3
diff --git a/vmbase/src/util.rs b/vmbase/src/util.rs
index 7396edc..8c230a1 100644
--- a/vmbase/src/util.rs
+++ b/vmbase/src/util.rs
@@ -14,6 +14,8 @@
 
 //! Utility functions.
 
+use core::ops::Range;
+
 /// Flatten [[T; N]] into &[T]
 /// TODO: use slice::flatten when it graduates from experimental
 pub fn flatten<T, const N: usize>(original: &[[T; N]]) -> &[T] {
@@ -70,3 +72,22 @@
 
     r.checked_div(den)
 }
+
+/// Trait to check containment of one range within another.
+pub trait RangeExt {
+    /// Returns true if `self` is contained within the `other` range.
+    fn is_within(&self, other: &Self) -> bool;
+
+    /// Returns true if `self` overlaps with the `other` range.
+    fn overlaps(&self, other: &Self) -> bool;
+}
+
+impl<T: PartialOrd> RangeExt for Range<T> {
+    fn is_within(&self, other: &Self) -> bool {
+        self.start >= other.start && self.end <= other.end
+    }
+
+    fn overlaps(&self, other: &Self) -> bool {
+        self.start < other.end && other.start < self.end
+    }
+}