Skip to content

Commit

Permalink
Add Debug trait implementation for ReadOnly, WriteOnly, and Volatile (#1
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fslongjin authored Apr 22, 2024
1 parent 61ece50 commit 6aebf3b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/transport/mmio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ impl Display for MmioError {
///
/// Ref: 4.2.2 MMIO Device Register Layout and 4.2.4 Legacy interface
#[repr(C)]
#[derive(Debug)]
pub struct VirtIOHeader {
/// Magic value
magic: ReadOnly<u32>,
Expand Down
20 changes: 20 additions & 0 deletions src/volatile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
#[repr(transparent)]
pub struct ReadOnly<T: Copy>(T);

impl<T: Debug + Copy> Debug for ReadOnly<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("ReadOnly").field(&self.0).finish()
}
}

impl<T: Copy> ReadOnly<T> {
/// Construct a new instance for testing.
pub fn new(value: T) -> Self {
Expand All @@ -15,6 +21,12 @@ impl<T: Copy> ReadOnly<T> {
#[repr(transparent)]
pub struct WriteOnly<T: Copy>(T);

impl<T: Debug + Copy> Debug for WriteOnly<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("WriteOnly").field(&self.0).finish()
}
}

/// An MMIO register which may be both read and written.
#[derive(Default)]
#[repr(transparent)]
Expand All @@ -27,6 +39,12 @@ impl<T: Copy> Volatile<T> {
}
}

impl<T: Debug + Copy> Debug for Volatile<T> {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.debug_tuple("Volatile").field(&self.0).finish()
}
}

/// A trait implemented by MMIO registers which may be read from.
pub trait VolatileReadable<T> {
/// Performs a volatile read from the MMIO register.
Expand Down Expand Up @@ -104,5 +122,7 @@ macro_rules! volwrite {
};
}

use core::fmt::{Debug, Formatter};

pub(crate) use volread;
pub(crate) use volwrite;

0 comments on commit 6aebf3b

Please sign in to comment.