Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Debug trait implementation for ReadOnly, WriteOnly, and Volatile #126

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the explicit lifetime <'_> here and in the other implementation below.

f.debug_tuple("ReadOnly").field(&self.0).finish()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReadOnly is intended to wrap MMIO registers which may have side-effects when reading, so it's probably not a good idea to implement Debug, as adding debug output may change behaviour. Also any reading should be done with read_volatile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, you are right, I didn't consider the issue of side effects.

}
}

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

WriteOnly is used to wrap MMIO registers which can only be written, so it doesn't make sense to read a value from it.

}
}

/// 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};
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put these imports at the top of the file.


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