Skip to content

Commit d4a780e

Browse files
committed
create unified Sealed trait
The traits we currently want to seal off are all marker traits and they all share some kind of debug representation.
1 parent a268e7a commit d4a780e

File tree

2 files changed

+23
-15
lines changed

2 files changed

+23
-15
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+
}

0 commit comments

Comments
 (0)