Skip to content

Commit

Permalink
Remove impl const and ~const in the standard library
Browse files Browse the repository at this point in the history
Otherwise it will fail to compile on nightly-2023-04-19

See rust-lang/rust#110395
  • Loading branch information
equation314 committed Apr 20, 2023
1 parent 706f7d1 commit 9bdea33
Showing 1 changed file with 61 additions and 63 deletions.
124 changes: 61 additions & 63 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
#![no_std]
#![feature(const_mut_refs)]
#![feature(const_trait_impl)]

use core::fmt;
use core::ops::{Add, AddAssign, Sub, SubAssign};
Expand All @@ -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<U>(addr: usize, align: U) -> usize
where
U: ~const Into<usize>,
{
addr & !(align.into() - 1)
pub const fn align_down(addr: usize, align: usize) -> usize {
addr & !(align - 1)
}

/// Align address upwards.
Expand All @@ -29,33 +25,23 @@ where
///
/// The alignment must be a power of two.
#[inline]
pub const fn align_up<U>(addr: usize, align: U) -> usize
where
U: ~const Into<usize>,
{
let align = align.into();
pub const fn align_up(addr: usize, align: usize) -> usize {
(addr + align - 1) & !(align - 1)
}

/// Returns the offset of the address within the alignment.
///
/// Equivalent to `addr % align`, but the alignment must be a power of two.
#[inline]
pub const fn align_offset<U>(addr: usize, align: U) -> usize
where
U: ~const Into<usize>,
{
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<U>(addr: usize, align: U) -> bool
where
U: ~const Into<usize>,
{
pub const fn is_aligned(addr: usize, align: usize) -> bool {
align_offset(addr, align) == 0
}

Expand Down Expand Up @@ -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 {
Expand All @@ -108,72 +100,78 @@ impl PhysAddr {
///
/// See the [`align_down`] function for more information.
#[inline]
pub const fn align_down<U>(self, align: U) -> Self
pub fn align_down<U>(self, align: U) -> Self
where
U: ~const Into<usize>,
U: Into<usize>,
{
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<U>(self, align: U) -> Self
pub fn align_up<U>(self, align: U) -> Self
where
U: ~const Into<usize>,
U: Into<usize>,
{
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<U>(self, align: U) -> usize
pub fn align_offset<U>(self, align: U) -> usize
where
U: ~const Into<usize>,
U: Into<usize>,
{
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<U>(self, align: U) -> bool
pub fn is_aligned<U>(self, align: U) -> bool
where
U: ~const Into<usize>,
U: Into<usize>,
{
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 {
Expand All @@ -196,145 +194,145 @@ impl VirtAddr {
///
/// See the [`align_down`] function for more information.
#[inline]
pub const fn align_down<U>(self, align: U) -> Self
pub fn align_down<U>(self, align: U) -> Self
where
U: ~const Into<usize>,
U: Into<usize>,
{
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<U>(self, align: U) -> Self
pub fn align_up<U>(self, align: U) -> Self
where
U: ~const Into<usize>,
U: Into<usize>,
{
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<U>(self, align: U) -> usize
pub fn align_offset<U>(self, align: U) -> usize
where
U: ~const Into<usize>,
U: Into<usize>,
{
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<U>(self, align: U) -> bool
pub fn is_aligned<U>(self, align: U) -> bool
where
U: ~const Into<usize>,
U: Into<usize>,
{
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<usize> for PhysAddr {
impl From<usize> for PhysAddr {
#[inline]
fn from(addr: usize) -> Self {
Self(addr)
}
}

impl const From<usize> for VirtAddr {
impl From<usize> for VirtAddr {
#[inline]
fn from(addr: usize) -> Self {
Self(addr)
}
}

impl const From<PhysAddr> for usize {
impl From<PhysAddr> for usize {
#[inline]
fn from(addr: PhysAddr) -> usize {
addr.0
}
}

impl const From<VirtAddr> for usize {
impl From<VirtAddr> for usize {
#[inline]
fn from(addr: VirtAddr) -> usize {
addr.0
}
}

impl const Add<usize> for PhysAddr {
impl Add<usize> for PhysAddr {
type Output = Self;
#[inline]
fn add(self, rhs: usize) -> Self {
Self(self.0 + rhs)
}
}

impl const AddAssign<usize> for PhysAddr {
impl AddAssign<usize> for PhysAddr {
#[inline]
fn add_assign(&mut self, rhs: usize) {
*self = *self + rhs;
}
}

impl const Sub<usize> for PhysAddr {
impl Sub<usize> for PhysAddr {
type Output = Self;
#[inline]
fn sub(self, rhs: usize) -> Self {
Self(self.0 - rhs)
}
}

impl const SubAssign<usize> for PhysAddr {
impl SubAssign<usize> for PhysAddr {
#[inline]
fn sub_assign(&mut self, rhs: usize) {
*self = *self - rhs;
}
}

impl const Add<usize> for VirtAddr {
impl Add<usize> for VirtAddr {
type Output = Self;
#[inline]
fn add(self, rhs: usize) -> Self {
Self(self.0 + rhs)
}
}

impl const AddAssign<usize> for VirtAddr {
impl AddAssign<usize> for VirtAddr {
#[inline]
fn add_assign(&mut self, rhs: usize) {
*self = *self + rhs;
}
}

impl const Sub<usize> for VirtAddr {
impl Sub<usize> for VirtAddr {
type Output = Self;
#[inline]
fn sub(self, rhs: usize) -> Self {
Expand Down

0 comments on commit 9bdea33

Please sign in to comment.