Skip to content

Commit 24db296

Browse files
committed
Remove *_mut variants of pointer methods
There is no reason to have separate methods now that all methods take `self` by value.
1 parent 759e312 commit 24db296

File tree

8 files changed

+36
-224
lines changed

8 files changed

+36
-224
lines changed

src/lib.rs

-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,5 @@ pub use volatile_ptr::VolatilePtr;
5050
pub use volatile_ref::VolatileRef;
5151

5252
pub mod access;
53-
mod macros;
5453
mod volatile_ptr;
5554
mod volatile_ref;

src/macros.rs renamed to src/volatile_ptr/macros.rs

-19
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ macro_rules! map_field {
3737
// sure that the field is not potentially unaligned. The body of the
3838
// if statement will never be executed, so it can never cause any UB.
3939
if false {
40-
#[deny(unaligned_references)]
4140
let _ref_to_field = &(unsafe { &*$volatile.as_raw_ptr().as_ptr() }).$place;
4241
}
4342

@@ -48,21 +47,3 @@ macro_rules! map_field {
4847
}
4948
}};
5049
}
51-
52-
#[macro_export]
53-
macro_rules! map_field_mut {
54-
($volatile:ident.$place:ident) => {{
55-
// Simulate creating a reference to the field. This is done to make
56-
// sure that the field is not potentially unaligned. The body of the
57-
// if statement will never be executed, so it can never cause any UB.
58-
if false {
59-
let _ref_to_field = &(unsafe { &*$volatile.as_raw_ptr().as_ptr() }).$place;
60-
}
61-
62-
unsafe {
63-
$volatile.map_mut(|ptr| {
64-
core::ptr::NonNull::new(core::ptr::addr_of_mut!((*ptr.as_ptr()).$place)).unwrap()
65-
})
66-
}
67-
}};
68-
}

src/volatile_ptr/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use core::{fmt, marker::PhantomData, ptr::NonNull};
22

33
use crate::access::{ReadWrite, Readable};
44

5+
mod macros;
56
mod operations;
7+
68
#[cfg(test)]
79
mod tests;
810
#[cfg(feature = "unstable")]

src/volatile_ptr/operations.rs

+7-12
Original file line numberDiff line numberDiff line change
@@ -200,20 +200,15 @@ where
200200
/// value
201201
/// })};
202202
/// ```
203-
pub unsafe fn map<F, U>(self, f: F) -> VolatilePtr<'a, U, A::RestrictShared>
203+
///
204+
/// ## Safety
205+
///
206+
/// The pointer returned by `f` must satisfy the requirements of [`Self::new`].
207+
pub unsafe fn map<F, U>(self, f: F) -> VolatilePtr<'a, U, A>
204208
where
205209
F: FnOnce(NonNull<T>) -> NonNull<U>,
206210
A: Access,
207211
U: ?Sized,
208-
{
209-
unsafe { VolatilePtr::new_restricted(Default::default(), f(self.pointer)) }
210-
}
211-
212-
pub unsafe fn map_mut<F, U>(self, f: F) -> VolatilePtr<'a, U, A>
213-
where
214-
F: FnOnce(NonNull<T>) -> NonNull<U>,
215-
U: ?Sized,
216-
A: Access,
217212
{
218213
unsafe { VolatilePtr::new_restricted(A::default(), f(self.pointer)) }
219214
}
@@ -250,15 +245,15 @@ where
250245
/// Creating a write-only pointer to a struct field:
251246
///
252247
/// ```
253-
/// use volatile::{VolatilePtr, map_field_mut};
248+
/// use volatile::{VolatilePtr, map_field};
254249
/// use core::ptr::NonNull;
255250
///
256251
/// struct Example { field_1: u32, field_2: u8, }
257252
/// let mut value = Example { field_1: 15, field_2: 255 };
258253
/// let mut volatile = unsafe { VolatilePtr::new((&mut value).into()) };
259254
///
260255
/// // construct a volatile write-only pointer to `field_2`
261-
/// let mut field_2 = map_field_mut!(volatile.field_2).write_only();
256+
/// let mut field_2 = map_field!(volatile.field_2).write_only();
262257
/// field_2.write(14);
263258
/// // field_2.read(); // compile-time error
264259
/// ```

