Skip to content

Commit

Permalink
Merge pull request #58 from mkroening/must-use
Browse files Browse the repository at this point in the history
fix: add `#[must_use]` to volatile types, `read`, and `as_raw_ptr`
  • Loading branch information
phil-opp committed Apr 28, 2024
2 parents 6616c04 + da0f987 commit ac3a016
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/volatile_ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ mod very_unstable;
/// to `ReadWrite`, which allows all operations.
///
/// The size of this struct is the same as the size of the contained reference.
#[must_use]
#[repr(transparent)]
pub struct VolatilePtr<'a, T, A = ReadWrite>
where
Expand Down
12 changes: 8 additions & 4 deletions src/volatile_ptr/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ where
/// };
/// assert_eq!(pointer.read(), 42);
/// ```
#[must_use]
pub fn read(self) -> T
where
T: Copy,
Expand Down Expand Up @@ -154,6 +155,7 @@ where
///
/// assert_eq!(unsafe { *unwrapped }, 50); // non volatile access, be careful!
/// ```
#[must_use]
pub fn as_raw_ptr(self) -> NonNull<T> {
self.pointer
}
Expand Down Expand Up @@ -191,10 +193,12 @@ where
///
/// // DON'T DO THIS:
/// let mut readout = 0;
/// unsafe { volatile.map(|value| {
/// readout = *value.as_ptr(); // non-volatile read, might lead to bugs
/// value
/// })};
/// unsafe {
/// let _ = volatile.map(|value| {
/// readout = *value.as_ptr(); // non-volatile read, might lead to bugs
/// value
/// });
/// };
/// ```
///
/// ## Safety
Expand Down
8 changes: 4 additions & 4 deletions src/volatile_ptr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ fn test_slice() {
fn test_bounds_check_1() {
let val: &mut [u32] = &mut [1, 2, 3];
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
volatile.index(3);
let _ = volatile.index(3);
}

#[cfg(feature = "unstable")]
Expand All @@ -138,7 +138,7 @@ fn test_bounds_check_2() {
let val: &mut [u32] = &mut [1, 2, 3];
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
#[allow(clippy::reversed_empty_ranges)]
volatile.index(2..1);
let _ = volatile.index(2..1);
}

#[cfg(feature = "unstable")]
Expand All @@ -147,7 +147,7 @@ fn test_bounds_check_2() {
fn test_bounds_check_3() {
let val: &mut [u32] = &mut [1, 2, 3];
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
volatile.index(4..); // `3..` is is still ok (see next test)
let _ = volatile.index(4..); // `3..` is is still ok (see next test)
}

#[cfg(feature = "unstable")]
Expand All @@ -164,7 +164,7 @@ fn test_bounds_check_4() {
fn test_bounds_check_5() {
let val: &mut [u32] = &mut [1, 2, 3];
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
volatile.index(..4);
let _ = volatile.index(..4);
}

#[cfg(feature = "unstable")]
Expand Down
1 change: 1 addition & 0 deletions src/volatile_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use core::{cmp::Ordering, fmt, hash, marker::PhantomData, ptr::NonNull};
/// to `ReadWrite`, which allows all operations.
///
/// The size of this struct is the same as the size of the contained reference.
#[must_use]
#[repr(transparent)]
pub struct VolatileRef<'a, T, A = ReadWrite>
where
Expand Down

0 comments on commit ac3a016

Please sign in to comment.