Skip to content

Commit b568699

Browse files
committed
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.
1 parent d4a780e commit b568699

File tree

2 files changed

+16
-9
lines changed

2 files changed

+16
-9
lines changed

src/structures/paging/frame.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ impl<S: PageSize> fmt::Debug for PhysFrame<S> {
8686
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
8787
f.write_fmt(format_args!(
8888
"PhysFrame[{}]({:#x})",
89-
S::SIZE_AS_DEBUG_STR,
89+
S::DEBUG_STR,
9090
self.start_address().as_u64()
9191
))
9292
}

src/structures/paging/page.rs

+15-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Abstractions for default-sized and huge virtual memory pages.
22
3+
use crate::sealed::Sealed;
34
use crate::structures::paging::page_table::PageTableLevel;
45
use crate::structures::paging::PageTableIndex;
56
use crate::VirtAddr;
@@ -10,12 +11,9 @@ use core::marker::PhantomData;
1011
use core::ops::{Add, AddAssign, Sub, SubAssign};
1112

1213
/// Trait for abstracting over the three possible page sizes on x86_64, 4KiB, 2MiB, 1GiB.
13-
pub trait PageSize: Copy + Eq + PartialOrd + Ord {
14+
pub trait PageSize: Copy + Eq + PartialOrd + Ord + Sealed {
1415
/// The page size in bytes.
1516
const SIZE: u64;
16-
17-
/// A string representation of the page size for debug output.
18-
const SIZE_AS_DEBUG_STR: &'static str;
1917
}
2018

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

3836
impl PageSize for Size4KiB {
3937
const SIZE: u64 = 4096;
40-
const SIZE_AS_DEBUG_STR: &'static str = "4KiB";
4138
}
4239

4340
impl NotGiantPageSize for Size4KiB {}
4441

42+
impl Sealed for super::Size4KiB {
43+
const DEBUG_STR: &'static str = "4KiB";
44+
}
45+
4546
impl PageSize for Size2MiB {
4647
const SIZE: u64 = Size4KiB::SIZE * 512;
47-
const SIZE_AS_DEBUG_STR: &'static str = "2MiB";
4848
}
4949

5050
impl NotGiantPageSize for Size2MiB {}
5151

52+
impl Sealed for super::Size2MiB {
53+
const DEBUG_STR: &'static str = "2MiB";
54+
}
55+
5256
impl PageSize for Size1GiB {
5357
const SIZE: u64 = Size2MiB::SIZE * 512;
54-
const SIZE_AS_DEBUG_STR: &'static str = "1GiB";
58+
}
59+
60+
impl Sealed for super::Size1GiB {
61+
const DEBUG_STR: &'static str = "1GiB";
5562
}
5663

5764
/// A virtual memory page.
@@ -223,7 +230,7 @@ impl<S: PageSize> fmt::Debug for Page<S> {
223230
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
224231
f.write_fmt(format_args!(
225232
"Page[{}]({:#x})",
226-
S::SIZE_AS_DEBUG_STR,
233+
S::DEBUG_STR,
227234
self.start_address().as_u64()
228235
))
229236
}

0 commit comments

Comments
 (0)