src/volatile_ptr/tests.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{
22
access::{ReadOnly, ReadWrite, WriteOnly},
3-
map_field_mut, VolatilePtr,
3+
map_field, VolatilePtr,
44
};
55
use core::ptr::NonNull;
66

@@ -66,11 +66,11 @@ fn test_struct() {
6666
};
6767
let volatile = unsafe { VolatilePtr::new(NonNull::from(&mut val)) };
6868
unsafe {
69-
volatile.map_mut(|s| NonNull::new(core::ptr::addr_of_mut!((*s.as_ptr()).field_1)).unwrap())
69+
volatile.map(|s| NonNull::new(core::ptr::addr_of_mut!((*s.as_ptr()).field_1)).unwrap())
7070
}
7171
.update(|v| v + 1);
7272
let field_2 = unsafe {
73-
volatile.map_mut(|s| NonNull::new(core::ptr::addr_of_mut!((*s.as_ptr()).field_2)).unwrap())
73+
volatile.map(|s| NonNull::new(core::ptr::addr_of_mut!((*s.as_ptr()).field_2)).unwrap())
7474
};
7575
assert!(field_2.read());
7676
field_2.write(false);
@@ -96,9 +96,9 @@ fn test_struct_macro() {
9696
field_2: true,
9797
};
9898
let volatile = unsafe { VolatilePtr::new(NonNull::from(&mut val)) };
99-
let field_1 = map_field_mut!(volatile.field_1);
99+
let field_1 = map_field!(volatile.field_1);
100100
field_1.update(|v| v + 1);
101-
let field_2 = map_field_mut!(volatile.field_2);
101+
let field_2 = map_field!(volatile.field_2);
102102
assert!(field_2.read());
103103
field_2.write(false);
104104
assert_eq!(
@@ -115,7 +115,7 @@ fn test_struct_macro() {
115115
fn test_slice() {
116116
let val: &mut [u32] = &mut [1, 2, 3];
117117
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
118-
volatile.index_mut(0).update(|v| v + 1);
118+
volatile.index(0).update(|v| v + 1);
119119

120120
let mut dst = [0; 3];
121121
volatile.copy_into_slice(&mut dst);
@@ -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_mut(3);
131+
volatile.index(3);
132132
}
133133

134134
#[cfg(feature = "unstable")]
@@ -137,7 +137,7 @@ fn test_bounds_check_1() {
137137
fn test_bounds_check_2() {
138138
let val: &mut [u32] = &mut [1, 2, 3];
139139
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
140-
volatile.index_mut(2..1);
140+
volatile.index(2..1);
141141
}
142142

143143
#[cfg(feature = "unstable")]
@@ -146,15 +146,15 @@ fn test_bounds_check_2() {
146146
fn test_bounds_check_3() {
147147
let val: &mut [u32] = &mut [1, 2, 3];
148148
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
149-
volatile.index_mut(4..); // `3..` is is still ok (see next test)
149+
volatile.index(4..); // `3..` is is still ok (see next test)
150150
}
151151

152152
#[cfg(feature = "unstable")]
153153
#[test]
154154
fn test_bounds_check_4() {
155155
let val: &mut [u32] = &mut [1, 2, 3];
156156
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
157-
assert_eq!(volatile.index_mut(3..).len(), 0);
157+
assert_eq!(volatile.index(3..).len(), 0);
158158
}
159159

160160
#[cfg(feature = "unstable")]
@@ -163,16 +163,16 @@ fn test_bounds_check_4() {
163163
fn test_bounds_check_5() {
164164
let val: &mut [u32] = &mut [1, 2, 3];
165165
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
166-
volatile.index_mut(..4);
166+
volatile.index(..4);
167167
}
168168

169169
#[cfg(feature = "unstable")]
170170
#[test]
171171
fn test_chunks() {
172172
let val: &mut [u32] = &mut [1, 2, 3, 4, 5, 6];
173173
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
174-
let chunks = volatile.as_chunks_mut().0;
175-
chunks.index_mut(1).write([10, 11, 12]);
174+
let chunks = volatile.as_chunks().0;
175+
chunks.index(1).write([10, 11, 12]);
176176
assert_eq!(chunks.index(0).read(), [1, 2, 3]);
177177
assert_eq!(chunks.index(1).read(), [10, 11, 12]);
178178
}

0 commit comments

Comments
 (0)