Skip to content

Commit ac3a016

Browse files
authored
Merge pull request #58 from mkroening/must-use
fix: add `#[must_use]` to volatile types, `read`, and `as_raw_ptr`
2 parents 6616c04 + da0f987 commit ac3a016

File tree

4 files changed

+14
-8
lines changed

4 files changed

+14
-8
lines changed

src/volatile_ptr/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ mod very_unstable;
2424
/// to `ReadWrite`, which allows all operations.
2525
///
2626
/// The size of this struct is the same as the size of the contained reference.
27+
#[must_use]
2728
#[repr(transparent)]
2829
pub struct VolatilePtr<'a, T, A = ReadWrite>
2930
where

src/volatile_ptr/operations.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ where
8282
/// };
8383
/// assert_eq!(pointer.read(), 42);
8484
/// ```
85+
#[must_use]
8586
pub fn read(self) -> T
8687
where
8788
T: Copy,
@@ -154,6 +155,7 @@ where
154155
///
155156
/// assert_eq!(unsafe { *unwrapped }, 50); // non volatile access, be careful!
156157
/// ```
158+
#[must_use]
157159
pub fn as_raw_ptr(self) -> NonNull<T> {
158160
self.pointer
159161
}
@@ -191,10 +193,12 @@ where
191193
///
192194
/// // DON'T DO THIS:
193195
/// let mut readout = 0;
194-
/// unsafe { volatile.map(|value| {
195-
/// readout = *value.as_ptr(); // non-volatile read, might lead to bugs
196-
/// value
197-
/// })};
196+
/// unsafe {
197+
/// let _ = volatile.map(|value| {
198+
/// readout = *value.as_ptr(); // non-volatile read, might lead to bugs
199+
/// value
200+
/// });
201+
/// };
198202
/// ```
199203
///
200204
/// ## Safety

src/volatile_ptr/tests.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ fn test_slice() {
128128
fn test_bounds_check_1() {
129129
let val: &mut [u32] = &mut [1, 2, 3];
130130
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
131-
volatile.index(3);
131+
let _ = volatile.index(3);
132132
}
133133

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

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

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

170170
#[cfg(feature = "unstable")]

src/volatile_ref.rs

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use core::{cmp::Ordering, fmt, hash, marker::PhantomData, ptr::NonNull};
2525
/// to `ReadWrite`, which allows all operations.
2626
///
2727
/// The size of this struct is the same as the size of the contained reference.
28+
#[must_use]
2829
#[repr(transparent)]
2930
pub struct VolatileRef<'a, T, A = ReadWrite>
3031
where

0 commit comments

Comments
 (0)