pub trait Address: AddressValue + Sized + Default + Copy + Eq + PartialEq + Ord + PartialOrd + BitAnd<<Self as AddressValue>::V, Output = Self> + BitOr<<Self as AddressValue>::V, Output = Self> {
Show 13 methods
// Required methods
fn new(addr: Self::V) -> Self;
fn raw_value(&self) -> Self::V;
fn checked_offset_from(&self, base: Self) -> Option<Self::V>;
fn checked_add(&self, other: Self::V) -> Option<Self>;
fn overflowing_add(&self, other: Self::V) -> (Self, bool);
fn unchecked_add(&self, offset: Self::V) -> Self;
fn checked_sub(&self, other: Self::V) -> Option<Self>;
fn overflowing_sub(&self, other: Self::V) -> (Self, bool);
fn unchecked_sub(&self, other: Self::V) -> Self;
// Provided methods
fn mask(&self, mask: Self::V) -> Self::V { ... }
fn unchecked_offset_from(&self, base: Self) -> Self::V { ... }
fn checked_align_up(&self, power_of_two: Self::V) -> Option<Self> { ... }
fn unchecked_align_up(&self, power_of_two: Self::V) -> Self { ... }
}
Expand description
Trait to represent an address within an address space.
To simplify the design and implementation, assume the same raw data type (AddressValue::V)
could be used to store address, size and offset for the address space. Thus the Address
trait
could be used to manage address, size and offset. On the other hand, type aliases may be
defined to improve code readability.
One design rule is applied to the Address
trait, namely that operators (+, -, &, | etc) are
not supported and it forces clients to explicitly invoke corresponding methods. But there are
always exceptions:
Address
(BitAnd|BitOr) AddressValue
are supported.
Required Methods§
sourcefn checked_offset_from(&self, base: Self) -> Option<Self::V>
fn checked_offset_from(&self, base: Self) -> Option<Self::V>
Computes the offset from this address to the given base address.
Returns None
if there is underflow.
sourcefn checked_add(&self, other: Self::V) -> Option<Self>
fn checked_add(&self, other: Self::V) -> Option<Self>
Computes self + other
, returning None
if overflow occurred.
sourcefn overflowing_add(&self, other: Self::V) -> (Self, bool)
fn overflowing_add(&self, other: Self::V) -> (Self, bool)
Computes self + other
.
Returns a tuple of the addition result along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped address is returned.
sourcefn unchecked_add(&self, offset: Self::V) -> Self
fn unchecked_add(&self, offset: Self::V) -> Self
Computes self + offset
.
In the event of overflow, follows standard Rust behavior, i.e. panic in debug builds, silently wrap in release builds.
Note that, unlike the unchecked_*
methods in std, this method never invokes undefined
behavior..
sourcefn checked_sub(&self, other: Self::V) -> Option<Self>
fn checked_sub(&self, other: Self::V) -> Option<Self>
Subtracts two addresses, checking for underflow. If underflow happens, None
is returned.
sourcefn overflowing_sub(&self, other: Self::V) -> (Self, bool)
fn overflowing_sub(&self, other: Self::V) -> (Self, bool)
Computes self - other
.
Returns a tuple of the subtraction result along with a boolean indicating whether an arithmetic overflow would occur. If an overflow would have occurred then the wrapped address is returned.
sourcefn unchecked_sub(&self, other: Self::V) -> Self
fn unchecked_sub(&self, other: Self::V) -> Self
Computes self - other
.
In the event of underflow, follows standard Rust behavior, i.e. panic in debug builds, silently wrap in release builds.
Note that, unlike the unchecked_*
methods in std, this method never invokes undefined
behavior.
Provided Methods§
sourcefn mask(&self, mask: Self::V) -> Self::V
fn mask(&self, mask: Self::V) -> Self::V
Returns the bitwise and of the address with the given mask.
sourcefn unchecked_offset_from(&self, base: Self) -> Self::V
fn unchecked_offset_from(&self, base: Self) -> Self::V
Computes the offset from this address to the given base address.
In the event of overflow, follows standard Rust behavior, i.e. panic in debug builds, silently wrap in release builds.
Note that, unlike the unchecked_*
methods in std, this method never invokes undefined
behavior.
§Examples
let base = GuestAddress(0x100);
let addr = GuestAddress(0x150);
assert_eq!(addr.unchecked_offset_from(base), 0x50);
sourcefn checked_align_up(&self, power_of_two: Self::V) -> Option<Self>
fn checked_align_up(&self, power_of_two: Self::V) -> Option<Self>
Returns self, aligned to the given power of two.
sourcefn unchecked_align_up(&self, power_of_two: Self::V) -> Self
fn unchecked_align_up(&self, power_of_two: Self::V) -> Self
Returns self, aligned to the given power of two. Only use this when the result is guaranteed not to overflow.