Skip to content

Commit 63ef2ae

Browse files
authored
Manually implement util traits for Port structs (#247)
1 parent 946d096 commit 63ef2ae

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/instructions/port.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Access to I/O ports
22
3+
use core::fmt;
34
use core::marker::PhantomData;
45

56
pub use crate::structures::port::{PortRead, PortWrite};
@@ -95,21 +96,18 @@ impl PortWrite for u32 {
9596
}
9697

9798
/// A read only I/O port.
98-
#[derive(Debug, Clone, PartialEq, Eq)]
9999
pub struct PortReadOnly<T> {
100100
port: u16,
101101
phantom: PhantomData<T>,
102102
}
103103

104104
/// A write only I/O port.
105-
#[derive(Debug, Clone, PartialEq, Eq)]
106105
pub struct PortWriteOnly<T> {
107106
port: u16,
108107
phantom: PhantomData<T>,
109108
}
110109

111110
/// An I/O port.
112-
#[derive(Debug, Clone, PartialEq, Eq)]
113111
pub struct Port<T> {
114112
port: u16,
115113
phantom: PhantomData<T>,
@@ -199,3 +197,36 @@ impl<T: PortWrite> Port<T> {
199197
T::write_to_port(self.port, value)
200198
}
201199
}
200+
201+
macro_rules! impl_port_util_traits {
202+
($struct_name:ident) => {
203+
impl<T> fmt::Debug for $struct_name<T> {
204+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
205+
f.debug_struct(stringify!($struct_name))
206+
.field("port", &self.port)
207+
.finish()
208+
}
209+
}
210+
211+
impl<T> Clone for $struct_name<T> {
212+
fn clone(&self) -> Self {
213+
Self {
214+
port: self.port,
215+
phantom: PhantomData,
216+
}
217+
}
218+
}
219+
220+
impl<T> PartialEq for $struct_name<T> {
221+
fn eq(&self, other: &Self) -> bool {
222+
self.port == other.port
223+
}
224+
}
225+
226+
impl<T> Eq for $struct_name<T> {}
227+
};
228+
}
229+
230+
impl_port_util_traits!(Port);
231+
impl_port_util_traits!(PortReadOnly);
232+
impl_port_util_traits!(PortWriteOnly);

0 commit comments

Comments
 (0)