Skip to content

fix: add #[must_use] to volatile types, read, and as_raw_ptr #58

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 4 commits into from
Apr 28, 2024
Merged
Show file tree
Hide file tree
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
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]
Copy link
Member

Choose a reason for hiding this comment

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

A user might be calling this function to cause side-effects and not necessarily because they're interested in the value.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, that's true.

For those cases, it might still be beneficial to be explicit about wanting side effects by discarding the value:

let _side_effect = pointer.read();

I can remove it, if you want me to, though. I would prefer #[must_use] and be explicit about side effects, but I have no strong opinion on this.

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