Skip to content

derive Default for Volatile, WriteOnly and ReadOnly #10

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

Merged
merged 2 commits into from
Jul 29, 2020
Merged
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
19 changes: 11 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#![cfg_attr(feature="const_fn", feature(const_fn))]
#![cfg_attr(feature = "const_fn", feature(const_fn))]

//! Provides wrapper types `Volatile`, `ReadOnly`, `WriteOnly`, `ReadWrite`, which wrap any copy-able type and allows for
//! volatile memory access to wrapped value. Volatile memory accesses are never optimized away by
Expand Down Expand Up @@ -34,7 +34,7 @@ use core::ptr;
/// take and return copies of the value.
///
/// The size of this struct is the same as the size of the contained type.
#[derive(Debug)]
#[derive(Debug, Default)]
#[repr(transparent)]
pub struct Volatile<T: Copy>(T);

Expand All @@ -50,7 +50,7 @@ impl<T: Copy> Volatile<T> {
/// # Panics
///
/// This method never panics.
#[cfg(feature="const_fn")]
#[cfg(feature = "const_fn")]
pub const fn new(value: T) -> Volatile<T> {
Volatile(value)
}
Expand All @@ -66,7 +66,7 @@ impl<T: Copy> Volatile<T> {
/// # Panics
///
/// This method never panics.
#[cfg(not(feature="const_fn"))]
#[cfg(not(feature = "const_fn"))]
pub fn new(value: T) -> Volatile<T> {
Volatile(value)
}
Expand Down Expand Up @@ -133,7 +133,8 @@ impl<T: Copy> Volatile<T> {
///
/// Ths method never panics.
pub fn update<F>(&mut self, f: F)
where F: FnOnce(&mut T)
where
F: FnOnce(&mut T),
{
let mut value = self.read();
f(&mut value);
Expand All @@ -150,7 +151,7 @@ impl<T: Copy> Clone for Volatile<T> {
/// A volatile wrapper which only allows read operations.
///
/// The size of this struct is the same as the contained type.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct ReadOnly<T: Copy>(Volatile<T>);

impl<T: Copy> ReadOnly<T> {
Expand Down Expand Up @@ -207,7 +208,7 @@ impl<T: Copy> ReadOnly<T> {
/// A volatile wrapper which only allows write operations.
///
/// The size of this struct is the same as the contained type.
#[derive(Debug, Clone)]
#[derive(Debug, Clone, Default)]
pub struct WriteOnly<T: Copy>(Volatile<T>);

impl<T: Copy> WriteOnly<T> {
Expand Down Expand Up @@ -300,7 +301,9 @@ mod tests {
let volatile_ptr = target_ptr as *mut Volatile<u32>;

// UNSAFE: Safe, as we know the value exists on the stack.
unsafe { (*volatile_ptr).write(42u32); }
unsafe {
(*volatile_ptr).write(42u32);
}

assert_eq!(target_value, 42u32);
}
Expand Down