From b568699456350e0fb97bef4c717d718380c3a762 Mon Sep 17 00:00:00 2001 From: Tom Dohrmann Date: Wed, 8 Mar 2023 12:54:52 +0100 Subject: [PATCH] seal off the `PageSize` trait 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. --- src/structures/paging/frame.rs | 2 +- src/structures/paging/page.rs | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/src/structures/paging/frame.rs b/src/structures/paging/frame.rs index 64935caee..6cae8faba 100644 --- a/src/structures/paging/frame.rs +++ b/src/structures/paging/frame.rs @@ -86,7 +86,7 @@ impl fmt::Debug for PhysFrame { 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() )) } diff --git a/src/structures/paging/page.rs b/src/structures/paging/page.rs index 13db89f82..933b8c3e6 100644 --- a/src/structures/paging/page.rs +++ b/src/structures/paging/page.rs @@ -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; @@ -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. @@ -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. @@ -223,7 +230,7 @@ impl fmt::Debug for Page { 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() )) }