Skip to content

Commit ed4b978

Browse files
authored
Merge pull request #404 from Freax13/seal-page-size
seal off the `PageSize` trait
2 parents a268e7a + b568699 commit ed4b978

File tree

4 files changed

+39
-24
lines changed

4 files changed

+39
-24
lines changed

src/instructions/port.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use core::arch::asm;
44
use core::fmt;
55
use core::marker::PhantomData;
66

7+
use crate::sealed::Sealed;
78
pub use crate::structures::port::{PortRead, PortWrite};
89

910
impl PortRead for u8 {
@@ -66,43 +67,43 @@ impl PortWrite for u32 {
6667
}
6768
}
6869

69-
mod sealed {
70-
pub trait Access {
71-
const DEBUG_NAME: &'static str;
72-
}
73-
}
70+
/// A marker trait for access types which allow accessing port values.
71+
pub trait PortAccess: Sealed {}
7472

7573
/// A marker trait for access types which allow reading port values.
76-
pub trait PortReadAccess: sealed::Access {}
74+
pub trait PortReadAccess: PortAccess {}
7775

7876
/// A marker trait for access types which allow writing port values.
79-
pub trait PortWriteAccess: sealed::Access {}
77+
pub trait PortWriteAccess: PortAccess {}
8078

8179
/// An access marker type indicating that a port is only allowed to read values.
8280
#[derive(Debug)]
8381
pub struct ReadOnlyAccess(());
8482

85-
impl sealed::Access for ReadOnlyAccess {
86-
const DEBUG_NAME: &'static str = "ReadOnly";
83+
impl Sealed for ReadOnlyAccess {
84+
const DEBUG_STR: &'static str = "ReadOnly";
8785
}
86+
impl PortAccess for ReadOnlyAccess {}
8887
impl PortReadAccess for ReadOnlyAccess {}
8988

9089
/// An access marker type indicating that a port is only allowed to write values.
9190
#[derive(Debug)]
9291
pub struct WriteOnlyAccess(());
9392

94-
impl sealed::Access for WriteOnlyAccess {
95-
const DEBUG_NAME: &'static str = "WriteOnly";
93+
impl Sealed for WriteOnlyAccess {
94+
const DEBUG_STR: &'static str = "WriteOnly";
9695
}
96+
impl PortAccess for WriteOnlyAccess {}
9797
impl PortWriteAccess for WriteOnlyAccess {}
9898

9999
/// An access marker type indicating that a port is allowed to read or write values.
100100
#[derive(Debug)]
101101
pub struct ReadWriteAccess(());
102102

103-
impl sealed::Access for ReadWriteAccess {
104-
const DEBUG_NAME: &'static str = "ReadWrite";
103+
impl Sealed for ReadWriteAccess {
104+
const DEBUG_STR: &'static str = "ReadWrite";
105105
}
106+
impl PortAccess for ReadWriteAccess {}
106107
impl PortReadAccess for ReadWriteAccess {}
107108
impl PortWriteAccess for ReadWriteAccess {}
108109

@@ -165,12 +166,12 @@ impl<T: PortWrite, A: PortWriteAccess> PortGeneric<T, A> {
165166
}
166167
}
167168

168-
impl<T, A: sealed::Access> fmt::Debug for PortGeneric<T, A> {
169+
impl<T, A: PortAccess> fmt::Debug for PortGeneric<T, A> {
169170
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
170171
f.debug_struct("PortGeneric")
171172
.field("port", &self.port)
172173
.field("size", &core::mem::size_of::<T>())
173-
.field("access", &format_args!("{}", A::DEBUG_NAME))
174+
.field("access", &format_args!("{}", A::DEBUG_STR))
174175
.finish()
175176
}
176177
}

src/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,10 @@ impl PrivilegeLevel {
6363
}
6464
}
6565
}
66+
67+
pub(crate) mod sealed {
68+
pub trait Sealed {
69+
/// A string representation for debug output.
70+
const DEBUG_STR: &'static str;
71+
}
72+
}

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)