Skip to content

Commit

Permalink
Move Sealed::DEBUG_STR to separate crate
Browse files Browse the repository at this point in the history
Items from supertraits are accessible even if the trait is private. By using a separate trait we avoid that.
  • Loading branch information
phil-opp committed Mar 15, 2024
1 parent a20e690 commit a5585ef
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 35 deletions.
13 changes: 8 additions & 5 deletions src/instructions/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use core::arch::asm;
use core::fmt;
use core::marker::PhantomData;

use crate::sealed::Sealed;
pub use crate::structures::port::{PortRead, PortWrite};
use crate::{sealed::Sealed, DebugOutput};

impl PortRead for u8 {
#[inline]
Expand Down Expand Up @@ -80,7 +80,8 @@ pub trait PortWriteAccess: PortAccess {}
#[derive(Debug)]
pub struct ReadOnlyAccess(());

impl Sealed for ReadOnlyAccess {
impl Sealed for ReadOnlyAccess {}
impl DebugOutput for ReadOnlyAccess {
const DEBUG_STR: &'static str = "ReadOnly";
}
impl PortAccess for ReadOnlyAccess {}
Expand All @@ -90,7 +91,8 @@ impl PortReadAccess for ReadOnlyAccess {}
#[derive(Debug)]
pub struct WriteOnlyAccess(());

impl Sealed for WriteOnlyAccess {
impl Sealed for WriteOnlyAccess {}
impl DebugOutput for WriteOnlyAccess {
const DEBUG_STR: &'static str = "WriteOnly";
}
impl PortAccess for WriteOnlyAccess {}
Expand All @@ -100,7 +102,8 @@ impl PortWriteAccess for WriteOnlyAccess {}
#[derive(Debug)]
pub struct ReadWriteAccess(());

impl Sealed for ReadWriteAccess {
impl Sealed for ReadWriteAccess {}
impl DebugOutput for ReadWriteAccess {
const DEBUG_STR: &'static str = "ReadWrite";
}
impl PortAccess for ReadWriteAccess {}
Expand Down Expand Up @@ -166,7 +169,7 @@ impl<T: PortWrite, A: PortWriteAccess> PortGeneric<T, A> {
}
}

impl<T, A: PortAccess> fmt::Debug for PortGeneric<T, A> {
impl<T, A: PortAccess + DebugOutput> fmt::Debug for PortGeneric<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PortGeneric")
.field("port", &self.port)
Expand Down
8 changes: 4 additions & 4 deletions src/instructions/tlb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
page::{NotGiantPageSize, PageRange},
Page, PageSize, Size2MiB, Size4KiB,
},
PrivilegeLevel, VirtAddr,
DebugOutput, PrivilegeLevel, VirtAddr,
};
use core::{arch::asm, cmp, convert::TryFrom, fmt};

Expand Down Expand Up @@ -220,7 +220,7 @@ impl Invlpgb {
#[must_use]
pub struct InvlpgbFlushBuilder<'a, S = Size4KiB>
where
S: NotGiantPageSize,
S: NotGiantPageSize + DebugOutput,
{
invlpgb: &'a Invlpgb,
page_range: Option<PageRange<S>>,
Expand All @@ -233,15 +233,15 @@ where

impl<'a, S> InvlpgbFlushBuilder<'a, S>
where
S: NotGiantPageSize,
S: NotGiantPageSize + DebugOutput,
{
/// Flush a range of pages.
///
/// If the range doesn't fit within `invlpgb_count_max`, `invlpgb` is
/// executed multiple times.
pub fn pages<T>(self, page_range: PageRange<T>) -> InvlpgbFlushBuilder<'a, T>
where
T: NotGiantPageSize,
T: NotGiantPageSize + DebugOutput,
{
InvlpgbFlushBuilder {
invlpgb: self.invlpgb,
Expand Down
10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,10 @@ impl PrivilegeLevel {
}

pub(crate) mod sealed {
pub trait Sealed {
/// A string representation for debug output.
const DEBUG_STR: &'static str;
}
pub trait Sealed {}
}

trait DebugOutput {
/// A string representation for debug output.
const DEBUG_STR: &'static str;
}
8 changes: 4 additions & 4 deletions src/structures/paging/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use super::page::AddressNotAligned;
use crate::structures::paging::page::{PageSize, Size4KiB};
use crate::PhysAddr;
use crate::{DebugOutput, PhysAddr};
use core::fmt;
use core::marker::PhantomData;
use core::ops::{Add, AddAssign, Sub, SubAssign};
Expand Down Expand Up @@ -82,7 +82,7 @@ impl<S: PageSize> PhysFrame<S> {
}
}

impl<S: PageSize> fmt::Debug for PhysFrame<S> {
impl<S: PageSize + DebugOutput> fmt::Debug for PhysFrame<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!(
"PhysFrame[{}]({:#x})",
Expand Down Expand Up @@ -163,7 +163,7 @@ impl<S: PageSize> Iterator for PhysFrameRange<S> {
}
}

impl<S: PageSize> fmt::Debug for PhysFrameRange<S> {
impl<S: PageSize + DebugOutput> fmt::Debug for PhysFrameRange<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PhysFrameRange")
.field("start", &self.start)
Expand Down Expand Up @@ -205,7 +205,7 @@ impl<S: PageSize> Iterator for PhysFrameRangeInclusive<S> {
}
}

impl<S: PageSize> fmt::Debug for PhysFrameRangeInclusive<S> {
impl<S: PageSize + DebugOutput> fmt::Debug for PhysFrameRangeInclusive<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PhysFrameRangeInclusive")
.field("start", &self.start)
Expand Down
21 changes: 12 additions & 9 deletions src/structures/paging/mapper/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ pub use self::offset_page_table::OffsetPageTable;
#[cfg(feature = "instructions")]
pub use self::recursive_page_table::{InvalidPageTable, RecursivePageTable};

use crate::structures::paging::{
frame_alloc::{FrameAllocator, FrameDeallocator},
page::PageRangeInclusive,
page_table::PageTableFlags,
Page, PageSize, PhysFrame, Size1GiB, Size2MiB, Size4KiB,
use crate::{
structures::paging::{
frame_alloc::{FrameAllocator, FrameDeallocator},
page::PageRangeInclusive,
page_table::PageTableFlags,
Page, PageSize, PhysFrame, Size1GiB, Size2MiB, Size4KiB,
},
DebugOutput,
};
use crate::{PhysAddr, VirtAddr};

Expand Down Expand Up @@ -107,7 +110,7 @@ impl MappedFrame {
}

/// A trait for common page table operations on pages of size `S`.
pub trait Mapper<S: PageSize> {
pub trait Mapper<S: PageSize + DebugOutput> {
/// Creates a new mapping in the page table.
///
/// This function might need additional physical frames to create new page tables. These
Expand Down Expand Up @@ -383,9 +386,9 @@ pub trait Mapper<S: PageSize> {
/// changed the mapping of a page to ensure that the TLB flush is not forgotten.
#[derive(Debug)]
#[must_use = "Page Table changes must be flushed or ignored."]
pub struct MapperFlush<S: PageSize>(Page<S>);
pub struct MapperFlush<S: PageSize + DebugOutput>(Page<S>);

impl<S: PageSize> MapperFlush<S> {
impl<S: PageSize + DebugOutput> MapperFlush<S> {
/// Create a new flush promise
///
/// Note that this method is intended for implementing the [`Mapper`] trait and no other uses
Expand Down Expand Up @@ -440,7 +443,7 @@ impl MapperFlushAll {

/// This error is returned from `map_to` and similar methods.
#[derive(Debug)]
pub enum MapToError<S: PageSize> {
pub enum MapToError<S: PageSize + DebugOutput> {
/// An additional frame was needed for the mapping process, but the frame allocator
/// returned `None`.
FrameAllocationFailed,
Expand Down
4 changes: 2 additions & 2 deletions src/structures/paging/mapper/recursive_page_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ impl<'a> RecursivePageTable<'a> {
/// Returns `MapToError::FrameAllocationFailed` if the entry is unused and the allocator
/// returned `None`. Returns `MapToError::ParentEntryHugePage` if the `HUGE_PAGE` flag is set
/// in the passed entry.
unsafe fn create_next_table<'b, A, S: PageSize>(
unsafe fn create_next_table<'b, A, S: PageSize + DebugOutput>(
entry: &'b mut PageTableEntry,
next_table_page: Page,
insert_flags: PageTableFlags,
Expand All @@ -128,7 +128,7 @@ impl<'a> RecursivePageTable<'a> {
/// This inner function is used to limit the scope of `unsafe`.
///
/// This is a safe function, so we need to use `unsafe` blocks when we do something unsafe.
fn inner<'b, A, S: PageSize>(
fn inner<'b, A, S: PageSize + DebugOutput>(
entry: &'b mut PageTableEntry,
next_table_page: Page,
insert_flags: PageTableFlags,
Expand Down
20 changes: 13 additions & 7 deletions src/structures/paging/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::sealed::Sealed;
use crate::structures::paging::page_table::PageTableLevel;
use crate::structures::paging::PageTableIndex;
use crate::VirtAddr;
use crate::{DebugOutput, VirtAddr};
use core::fmt;
#[cfg(feature = "step_trait")]
use core::iter::Step;
Expand Down Expand Up @@ -39,7 +39,9 @@ impl PageSize for Size4KiB {

impl NotGiantPageSize for Size4KiB {}

impl Sealed for super::Size4KiB {
impl Sealed for super::Size4KiB {}

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

Expand All @@ -49,15 +51,19 @@ impl PageSize for Size2MiB {

impl NotGiantPageSize for Size2MiB {}

impl Sealed for super::Size2MiB {
impl Sealed for super::Size2MiB {}

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

impl PageSize for Size1GiB {
const SIZE: u64 = Size2MiB::SIZE * 512;
}

impl Sealed for super::Size1GiB {
impl Sealed for super::Size1GiB {}

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

Expand Down Expand Up @@ -241,7 +247,7 @@ impl Page<Size4KiB> {
}
}

impl<S: PageSize> fmt::Debug for Page<S> {
impl<S: PageSize + DebugOutput> fmt::Debug for Page<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_fmt(format_args!(
"Page[{}]({:#x})",
Expand Down Expand Up @@ -353,7 +359,7 @@ impl PageRange<Size2MiB> {
}
}

impl<S: PageSize> fmt::Debug for PageRange<S> {
impl<S: PageSize + DebugOutput> fmt::Debug for PageRange<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PageRange")
.field("start", &self.start)
Expand Down Expand Up @@ -404,7 +410,7 @@ impl<S: PageSize> Iterator for PageRangeInclusive<S> {
}
}

impl<S: PageSize> fmt::Debug for PageRangeInclusive<S> {
impl<S: PageSize + DebugOutput> fmt::Debug for PageRangeInclusive<S> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("PageRangeInclusive")
.field("start", &self.start)
Expand Down

0 comments on commit a5585ef

Please sign in to comment.