Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make more VirtAddr and PhysAddr methods const #369

Merged
merged 1 commit into from
Mar 31, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions src/addr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,14 @@ impl VirtAddr {
/// Converts the address to a raw pointer.
#[cfg(target_pointer_width = "64")]
#[inline]
pub fn as_ptr<T>(self) -> *const T {
pub const fn as_ptr<T>(self) -> *const T {
self.as_u64() as *const T
}

/// Converts the address to a mutable raw pointer.
#[cfg(target_pointer_width = "64")]
#[inline]
pub fn as_mut_ptr<T>(self) -> *mut T {
pub const fn as_mut_ptr<T>(self) -> *mut T {
self.as_ptr::<T>() as *mut T
}

Expand Down Expand Up @@ -420,13 +420,12 @@ impl PhysAddr {
///
/// This function panics if a bit in the range 52 to 64 is set.
#[inline]
pub fn new(addr: u64) -> PhysAddr {
assert_eq!(
addr.get_bits(52..64),
0,
"physical addresses must not have any bits in the range 52 to 64 set"
);
PhysAddr(addr)
pub const fn new(addr: u64) -> Self {
// TODO: Replace with .ok().expect(msg) when that works on stable.
match Self::try_new(addr) {
Ok(p) => p,
Err(_) => panic!("physical addresses must not have any bits in the range 52 to 64 set"),
}
}

/// Creates a new physical address, throwing bits 52..64 away.
Expand All @@ -449,10 +448,12 @@ impl PhysAddr {
///
/// Fails if any bits in the range 52 to 64 are set.
#[inline]
pub fn try_new(addr: u64) -> Result<PhysAddr, PhysAddrNotValid> {
match addr.get_bits(52..64) {
0 => Ok(PhysAddr(addr)), // address is valid
_ => Err(PhysAddrNotValid(addr)),
pub const fn try_new(addr: u64) -> Result<Self, PhysAddrNotValid> {
let p = Self::new_truncate(addr);
if p.0 == addr {
Ok(p)
} else {
Err(PhysAddrNotValid(addr))
}
}

Expand Down