Skip to content

Commit

Permalink
create unified Sealed trait
Browse files Browse the repository at this point in the history
The traits we currently want to seal off are all marker traits and they
all share some kind of debug representation.
  • Loading branch information
Freax13 committed Mar 8, 2023
1 parent a268e7a commit d4a780e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
31 changes: 16 additions & 15 deletions src/instructions/port.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use core::arch::asm;
use core::fmt;
use core::marker::PhantomData;

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

impl PortRead for u8 {
Expand Down Expand Up @@ -66,43 +67,43 @@ impl PortWrite for u32 {
}
}

mod sealed {
pub trait Access {
const DEBUG_NAME: &'static str;
}
}
/// A marker trait for access types which allow accessing port values.
pub trait PortAccess: Sealed {}

/// A marker trait for access types which allow reading port values.
pub trait PortReadAccess: sealed::Access {}
pub trait PortReadAccess: PortAccess {}

/// A marker trait for access types which allow writing port values.
pub trait PortWriteAccess: sealed::Access {}
pub trait PortWriteAccess: PortAccess {}

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

impl sealed::Access for ReadOnlyAccess {
const DEBUG_NAME: &'static str = "ReadOnly";
impl Sealed for ReadOnlyAccess {
const DEBUG_STR: &'static str = "ReadOnly";
}
impl PortAccess for ReadOnlyAccess {}
impl PortReadAccess for ReadOnlyAccess {}

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

impl sealed::Access for WriteOnlyAccess {
const DEBUG_NAME: &'static str = "WriteOnly";
impl Sealed for WriteOnlyAccess {
const DEBUG_STR: &'static str = "WriteOnly";
}
impl PortAccess for WriteOnlyAccess {}
impl PortWriteAccess for WriteOnlyAccess {}

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

impl sealed::Access for ReadWriteAccess {
const DEBUG_NAME: &'static str = "ReadWrite";
impl Sealed for ReadWriteAccess {
const DEBUG_STR: &'static str = "ReadWrite";
}
impl PortAccess for ReadWriteAccess {}
impl PortReadAccess for ReadWriteAccess {}
impl PortWriteAccess for ReadWriteAccess {}

Expand Down Expand Up @@ -165,12 +166,12 @@ impl<T: PortWrite, A: PortWriteAccess> PortGeneric<T, A> {
}
}

impl<T, A: sealed::Access> fmt::Debug for PortGeneric<T, A> {
impl<T, A: PortAccess> fmt::Debug for PortGeneric<T, A> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("PortGeneric")
.field("port", &self.port)
.field("size", &core::mem::size_of::<T>())
.field("access", &format_args!("{}", A::DEBUG_NAME))
.field("access", &format_args!("{}", A::DEBUG_STR))
.finish()
}
}
Expand Down
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,10 @@ impl PrivilegeLevel {
}
}
}

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

0 comments on commit d4a780e

Please sign in to comment.