diff --git a/src/lib.rs b/src/lib.rs index d5e74e8..884d837 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -71,7 +71,8 @@ pub trait MemoryAddress: + BitOr + BitXor + Debug - + From; + + From + + TryInto; /// Get the raw underlying address value. fn raw(self) -> Self::RAW; @@ -97,8 +98,10 @@ impl fmt::Display for AddrRangeError { /// A memory range. pub struct AddrRange { - start: T, - end: T, + /// Starting address + pub start: T, + /// End address (exclusive) + pub end: T, } impl AddrRange { /// Construct a new memory range from `start` (inclusive) to `end` (exclusive). @@ -121,6 +124,15 @@ impl AddrRange { pub fn contains(&self, element: &T) -> bool { element.raw() >= self.start.raw() && element.raw() < self.end.raw() } + + /// Amount of addresses in the range. + /// + /// `AddrRange`s are half open, so the range from `0x0` to `0x1` has length 1. + pub fn length(&self) -> usize { + (self.end.raw() - self.start.raw()) + .try_into() + .expect("address range is larger than the architecture's usize") + } } /// An iterator over a memory range