Skip to content

Commit

Permalink
seal off the PageSize trait
Browse files Browse the repository at this point in the history
Users should never implement this trait themselves. This also allows us
to add more supertraits to `PageSize` and `NotGiantPageSize` without
introducing a major breaking change.
  • Loading branch information
Freax13 committed Mar 8, 2023
1 parent d4a780e commit b568699
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/structures/paging/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl<S: PageSize> fmt::Debug for PhysFrame<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!(
"PhysFrame[{}]({:#x})",
S::SIZE_AS_DEBUG_STR,
S::DEBUG_STR,
self.start_address().as_u64()
))
}
Expand Down
23 changes: 15 additions & 8 deletions src/structures/paging/page.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
//! Abstractions for default-sized and huge virtual memory pages.
use crate::sealed::Sealed;
use crate::structures::paging::page_table::PageTableLevel;
use crate::structures::paging::PageTableIndex;
use crate::VirtAddr;
Expand All @@ -10,12 +11,9 @@ use core::marker::PhantomData;
use core::ops::{Add, AddAssign, Sub, SubAssign};

/// Trait for abstracting over the three possible page sizes on x86_64, 4KiB, 2MiB, 1GiB.
pub trait PageSize: Copy + Eq + PartialOrd + Ord {
pub trait PageSize: Copy + Eq + PartialOrd + Ord + Sealed {
/// The page size in bytes.
const SIZE: u64;

/// A string representation of the page size for debug output.
const SIZE_AS_DEBUG_STR: &'static str;
}

/// This trait is implemented for 4KiB and 2MiB pages, but not for 1GiB pages.
Expand All @@ -37,21 +35,30 @@ pub enum Size1GiB {}

impl PageSize for Size4KiB {
const SIZE: u64 = 4096;
const SIZE_AS_DEBUG_STR: &'static str = "4KiB";
}

impl NotGiantPageSize for Size4KiB {}

impl Sealed for super::Size4KiB {
const DEBUG_STR: &'static str = "4KiB";
}

impl PageSize for Size2MiB {
const SIZE: u64 = Size4KiB::SIZE * 512;
const SIZE_AS_DEBUG_STR: &'static str = "2MiB";
}

impl NotGiantPageSize for Size2MiB {}

impl Sealed for super::Size2MiB {
const DEBUG_STR: &'static str = "2MiB";
}

impl PageSize for Size1GiB {
const SIZE: u64 = Size2MiB::SIZE * 512;
const SIZE_AS_DEBUG_STR: &'static str = "1GiB";
}

impl Sealed for super::Size1GiB {
const DEBUG_STR: &'static str = "1GiB";
}

/// A virtual memory page.
Expand Down Expand Up @@ -223,7 +230,7 @@ impl<S: PageSize> fmt::Debug for Page<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!(
"Page[{}]({:#x})",
S::SIZE_AS_DEBUG_STR,
S::DEBUG_STR,
self.start_address().as_u64()
))
}
Expand Down

0 comments on commit b568699

Please sign in to comment.