Skip to content

Commit 28dc857

Browse files
committed
Provide a custom fmt::Debug implementation to prevent reading of write-only values
1 parent ed91bf5 commit 28dc857

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/lib.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use core::{
2222
ops::Deref,
2323
ops::{DerefMut, Index, IndexMut},
2424
ptr,
25-
slice::SliceIndex,
25+
slice::SliceIndex, fmt,
2626
};
2727

2828
/// Allows creating read-only and write-only `Volatile` values.
@@ -36,7 +36,7 @@ pub mod access;
3636
/// The size of this struct is the same as the size of the contained type.
3737
///
3838
/// TODO: read/write permissions
39-
#[derive(Debug, Default, Clone)]
39+
#[derive(Default, Clone)]
4040
#[repr(transparent)]
4141
pub struct Volatile<R, A = ReadWrite> {
4242
reference: R,
@@ -440,6 +440,18 @@ where
440440
}
441441
}
442442

443+
impl<R, T, A> fmt::Debug for Volatile<R, A> where R: Deref<Target = T>, T:Copy + fmt::Debug, A: Readable {
444+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
445+
f.debug_tuple("Volatile").field(&self.read()).finish()
446+
}
447+
}
448+
449+
impl<R> fmt::Debug for Volatile<R, WriteOnly> where R: Deref {
450+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
451+
f.debug_tuple("Volatile").field(&"[write-only]").finish()
452+
}
453+
}
454+
443455
#[cfg(test)]
444456
mod tests {
445457
use super::Volatile;

0 commit comments

Comments
 (0)