Skip to content

Commit 364c4b4

Browse files
committed
Added lenght fn for AddrRange & made start & end pub
1 parent 138efd7 commit 364c4b4

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/lib.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ pub trait MemoryAddress:
7171
+ BitOr<Output = Self::RAW>
7272
+ BitXor<Output = Self::RAW>
7373
+ Debug
74-
+ From<u8>;
74+
+ From<u8>
75+
+ TryInto<usize, Error: Debug>;
7576

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

9899
/// A memory range.
99100
pub struct AddrRange<T: MemoryAddress> {
100-
start: T,
101-
end: T,
101+
/// Starting address
102+
pub start: T,
103+
/// End address (exclusive)
104+
pub end: T,
102105
}
103106
impl<T: MemoryAddress> AddrRange<T> {
104107
/// Construct a new memory range from `start` (inclusive) to `end` (exclusive).
@@ -121,6 +124,15 @@ impl<T: MemoryAddress> AddrRange<T> {
121124
pub fn contains(&self, element: &T) -> bool {
122125
element.raw() >= self.start.raw() && element.raw() < self.end.raw()
123126
}
127+
128+
/// Amount of addresses in the range.
129+
///
130+
/// `AddrRange`s are half open, so the range from `0x0` to `0x1` has length 1.
131+
pub fn length(&self) -> usize {
132+
(self.end.raw() - self.start.raw())
133+
.try_into()
134+
.expect("address range is larger than the architecture's usize")
135+
}
124136
}
125137

126138
/// An iterator over a memory range

0 commit comments

Comments
 (0)