Skip to content

Commit 4b156f9

Browse files
refactor to use &raw mut
Signed-off-by: Antonio Hickey <[email protected]>
1 parent 21f7275 commit 4b156f9

File tree

5 files changed

+24
-32
lines changed

5 files changed

+24
-32
lines changed

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,6 @@ actually does the initialization in the correct way. Here are the things to look
148148
```rust
149149
use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
150150
use core::{
151-
ptr::addr_of_mut,
152151
marker::PhantomPinned,
153152
cell::UnsafeCell,
154153
pin::Pin,
@@ -187,7 +186,7 @@ impl RawFoo {
187186
unsafe {
188187
pin_init_from_closure(move |slot: *mut Self| {
189188
// `slot` contains uninit memory, avoid creating a reference.
190-
let foo = addr_of_mut!((*slot).foo);
189+
let foo = &raw mut (*slot).foo;
191190
let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
192191

193192
// Initialize the `foo`

src/lib.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@
172172
//! # #![feature(extern_types)]
173173
//! use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
174174
//! use core::{
175-
//! ptr::addr_of_mut,
176175
//! marker::PhantomPinned,
177176
//! cell::UnsafeCell,
178177
//! pin::Pin,
@@ -211,7 +210,7 @@
211210
//! unsafe {
212211
//! pin_init_from_closure(move |slot: *mut Self| {
213212
//! // `slot` contains uninit memory, avoid creating a reference.
214-
//! let foo = addr_of_mut!((*slot).foo);
213+
//! let foo = &raw mut (*slot).foo;
215214
//! let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
216215
//!
217216
//! // Initialize the `foo`
@@ -750,7 +749,7 @@ macro_rules! stack_try_pin_init {
750749
///
751750
/// ```rust
752751
/// # use pin_init::*;
753-
/// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
752+
/// # use core::marker::PhantomPinned;
754753
/// #[pin_data]
755754
/// #[derive(Zeroable)]
756755
/// struct Buf {
@@ -764,7 +763,7 @@ macro_rules! stack_try_pin_init {
764763
/// let init = pin_init!(&this in Buf {
765764
/// buf: [0; 64],
766765
/// // SAFETY: TODO.
767-
/// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
766+
/// ptr: unsafe { (&raw mut (*this.as_ptr()).buf).cast() },
768767
/// pin: PhantomPinned,
769768
/// });
770769
/// let init = pin_init!(Buf {

src/macros.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -244,25 +244,25 @@
244244
//! struct __InitOk;
245245
//! // This is the expansion of `t,`, which is syntactic sugar for `t: t,`.
246246
//! {
247-
//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).t), t) };
247+
//! unsafe { ::core::ptr::write(&raw mut (*slot).t, t) };
248248
//! }
249249
//! // Since initialization could fail later (not in this case, since the
250250
//! // error type is `Infallible`) we will need to drop this field if there
251251
//! // is an error later. This `DropGuard` will drop the field when it gets
252252
//! // dropped and has not yet been forgotten.
253253
//! let __t_guard = unsafe {
254-
//! ::pin_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).t))
254+
//! ::pin_init::__internal::DropGuard::new(&raw mut (*slot).t)
255255
//! };
256256
//! // Expansion of `x: 0,`:
257257
//! // Since this can be an arbitrary expression we cannot place it inside
258258
//! // of the `unsafe` block, so we bind it here.
259259
//! {
260260
//! let x = 0;
261-
//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).x), x) };
261+
//! unsafe { ::core::ptr::write(&raw mut (*slot).x, x) };
262262
//! }
263263
//! // We again create a `DropGuard`.
264264
//! let __x_guard = unsafe {
265-
//! ::pin_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).x))
265+
//! ::pin_init::__internal::DropGuard::new(&raw mut (*slot).x)
266266
//! };
267267
//! // Since initialization has successfully completed, we can now forget
268268
//! // the guards. This is not `mem::forget`, since we only have
@@ -459,15 +459,15 @@
459459
//! {
460460
//! struct __InitOk;
461461
//! {
462-
//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).a), a) };
462+
//! unsafe { ::core::ptr::write(&raw mut (*slot).a, a) };
463463
//! }
464464
//! let __a_guard = unsafe {
465-
//! ::pin_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).a))
465+
//! ::pin_init::__internal::DropGuard::new(&raw mut (*slot).a)
466466
//! };
467467
//! let init = Bar::new(36);
468-
//! unsafe { data.b(::core::addr_of_mut!((*slot).b), b)? };
468+
//! unsafe { data.b(&raw mut (*slot).b, b)? };
469469
//! let __b_guard = unsafe {
470-
//! ::pin_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).b))
470+
//! ::pin_init::__internal::DropGuard::new(&raw mut (*slot).b)
471471
//! };
472472
//! ::core::mem::forget(__b_guard);
473473
//! ::core::mem::forget(__a_guard);
@@ -1215,15 +1215,15 @@ macro_rules! __init_internal {
12151215
// SAFETY: `slot` is valid, because we are inside of an initializer closure, we
12161216
// return when an error/panic occurs.
12171217
// We also use the `data` to require the correct trait (`Init` or `PinInit`) for `$field`.
1218-
unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), init)? };
1218+
unsafe { $data.$field(&raw mut (*$slot).$field, init)? };
12191219
// Create the drop guard:
12201220
//
12211221
// We rely on macro hygiene to make it impossible for users to access this local variable.
12221222
// We use `paste!` to create new hygiene for `$field`.
12231223
$crate::macros::paste! {
12241224
// SAFETY: We forget the guard later when initialization has succeeded.
12251225
let [< __ $field _guard >] = unsafe {
1226-
$crate::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
1226+
$crate::__internal::DropGuard::new(&raw mut (*$slot).$field)
12271227
};
12281228

12291229
$crate::__init_internal!(init_slot($use_data):
@@ -1246,15 +1246,15 @@ macro_rules! __init_internal {
12461246
//
12471247
// SAFETY: `slot` is valid, because we are inside of an initializer closure, we
12481248
// return when an error/panic occurs.
1249-
unsafe { $crate::Init::__init(init, ::core::ptr::addr_of_mut!((*$slot).$field))? };
1249+
unsafe { $crate::Init::__init(init, &raw mut (*$slot).$field)? };
12501250
// Create the drop guard:
12511251
//
12521252
// We rely on macro hygiene to make it impossible for users to access this local variable.
12531253
// We use `paste!` to create new hygiene for `$field`.
12541254
$crate::macros::paste! {
12551255
// SAFETY: We forget the guard later when initialization has succeeded.
12561256
let [< __ $field _guard >] = unsafe {
1257-
$crate::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
1257+
$crate::__internal::DropGuard::new(&raw mut (*$slot).$field)
12581258
};
12591259

12601260
$crate::__init_internal!(init_slot():
@@ -1277,7 +1277,7 @@ macro_rules! __init_internal {
12771277
// Initialize the field.
12781278
//
12791279
// SAFETY: The memory at `slot` is uninitialized.
1280-
unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
1280+
unsafe { ::core::ptr::write(&raw mut (*$slot).$field, $field) };
12811281
}
12821282
// Create the drop guard:
12831283
//
@@ -1286,7 +1286,7 @@ macro_rules! __init_internal {
12861286
$crate::macros::paste! {
12871287
// SAFETY: We forget the guard later when initialization has succeeded.
12881288
let [< __ $field _guard >] = unsafe {
1289-
$crate::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field))
1289+
$crate::__internal::DropGuard::new(&raw mut (*$slot).$field)
12901290
};
12911291

12921292
$crate::__init_internal!(init_slot($($use_data)?):

tests/ring_buf.rs

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
33
#![cfg_attr(feature = "alloc", feature(allocator_api))]
44

5-
use core::{
6-
convert::Infallible,
7-
marker::PhantomPinned,
8-
mem::MaybeUninit,
9-
pin::Pin,
10-
ptr::{self, addr_of_mut},
11-
};
5+
use core::{convert::Infallible, marker::PhantomPinned, mem::MaybeUninit, pin::Pin, ptr};
126
use pin_init::*;
137
use std::sync::Arc;
148

@@ -45,8 +39,8 @@ impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
4539
// SAFETY: The elements of the array can be uninitialized.
4640
buffer <- unsafe { init_from_closure(|_| Ok::<_, Infallible>(())) },
4741
// SAFETY: `this` is a valid pointer.
48-
head: unsafe { addr_of_mut!((*this.as_ptr()).buffer).cast::<T>() },
49-
tail: unsafe { addr_of_mut!((*this.as_ptr()).buffer).cast::<T>() },
42+
head: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::<T>() },
43+
tail: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::<T>() },
5044
_pin: PhantomPinned,
5145
})
5246
}
@@ -107,7 +101,7 @@ impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
107101
unsafe fn advance(&mut self, ptr: *mut T) -> *mut T {
108102
// SAFETY: ptr's offset from buffer is < SIZE
109103
let ptr = unsafe { ptr.add(1) };
110-
let origin: *mut _ = addr_of_mut!(self.buffer);
104+
let origin: *mut _ = &raw mut (self.buffer);
111105
let origin = origin.cast::<T>();
112106
let offset = unsafe { ptr.offset_from(origin) };
113107
if offset >= SIZE as isize {

tests/zeroing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
22

3-
use std::{marker::PhantomPinned, ptr::addr_of_mut};
3+
use std::marker::PhantomPinned;
44

55
use pin_init::*;
66

@@ -22,7 +22,7 @@ impl Foo {
2222
marks: {
2323
let ptr = this.as_ptr();
2424
// SAFETY: project from the NonNull<Foo> to the buf field
25-
let ptr = unsafe { addr_of_mut!((*ptr).buf) }.cast::<u8>();
25+
let ptr = unsafe { &raw mut (*ptr).buf }.cast::<u8>();
2626
[ptr; MARKS]},
2727
..Zeroable::zeroed()
2828
})

0 commit comments

Comments
 (0)