Va

Struct Va 

Source
#[repr(transparent)]
pub struct Va(usize);
Expand description

Represents a virtual address.

The Va (Virtual Address) struct represents an address in the virtual memory space used by the kernel or user-space applications.

This abstraction provides utility methods for validation, alignment, and address manipulation, ensuring safe and consistent handling of virtual addresses.

Tuple Fields§

§0: usize

Implementations§

Source§

impl Va

Source

pub const fn new(addr: usize) -> Option<Self>

Creates a new virtual address if the address is valid.

This method checks whether the given address falls within the valid virtual address range. If it does, a Some(Va) is returned; otherwise, None is returned.

§Arguments
  • addr: A usize representing the virtual address.
§Returns
  • Some(Va): If the address is within the valid virtual memory range.
  • None: If the address is invalid.
§Example
let va = Va::new(0xFFFF_8000_1234_5678);
assert!(va.is_some()); // Valid virtual address

let invalid_va = Va::new(0xFFFF_7000_1234_5678);
assert!(invalid_va.is_none()); // Invalid virtual address
Source

pub const fn into_usize(self) -> usize

Returns the raw usize representation of the virtual address.

This method allows extracting the underlying usize value, enabling low-level operations or conversions between address types.

§Returns
  • The virtual address as a usize.
§Example
let va = Va::new(0xFFFF_8000_1234_5678).unwrap();
let raw_addr = va.into_usize();
assert_eq!(raw_addr, 0xFFFF_8000_1234_5678);
Source

pub const fn page_down(self) -> Self

Aligns the virtual address down to the nearest page boundary.

This method ensures that the address is aligned to the start of its memory page by clearing the lower bits that represent the page offset.

§Returns
  • A new Va instance representing the aligned address.
§Example
let va = Va::new(0xFFFF_8000_1234_5678).unwrap();
let aligned = va.page_down();
assert_eq!(aligned.into_usize(), 0xFFFF_8000_1234_5000); // Example alignment
Source

pub const fn page_up(self) -> Self

Aligns the virtual address up to the nearest page boundary.

This method rounds up the address to the next memory page.

§Returns
  • A new Va instance representing the aligned address.
§Example
let va = Va::new(0xFFFF_8000_1234_5678).unwrap();
let aligned = va.page_up();
assert_eq!(aligned.into_usize(), 0xFFFF_8000_1234_6000); // Example alignment
Source

pub const fn offset(self) -> usize

Extracts the offset within the memory page from the virtual address.

This method retrieves the lower bits of the address that indicate the position within a page, which is useful for memory management and page-related operations.

§Returns
  • The offset within the page as a usize.
§Example
let va = Va::new(0xFFFF_8000_1234_5678).unwrap();
let offset = va.offset();
assert_eq!(offset, 0x678); // Example offset within the page

Trait Implementations§

Source§

impl Add<usize> for Va

Source§

type Output = Va

The resulting type after applying the + operator.
Source§

fn add(self, other: usize) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign<usize> for Va

Source§

fn add_assign(&mut self, other: usize)

Performs the += operation. Read more
Source§

impl BitAnd<usize> for Va

Source§

type Output = Va

The resulting type after applying the & operator.
Source§

fn bitand(self, other: usize) -> Self

Performs the & operation. Read more
Source§

impl BitAndAssign<usize> for Va

Source§

fn bitand_assign(&mut self, other: usize)

Performs the &= operation. Read more
Source§

impl BitOr<usize> for Va

Source§

type Output = Va

The resulting type after applying the | operator.
Source§

fn bitor(self, other: usize) -> Self

Performs the | operation. Read more
Source§

impl BitOrAssign<usize> for Va

Source§

fn bitor_assign(&mut self, other: usize)

Performs the |= operation. Read more
Source§

impl Clone for Va

Source§

fn clone(&self) -> Va

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Va

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Va

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Ord for Va

Source§

fn cmp(&self, other: &Va) -> Ordering

This method returns an [Ordering] between self and other. Read more
1.21.0§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Va

Source§

fn eq(&self, other: &Va) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Va

Source§

fn partial_cmp(&self, other: &Va) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub<usize> for Va

Source§

type Output = Va

The resulting type after applying the - operator.
Source§

fn sub(self, other: usize) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for Va

Source§

type Output = usize

The resulting type after applying the - operator.
Source§

fn sub(self, other: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign<usize> for Va

Source§

fn sub_assign(&mut self, other: usize)

Performs the -= operation. Read more
Source§

impl Copy for Va

Source§

impl Eq for Va

Source§

impl StructuralPartialEq for Va

Auto Trait Implementations§

§

impl Freeze for Va

§

impl RefUnwindSafe for Va

§

impl Send for Va

§

impl Sync for Va

§

impl Unpin for Va

§

impl UnwindSafe for Va

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T> ToString for T
where T: Display + ?Sized,

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.