From 9bdea3325e5c99d5107e729e58cec372db460390 Mon Sep 17 00:00:00 2001 From: Yuekai Jia Date: Thu, 20 Apr 2023 23:34:16 +0800 Subject: [PATCH] Remove `impl const` and `~const` in the standard library Otherwise it will fail to compile on nightly-2023-04-19 See https://github.com/rust-lang/rust/issues/110395 --- src/lib.rs | 124 ++++++++++++++++++++++++++--------------------------- 1 file changed, 61 insertions(+), 63 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2b65630..bce51aa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,7 +2,6 @@ #![no_std] #![feature(const_mut_refs)] -#![feature(const_trait_impl)] use core::fmt; use core::ops::{Add, AddAssign, Sub, SubAssign}; @@ -16,11 +15,8 @@ pub const PAGE_SIZE_4K: usize = 0x1000; /// /// The alignment must be a power of two. #[inline] -pub const fn align_down(addr: usize, align: U) -> usize -where - U: ~const Into, -{ - addr & !(align.into() - 1) +pub const fn align_down(addr: usize, align: usize) -> usize { + addr & !(align - 1) } /// Align address upwards. @@ -29,11 +25,7 @@ where /// /// The alignment must be a power of two. #[inline] -pub const fn align_up(addr: usize, align: U) -> usize -where - U: ~const Into, -{ - let align = align.into(); +pub const fn align_up(addr: usize, align: usize) -> usize { (addr + align - 1) & !(align - 1) } @@ -41,21 +33,15 @@ where /// /// Equivalent to `addr % align`, but the alignment must be a power of two. #[inline] -pub const fn align_offset(addr: usize, align: U) -> usize -where - U: ~const Into, -{ - addr & (align.into() - 1) +pub const fn align_offset(addr: usize, align: usize) -> usize { + addr & (align - 1) } /// Checks whether the address has the demanded alignment. /// /// Equivalent to `addr % align == 0`, but the alignment must be a power of two. #[inline] -pub const fn is_aligned(addr: usize, align: U) -> bool -where - U: ~const Into, -{ +pub const fn is_aligned(addr: usize, align: usize) -> bool { align_offset(addr, align) == 0 } @@ -98,6 +84,12 @@ pub struct PhysAddr(usize); pub struct VirtAddr(usize); impl PhysAddr { + /// Converts an `usize` to a physical address. + #[inline] + pub const fn from(addr: usize) -> Self { + Self(addr) + } + /// Converts the address to an `usize`. #[inline] pub const fn as_usize(self) -> usize { @@ -108,72 +100,78 @@ impl PhysAddr { /// /// See the [`align_down`] function for more information. #[inline] - pub const fn align_down(self, align: U) -> Self + pub fn align_down(self, align: U) -> Self where - U: ~const Into, + U: Into, { - Self(align_down(self.0, align)) + Self(align_down(self.0, align.into())) } /// Aligns the address upwards to the given alignment. /// /// See the [`align_up`] function for more information. #[inline] - pub const fn align_up(self, align: U) -> Self + pub fn align_up(self, align: U) -> Self where - U: ~const Into, + U: Into, { - Self(align_up(self.0, align)) + Self(align_up(self.0, align.into())) } /// Returns the offset of the address within the given alignment. /// /// See the [`align_offset`] function for more information. #[inline] - pub const fn align_offset(self, align: U) -> usize + pub fn align_offset(self, align: U) -> usize where - U: ~const Into, + U: Into, { - align_offset(self.0, align) + align_offset(self.0, align.into()) } /// Checks whether the address has the demanded alignment. /// /// See the [`is_aligned`] function for more information. #[inline] - pub const fn is_aligned(self, align: U) -> bool + pub fn is_aligned(self, align: U) -> bool where - U: ~const Into, + U: Into, { - is_aligned(self.0, align) + is_aligned(self.0, align.into()) } /// Aligns the address downwards to 4096 (bytes). #[inline] - pub const fn align_down_4k(self) -> Self { + pub fn align_down_4k(self) -> Self { self.align_down(PAGE_SIZE_4K) } /// Aligns the address upwards to 4096 (bytes). #[inline] - pub const fn align_up_4k(self) -> Self { + pub fn align_up_4k(self) -> Self { self.align_up(PAGE_SIZE_4K) } /// Returns the offset of the address within a 4K-sized page. #[inline] - pub const fn align_offset_4k(self) -> usize { + pub fn align_offset_4k(self) -> usize { self.align_offset(PAGE_SIZE_4K) } /// Checks whether the address is 4K-aligned. #[inline] - pub const fn is_aligned_4k(self) -> bool { + pub fn is_aligned_4k(self) -> bool { self.is_aligned(PAGE_SIZE_4K) } } impl VirtAddr { + /// Converts an `usize` to a virtual address. + #[inline] + pub const fn from(addr: usize) -> Self { + Self(addr) + } + /// Converts the address to an `usize`. #[inline] pub const fn as_usize(self) -> usize { @@ -196,100 +194,100 @@ impl VirtAddr { /// /// See the [`align_down`] function for more information. #[inline] - pub const fn align_down(self, align: U) -> Self + pub fn align_down(self, align: U) -> Self where - U: ~const Into, + U: Into, { - Self(align_down(self.0, align)) + Self(align_down(self.0, align.into())) } /// Aligns the address upwards to the given alignment. /// /// See the [`align_up`] function for more information. #[inline] - pub const fn align_up(self, align: U) -> Self + pub fn align_up(self, align: U) -> Self where - U: ~const Into, + U: Into, { - Self(align_up(self.0, align)) + Self(align_up(self.0, align.into())) } /// Returns the offset of the address within the given alignment. /// /// See the [`align_offset`] function for more information. #[inline] - pub const fn align_offset(self, align: U) -> usize + pub fn align_offset(self, align: U) -> usize where - U: ~const Into, + U: Into, { - align_offset(self.0, align) + align_offset(self.0, align.into()) } ///Checks whether the address has the demanded alignment. /// /// See the [`is_aligned`] function for more information. #[inline] - pub const fn is_aligned(self, align: U) -> bool + pub fn is_aligned(self, align: U) -> bool where - U: ~const Into, + U: Into, { - is_aligned(self.0, align) + is_aligned(self.0, align.into()) } /// Aligns the address downwards to 4096 (bytes). #[inline] - pub const fn align_down_4k(self) -> Self { + pub fn align_down_4k(self) -> Self { self.align_down(PAGE_SIZE_4K) } /// Aligns the address upwards to 4096 (bytes). #[inline] - pub const fn align_up_4k(self) -> Self { + pub fn align_up_4k(self) -> Self { self.align_up(PAGE_SIZE_4K) } /// Returns the offset of the address within a 4K-sized page. #[inline] - pub const fn align_offset_4k(self) -> usize { + pub fn align_offset_4k(self) -> usize { self.align_offset(PAGE_SIZE_4K) } /// Checks whether the address is 4K-aligned. #[inline] - pub const fn is_aligned_4k(self) -> bool { + pub fn is_aligned_4k(self) -> bool { self.is_aligned(PAGE_SIZE_4K) } } -impl const From for PhysAddr { +impl From for PhysAddr { #[inline] fn from(addr: usize) -> Self { Self(addr) } } -impl const From for VirtAddr { +impl From for VirtAddr { #[inline] fn from(addr: usize) -> Self { Self(addr) } } -impl const From for usize { +impl From for usize { #[inline] fn from(addr: PhysAddr) -> usize { addr.0 } } -impl const From for usize { +impl From for usize { #[inline] fn from(addr: VirtAddr) -> usize { addr.0 } } -impl const Add for PhysAddr { +impl Add for PhysAddr { type Output = Self; #[inline] fn add(self, rhs: usize) -> Self { @@ -297,14 +295,14 @@ impl const Add for PhysAddr { } } -impl const AddAssign for PhysAddr { +impl AddAssign for PhysAddr { #[inline] fn add_assign(&mut self, rhs: usize) { *self = *self + rhs; } } -impl const Sub for PhysAddr { +impl Sub for PhysAddr { type Output = Self; #[inline] fn sub(self, rhs: usize) -> Self { @@ -312,14 +310,14 @@ impl const Sub for PhysAddr { } } -impl const SubAssign for PhysAddr { +impl SubAssign for PhysAddr { #[inline] fn sub_assign(&mut self, rhs: usize) { *self = *self - rhs; } } -impl const Add for VirtAddr { +impl Add for VirtAddr { type Output = Self; #[inline] fn add(self, rhs: usize) -> Self { @@ -327,14 +325,14 @@ impl const Add for VirtAddr { } } -impl const AddAssign for VirtAddr { +impl AddAssign for VirtAddr { #[inline] fn add_assign(&mut self, rhs: usize) { *self = *self + rhs; } } -impl const Sub for VirtAddr { +impl Sub for VirtAddr { type Output = Self; #[inline] fn sub(self, rhs: usize) -> Self {