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

Breaking Change: Improved VirtAddr & PhysAddr #8

Merged
merged 2 commits into from
Feb 28, 2024
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
84 changes: 14 additions & 70 deletions src/paging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ fn align_up(addr: u64, align: u64) -> u64 {
pub struct PhysAddr(pub u64);

impl PhysAddr {
#[inline]
pub const fn new(addr: u64) -> Self {
Self(addr)
}

/// Convert to `u64`
pub fn as_u64(self) -> u64 {
self.0
Expand Down Expand Up @@ -113,14 +118,6 @@ impl Into<usize> for PhysAddr {
}
}

impl ops::Add for PhysAddr {
type Output = PhysAddr;

fn add(self, rhs: PhysAddr) -> Self::Output {
PhysAddr(self.0 + rhs.0)
}
}

impl ops::Add<u64> for PhysAddr {
type Output = PhysAddr;

Expand All @@ -129,31 +126,17 @@ impl ops::Add<u64> for PhysAddr {
}
}

impl ops::Add<usize> for PhysAddr {
type Output = PhysAddr;

fn add(self, rhs: usize) -> Self::Output {
PhysAddr::from(self.0 + rhs as u64)
}
}

impl ops::AddAssign for PhysAddr {
fn add_assign(&mut self, other: PhysAddr) {
*self = PhysAddr::from(self.0 + other.0);
}
}

impl ops::AddAssign<u64> for PhysAddr {
fn add_assign(&mut self, offset: u64) {
*self = PhysAddr::from(self.0 + offset);
}
}

impl ops::Sub for PhysAddr {
type Output = PhysAddr;
type Output = u64;

fn sub(self, rhs: PhysAddr) -> Self::Output {
PhysAddr::from(self.0 - rhs.0)
self.as_u64().checked_sub(rhs.as_u64()).unwrap()
}
}

Expand All @@ -165,14 +148,6 @@ impl ops::Sub<u64> for PhysAddr {
}
}

impl ops::Sub<usize> for PhysAddr {
type Output = PhysAddr;

fn sub(self, rhs: usize) -> Self::Output {
PhysAddr::from(self.0 - rhs as u64)
}
}

impl ops::Rem for PhysAddr {
type Output = PhysAddr;

Expand Down Expand Up @@ -287,6 +262,11 @@ impl fmt::Pointer for PhysAddr {
pub struct VirtAddr(pub u64);

impl VirtAddr {
#[inline]
pub const fn new(addr: u64) -> Self {
Self(addr)
}

/// Convert from `u64`
pub const fn from_u64(v: u64) -> Self {
VirtAddr(v)
Expand Down Expand Up @@ -429,14 +409,6 @@ impl Into<usize> for VirtAddr {
}
}

impl ops::Add for VirtAddr {
type Output = VirtAddr;

fn add(self, rhs: VirtAddr) -> Self::Output {
VirtAddr(self.0 + rhs.0)
}
}

impl ops::Add<u64> for VirtAddr {
type Output = VirtAddr;

Expand All @@ -445,37 +417,17 @@ impl ops::Add<u64> for VirtAddr {
}
}

impl ops::Add<usize> for VirtAddr {
type Output = VirtAddr;

fn add(self, rhs: usize) -> Self::Output {
VirtAddr::from(self.0 + rhs as u64)
}
}

impl ops::AddAssign for VirtAddr {
fn add_assign(&mut self, other: VirtAddr) {
*self = VirtAddr::from(self.0 + other.0);
}
}

impl ops::AddAssign<u64> for VirtAddr {
fn add_assign(&mut self, offset: u64) {
*self = VirtAddr::from(self.0 + offset);
}
}

impl ops::AddAssign<usize> for VirtAddr {
fn add_assign(&mut self, offset: usize) {
*self = VirtAddr::from(self.0 + offset as u64);
}
}

impl ops::Sub for VirtAddr {
type Output = VirtAddr;
type Output = u64;

fn sub(self, rhs: VirtAddr) -> Self::Output {
VirtAddr::from(self.0 - rhs.0)
self.as_u64().checked_sub(rhs.as_u64()).unwrap()
}
}

Expand All @@ -487,14 +439,6 @@ impl ops::Sub<u64> for VirtAddr {
}
}

impl ops::Sub<usize> for VirtAddr {
type Output = VirtAddr;

fn sub(self, rhs: usize) -> Self::Output {
VirtAddr::from(self.0 - rhs as u64)
}
}

impl ops::Rem for VirtAddr {
type Output = VirtAddr;

Expand Down
Loading