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

Conversation

fslongjin
Copy link
Contributor

No description provided.

Copy link
Collaborator

@qwandor qwandor left a comment

Choose a reason for hiding this comment

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

How would these Debug implementations be used? These types are used to wrap MMIO registers, which can't just be read directly.

@@ -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.

@@ -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.

@@ -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.

@@ -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()
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.

@fslongjin fslongjin closed this Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants