Skip to content

Commit

Permalink
Remove *_mut variants of pointer methods
Browse files Browse the repository at this point in the history
There is no reason to have separate methods now that all methods take `self` by value.
  • Loading branch information
phil-opp committed Jun 24, 2023
1 parent 759e312 commit 24db296
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 224 deletions.
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,5 @@ pub use volatile_ptr::VolatilePtr;
pub use volatile_ref::VolatileRef;

pub mod access;
mod macros;
mod volatile_ptr;
mod volatile_ref;
19 changes: 0 additions & 19 deletions src/macros.rs → src/volatile_ptr/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ macro_rules! map_field {
// sure that the field is not potentially unaligned. The body of the
// if statement will never be executed, so it can never cause any UB.
if false {
#[deny(unaligned_references)]
let _ref_to_field = &(unsafe { &*$volatile.as_raw_ptr().as_ptr() }).$place;
}

Expand All @@ -48,21 +47,3 @@ macro_rules! map_field {
}
}};
}

#[macro_export]
macro_rules! map_field_mut {
($volatile:ident.$place:ident) => {{
// Simulate creating a reference to the field. This is done to make
// sure that the field is not potentially unaligned. The body of the
// if statement will never be executed, so it can never cause any UB.
if false {
let _ref_to_field = &(unsafe { &*$volatile.as_raw_ptr().as_ptr() }).$place;
}

unsafe {
$volatile.map_mut(|ptr| {
core::ptr::NonNull::new(core::ptr::addr_of_mut!((*ptr.as_ptr()).$place)).unwrap()
})
}
}};
}
2 changes: 2 additions & 0 deletions src/volatile_ptr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use core::{fmt, marker::PhantomData, ptr::NonNull};

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

mod macros;
mod operations;

#[cfg(test)]
mod tests;
#[cfg(feature = "unstable")]
Expand Down
19 changes: 7 additions & 12 deletions src/volatile_ptr/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,20 +200,15 @@ where
/// value
/// })};
/// ```
pub unsafe fn map<F, U>(self, f: F) -> VolatilePtr<'a, U, A::RestrictShared>
///
/// ## Safety
///
/// The pointer returned by `f` must satisfy the requirements of [`Self::new`].
pub unsafe fn map<F, U>(self, f: F) -> VolatilePtr<'a, U, A>
where
F: FnOnce(NonNull<T>) -> NonNull<U>,
A: Access,
U: ?Sized,
{
unsafe { VolatilePtr::new_restricted(Default::default(), f(self.pointer)) }
}

pub unsafe fn map_mut<F, U>(self, f: F) -> VolatilePtr<'a, U, A>
where
F: FnOnce(NonNull<T>) -> NonNull<U>,
U: ?Sized,
A: Access,
{
unsafe { VolatilePtr::new_restricted(A::default(), f(self.pointer)) }
}
Expand Down Expand Up @@ -250,15 +245,15 @@ where
/// Creating a write-only pointer to a struct field:
///
/// ```
/// use volatile::{VolatilePtr, map_field_mut};
/// use volatile::{VolatilePtr, map_field};
/// use core::ptr::NonNull;
///
/// struct Example { field_1: u32, field_2: u8, }
/// let mut value = Example { field_1: 15, field_2: 255 };
/// let mut volatile = unsafe { VolatilePtr::new((&mut value).into()) };
///
/// // construct a volatile write-only pointer to `field_2`
/// let mut field_2 = map_field_mut!(volatile.field_2).write_only();
/// let mut field_2 = map_field!(volatile.field_2).write_only();
/// field_2.write(14);
/// // field_2.read(); // compile-time error
/// ```
Expand Down
26 changes: 13 additions & 13 deletions src/volatile_ptr/tests.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
access::{ReadOnly, ReadWrite, WriteOnly},
map_field_mut, VolatilePtr,
map_field, VolatilePtr,
};
use core::ptr::NonNull;

Expand Down Expand Up @@ -66,11 +66,11 @@ fn test_struct() {
};
let volatile = unsafe { VolatilePtr::new(NonNull::from(&mut val)) };
unsafe {
volatile.map_mut(|s| NonNull::new(core::ptr::addr_of_mut!((*s.as_ptr()).field_1)).unwrap())
volatile.map(|s| NonNull::new(core::ptr::addr_of_mut!((*s.as_ptr()).field_1)).unwrap())
}
.update(|v| v + 1);
let field_2 = unsafe {
volatile.map_mut(|s| NonNull::new(core::ptr::addr_of_mut!((*s.as_ptr()).field_2)).unwrap())
volatile.map(|s| NonNull::new(core::ptr::addr_of_mut!((*s.as_ptr()).field_2)).unwrap())
};
assert!(field_2.read());
field_2.write(false);
Expand All @@ -96,9 +96,9 @@ fn test_struct_macro() {
field_2: true,
};
let volatile = unsafe { VolatilePtr::new(NonNull::from(&mut val)) };
let field_1 = map_field_mut!(volatile.field_1);
let field_1 = map_field!(volatile.field_1);
field_1.update(|v| v + 1);
let field_2 = map_field_mut!(volatile.field_2);
let field_2 = map_field!(volatile.field_2);
assert!(field_2.read());
field_2.write(false);
assert_eq!(
Expand All @@ -115,7 +115,7 @@ fn test_struct_macro() {
fn test_slice() {
let val: &mut [u32] = &mut [1, 2, 3];
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
volatile.index_mut(0).update(|v| v + 1);
volatile.index(0).update(|v| v + 1);

let mut dst = [0; 3];
volatile.copy_into_slice(&mut dst);
Expand All @@ -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_mut(3);
volatile.index(3);
}

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

#[cfg(feature = "unstable")]
Expand All @@ -146,15 +146,15 @@ 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_mut(4..); // `3..` is is still ok (see next test)
volatile.index(4..); // `3..` is is still ok (see next test)
}

#[cfg(feature = "unstable")]
#[test]
fn test_bounds_check_4() {
let val: &mut [u32] = &mut [1, 2, 3];
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
assert_eq!(volatile.index_mut(3..).len(), 0);
assert_eq!(volatile.index(3..).len(), 0);
}

#[cfg(feature = "unstable")]
Expand All @@ -163,16 +163,16 @@ 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_mut(..4);
volatile.index(..4);
}

#[cfg(feature = "unstable")]
#[test]
fn test_chunks() {
let val: &mut [u32] = &mut [1, 2, 3, 4, 5, 6];
let volatile = unsafe { VolatilePtr::new(NonNull::from(val)) };
let chunks = volatile.as_chunks_mut().0;
chunks.index_mut(1).write([10, 11, 12]);
let chunks = volatile.as_chunks().0;
chunks.index(1).write([10, 11, 12]);
assert_eq!(chunks.index(0).read(), [1, 2, 3]);
assert_eq!(chunks.index(1).read(), [10, 11, 12]);
}
Loading

0 comments on commit 24db296

Please sign in to comment.