Skip to content

Commit

Permalink
Added lenght fn for AddrRange & made start & end pub
Browse files Browse the repository at this point in the history
  • Loading branch information
jounathaen committed Oct 31, 2024
1 parent 138efd7 commit 364c4b4
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ pub trait MemoryAddress:
+ BitOr<Output = Self::RAW>
+ BitXor<Output = Self::RAW>
+ Debug
+ From<u8>;
+ From<u8>
+ TryInto<usize, Error: Debug>;

/// Get the raw underlying address value.
fn raw(self) -> Self::RAW;
Expand All @@ -97,8 +98,10 @@ impl fmt::Display for AddrRangeError {

/// A memory range.
pub struct AddrRange<T: MemoryAddress> {
start: T,
end: T,
/// Starting address
pub start: T,
/// End address (exclusive)
pub end: T,
}
impl<T: MemoryAddress> AddrRange<T> {
/// Construct a new memory range from `start` (inclusive) to `end` (exclusive).
Expand All @@ -121,6 +124,15 @@ impl<T: MemoryAddress> AddrRange<T> {
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
Expand Down

0 comments on commit 364c4b4

Please sign in to comment.