Trait vm_memory::address::Address

source ·
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§

source

fn new(addr: Self::V) -> Self

Creates an address from a raw address value.

source

fn raw_value(&self) -> Self::V

Returns the raw value of the address.

source

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.

source

fn checked_add(&self, other: Self::V) -> Option<Self>

Computes self + other, returning None if overflow occurred.

source

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.

source

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..

source

fn checked_sub(&self, other: Self::V) -> Option<Self>

Subtracts two addresses, checking for underflow. If underflow happens, None is returned.

source

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.

source

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§

source

fn mask(&self, mask: Self::V) -> Self::V

Returns the bitwise and of the address with the given mask.

source

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);
source

fn checked_align_up(&self, power_of_two: Self::V) -> Option<Self>

Returns self, aligned to the given power of two.

source

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.

Object Safety§

This trait is not object safe.

Implementors§