-
Notifications
You must be signed in to change notification settings - Fork 137
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
Implement conversion traits for PhysAddr and VirtAddr #394
base: master
Are you sure you want to change the base?
Changes from 3 commits
4a01d73
f750e82
63feae8
f914de1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
//! Physical and virtual addresses manipulation | ||
|
||
#[cfg(feature = "step_trait")] | ||
use core::convert::TryFrom; | ||
use core::convert::TryInto; | ||
use core::fmt; | ||
#[cfg(feature = "step_trait")] | ||
use core::iter::Step; | ||
|
@@ -403,6 +403,96 @@ impl Step for VirtAddr { | |
} | ||
} | ||
|
||
impl TryFrom<u64> for VirtAddr { | ||
type Error = VirtAddrNotValid; | ||
|
||
#[inline] | ||
fn try_from(addr: u64) -> Result<Self, Self::Error> { | ||
Self::try_new(addr) | ||
} | ||
} | ||
|
||
// if `target_pointer_width > 64`, we would need a different error type | ||
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] | ||
impl TryFrom<usize> for VirtAddr { | ||
type Error = VirtAddrNotValid; | ||
|
||
#[inline] | ||
fn try_from(addr: usize) -> Result<Self, Self::Error> { | ||
Self::try_new(addr.try_into().unwrap()) | ||
} | ||
} | ||
|
||
// if `target_pointer_width > 64`, we would need a different error type | ||
// (and `as` would truncate) | ||
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] | ||
impl<T> TryFrom<*const T> for VirtAddr { | ||
type Error = VirtAddrNotValid; | ||
|
||
#[inline] | ||
fn try_from(addr: *const T) -> Result<Self, Self::Error> { | ||
Self::try_new(addr as u64) | ||
} | ||
} | ||
|
||
// if target_pointer_width is 128, we would need a different error type | ||
// (and `as` would truncate) | ||
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] | ||
impl<T> TryFrom<*mut T> for VirtAddr { | ||
type Error = VirtAddrNotValid; | ||
|
||
#[inline] | ||
fn try_from(addr: *mut T) -> Result<Self, Self::Error> { | ||
Self::try_new(addr as u64) | ||
} | ||
} | ||
|
||
impl From<u32> for VirtAddr { | ||
#[inline] | ||
fn from(addr: u32) -> Self { | ||
Self::new(addr.into()) | ||
} | ||
} | ||
|
||
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] | ||
impl<T> From<&T> for VirtAddr { | ||
#[inline] | ||
fn from(addr: &T) -> Self { | ||
Self::new(addr as *const _ as u64) | ||
} | ||
} | ||
|
||
#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] | ||
impl<T> From<&mut T> for VirtAddr { | ||
#[inline] | ||
fn from(addr: &mut T) -> Self { | ||
Self::new(addr as *mut _ as u64) | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These could fail on different architectures. Do we want to care about that given that most users will probably run on x86? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah yeah, good point! I guess we could a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that'd work There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changes these to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was also wondering if we ought to use |
||
|
||
impl From<VirtAddr> for u64 { | ||
#[inline] | ||
fn from(addr: VirtAddr) -> Self { | ||
addr.0 | ||
} | ||
} | ||
|
||
#[cfg(target_pointer_width = "64")] | ||
impl<T> From<VirtAddr> for *const T { | ||
#[inline] | ||
fn from(addr: VirtAddr) -> Self { | ||
addr.0 as *const T | ||
} | ||
} | ||
|
||
#[cfg(target_pointer_width = "64")] | ||
impl<T> From<VirtAddr> for *mut T { | ||
#[inline] | ||
fn from(addr: VirtAddr) -> Self { | ||
addr.0 as *mut T | ||
} | ||
} | ||
|
||
/// A passed `u64` was not a valid physical address. | ||
/// | ||
/// This means that bits 52 to 64 were not all null. | ||
|
@@ -632,6 +722,29 @@ impl Sub<PhysAddr> for PhysAddr { | |
} | ||
} | ||
|
||
impl TryFrom<u64> for PhysAddr { | ||
type Error = PhysAddrNotValid; | ||
|
||
#[inline] | ||
fn try_from(addr: u64) -> Result<Self, Self::Error> { | ||
Self::try_new(addr) | ||
} | ||
} | ||
|
||
impl From<u32> for PhysAddr { | ||
#[inline] | ||
fn from(addr: u32) -> Self { | ||
Self::new(addr.into()) | ||
} | ||
} | ||
|
||
impl From<PhysAddr> for u64 { | ||
#[inline] | ||
fn from(addr: PhysAddr) -> Self { | ||
addr.0 | ||
} | ||
} | ||
|
||
/// Align address downwards. | ||
/// | ||
/// Returns the greatest `x` with alignment `align` so that `x <= addr`. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also
target_pointer_width = "16"
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
target_pointer_width = "16"
where applicable.