From 4a01d73a3f909a3f7dcb87d4d6bb5f66bd813e21 Mon Sep 17 00:00:00 2001 From: Mike Leany <55358344+mikeleany@users.noreply.github.com> Date: Sat, 22 Oct 2022 16:50:44 -0600 Subject: [PATCH 1/4] Impl `From` and `TryFrom` traits for `VirtAddr`. --- src/addr.rs | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 75 insertions(+), 1 deletion(-) diff --git a/src/addr.rs b/src/addr.rs index d3bec6a8b..01e0757bb 100644 --- a/src/addr.rs +++ b/src/addr.rs @@ -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,80 @@ impl Step for VirtAddr { } } +impl TryFrom for VirtAddr { + type Error = VirtAddrNotValid; + + #[inline] + fn try_from(addr: u64) -> Result { + 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 for VirtAddr { + type Error = VirtAddrNotValid; + + #[inline] + fn try_from(addr: usize) -> Result { + 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 TryFrom<*const T> for VirtAddr { + type Error = VirtAddrNotValid; + + #[inline] + fn try_from(addr: *const T) -> Result { + 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 TryFrom<*mut T> for VirtAddr { + type Error = VirtAddrNotValid; + + #[inline] + fn try_from(addr: *mut T) -> Result { + Self::try_new(addr as u64) + } +} + +impl From for VirtAddr { + #[inline] + fn from(addr: u32) -> Self { + Self::new(addr.into()) + } +} + +#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] +impl 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 From<&mut T> for VirtAddr { + #[inline] + fn from(addr: &mut T) -> Self { + Self::new(addr as *mut _ as u64) + } +} + +impl From for u64 { + #[inline] + fn from(addr: VirtAddr) -> Self { + addr.0 + } +} + /// A passed `u64` was not a valid physical address. /// /// This means that bits 52 to 64 were not all null. From f750e82c15bbf73c9a92c7bb20bd7e9cfd798ef6 Mon Sep 17 00:00:00 2001 From: Mike Leany <55358344+mikeleany@users.noreply.github.com> Date: Sat, 22 Oct 2022 20:07:13 -0600 Subject: [PATCH 2/4] Impl `From` and `TryFrom` traits for `PhysAddr`. --- src/addr.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/addr.rs b/src/addr.rs index 01e0757bb..8c86c9d46 100644 --- a/src/addr.rs +++ b/src/addr.rs @@ -706,6 +706,29 @@ impl Sub for PhysAddr { } } +impl TryFrom for PhysAddr { + type Error = PhysAddrNotValid; + + #[inline] + fn try_from(addr: u64) -> Result { + Self::try_new(addr) + } +} + +impl From for PhysAddr { + #[inline] + fn from(addr: u32) -> Self { + Self::new(addr.into()) + } +} + +impl From for u64 { + #[inline] + fn from(addr: PhysAddr) -> Self { + addr.0 + } +} + /// Align address downwards. /// /// Returns the greatest `x` with alignment `align` so that `x <= addr`. From 63feae856f543789df72d36b8e3cbb3139ec765c Mon Sep 17 00:00:00 2001 From: Mike Leany <55358344+mikeleany@users.noreply.github.com> Date: Sat, 22 Oct 2022 20:36:42 -0600 Subject: [PATCH 3/4] Impl `From` for `*const T` and `*mut T`. --- src/addr.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/addr.rs b/src/addr.rs index 8c86c9d46..988973226 100644 --- a/src/addr.rs +++ b/src/addr.rs @@ -477,6 +477,22 @@ impl From for u64 { } } +#[cfg(target_pointer_width = "64")] +impl From for *const T { + #[inline] + fn from(addr: VirtAddr) -> Self { + addr.0 as *const T + } +} + +#[cfg(target_pointer_width = "64")] +impl From 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. From f914de112b59a733df14d843295f115e05b28ce4 Mon Sep 17 00:00:00 2001 From: Mike Leany <55358344+mikeleany@users.noreply.github.com> Date: Mon, 31 Oct 2022 10:45:00 -0600 Subject: [PATCH 4/4] Adjust `cfg`s on pointer-sized converions. --- src/addr.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/addr.rs b/src/addr.rs index 988973226..246529e19 100644 --- a/src/addr.rs +++ b/src/addr.rs @@ -413,7 +413,11 @@ impl TryFrom for VirtAddr { } // if `target_pointer_width > 64`, we would need a different error type -#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] +#[cfg(any( + target_pointer_width = "16", + target_pointer_width = "32", + target_pointer_width = "64" +))] impl TryFrom for VirtAddr { type Error = VirtAddrNotValid; @@ -425,7 +429,11 @@ impl TryFrom for VirtAddr { // 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"))] +#[cfg(any( + target_pointer_width = "16", + target_pointer_width = "32", + target_pointer_width = "64" +))] impl TryFrom<*const T> for VirtAddr { type Error = VirtAddrNotValid; @@ -435,9 +443,13 @@ impl TryFrom<*const T> for VirtAddr { } } -// if target_pointer_width is 128, we would need a different error type +// 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"))] +#[cfg(any( + target_pointer_width = "16", + target_pointer_width = "32", + target_pointer_width = "64" +))] impl TryFrom<*mut T> for VirtAddr { type Error = VirtAddrNotValid; @@ -454,7 +466,7 @@ impl From for VirtAddr { } } -#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] impl From<&T> for VirtAddr { #[inline] fn from(addr: &T) -> Self { @@ -462,7 +474,7 @@ impl From<&T> for VirtAddr { } } -#[cfg(any(target_pointer_width = "32", target_pointer_width = "64"))] +#[cfg(any(target_arch = "x86", target_arch = "x86_64"))] impl From<&mut T> for VirtAddr { #[inline] fn from(addr: &mut T) -> Self {