Skip to content

Commit abb7bfa

Browse files
authored
Merge pull request #10 from Freax13/master
derive Default for Volatile, WriteOnly and ReadOnly
2 parents a5a6d78 + fc99347 commit abb7bfa

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/lib.rs

+11-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#![cfg_attr(feature="const_fn", feature(const_fn))]
1+
#![cfg_attr(feature = "const_fn", feature(const_fn))]
22

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

@@ -50,7 +50,7 @@ impl<T: Copy> Volatile<T> {
5050
/// # Panics
5151
///
5252
/// This method never panics.
53-
#[cfg(feature="const_fn")]
53+
#[cfg(feature = "const_fn")]
5454
pub const fn new(value: T) -> Volatile<T> {
5555
Volatile(value)
5656
}
@@ -66,7 +66,7 @@ impl<T: Copy> Volatile<T> {
6666
/// # Panics
6767
///
6868
/// This method never panics.
69-
#[cfg(not(feature="const_fn"))]
69+
#[cfg(not(feature = "const_fn"))]
7070
pub fn new(value: T) -> Volatile<T> {
7171
Volatile(value)
7272
}
@@ -133,7 +133,8 @@ impl<T: Copy> Volatile<T> {
133133
///
134134
/// Ths method never panics.
135135
pub fn update<F>(&mut self, f: F)
136-
where F: FnOnce(&mut T)
136+
where
137+
F: FnOnce(&mut T),
137138
{
138139
let mut value = self.read();
139140
f(&mut value);
@@ -150,7 +151,7 @@ impl<T: Copy> Clone for Volatile<T> {
150151
/// A volatile wrapper which only allows read operations.
151152
///
152153
/// The size of this struct is the same as the contained type.
153-
#[derive(Debug, Clone)]
154+
#[derive(Debug, Clone, Default)]
154155
pub struct ReadOnly<T: Copy>(Volatile<T>);
155156

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

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

302303
// UNSAFE: Safe, as we know the value exists on the stack.
303-
unsafe { (*volatile_ptr).write(42u32); }
304+
unsafe {
305+
(*volatile_ptr).write(42u32);
306+
}
304307

305308
assert_eq!(target_value, 42u32);
306309
}

0 commit comments

Comments
 (0)