Skip to content

Commit 5dd94e7

Browse files
refactor to use &raw mut
Replacing all occurrences of `addr_of_mut!(place)` with `&raw mut place`. This will allow us to reduce macro complexity, and improve consistency with existing reference syntax as `&raw mut` is similar to `&mut` making it fit more naturally with other existing code. Suggested-by: Benno Lossin <[email protected]> Link: Rust-for-Linux/linux#1148 Signed-off-by: Antonio Hickey <[email protected]>
1 parent f2451d9 commit 5dd94e7

File tree

8 files changed

+32
-32
lines changed

8 files changed

+32
-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`

build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_version::{version, Version};
33
fn main() {
44
println!("cargo::rustc-check-cfg=cfg(RUSTC_LINT_REASONS_IS_STABLE)");
55
println!("cargo::rustc-check-cfg=cfg(RUSTC_NEW_UNINIT_IS_STABLE)");
6+
println!("cargo::rustc-check-cfg=cfg(RUSTC_RAW_REF_OP_IS_STABLE)");
67
println!("cargo::rustc-check-cfg=cfg(CONFIG_RUSTC_HAS_UNSAFE_PINNED)");
78
if version().unwrap() >= Version::parse("1.81.0").unwrap()
89
|| version().unwrap() >= Version::parse("1.81.0-nightly").unwrap()
@@ -15,4 +16,7 @@ fn main() {
1516
if version().unwrap() >= Version::parse("1.89.0-nightly").unwrap() {
1617
println!("cargo:rustc-cfg=CONFIG_RUSTC_HAS_UNSAFE_PINNED");
1718
}
19+
if version().unwrap() >= Version::parse("1.82.0").unwrap() {
20+
println!("cargo:rustc-cfg=RUSTC_RAW_REF_OP_IS_STABLE");
21+
}
1822
}

examples/big_struct_in_place.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0 OR MIT
22

3+
#![cfg_attr(not(RUSTC_RAW_REF_OP_IS_STABLE), feature(raw_ref_op))]
4+
35
use pin_init::*;
46

57
// Struct with size over 1GiB

examples/pthread_mutex.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![allow(clippy::undocumented_unsafe_blocks)]
55
#![cfg_attr(feature = "alloc", feature(allocator_api))]
66
#![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))]
7+
#![cfg_attr(not(RUSTC_RAW_REF_OP_IS_STABLE), feature(raw_ref_op))]
78

89
#[cfg(not(windows))]
910
mod pthread_mtx {

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
@@ -4,13 +4,7 @@
44

55
#[cfg(all(not(feature = "std"), feature = "alloc"))]
66
use alloc::sync::Arc;
7-
use core::{
8-
convert::Infallible,
9-
marker::PhantomPinned,
10-
mem::MaybeUninit,
11-
pin::Pin,
12-
ptr::{self, addr_of_mut},
13-
};
7+
use core::{convert::Infallible, marker::PhantomPinned, mem::MaybeUninit, pin::Pin, ptr};
148
use pin_init::*;
159
#[cfg(feature = "std")]
1610
use std::sync::Arc;
@@ -50,8 +44,8 @@ impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
5044
// SAFETY: The elements of the array can be uninitialized.
5145
buffer <- unsafe { init_from_closure(|_| Ok::<_, Infallible>(())) },
5246
// SAFETY: `this` is a valid pointer.
53-
head: unsafe { addr_of_mut!((*this.as_ptr()).buffer).cast::<T>() },
54-
tail: unsafe { addr_of_mut!((*this.as_ptr()).buffer).cast::<T>() },
47+
head: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::<T>() },
48+
tail: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::<T>() },
5549
_pin: PhantomPinned,
5650
})
5751
}
@@ -112,7 +106,7 @@ impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
112106
unsafe fn advance(&mut self, ptr: *mut T) -> *mut T {
113107
// SAFETY: ptr's offset from buffer is < SIZE
114108
let ptr = unsafe { ptr.add(1) };
115-
let origin: *mut _ = addr_of_mut!(self.buffer);
109+
let origin: *mut _ = &raw mut (self.buffer);
116110
let origin = origin.cast::<T>();
117111
let offset = unsafe { ptr.offset_from(origin) };
118112
if offset >= SIZE as isize {

tests/zeroing.rs

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

3-
use core::{marker::PhantomPinned, ptr::addr_of_mut};
4+
use std::marker::PhantomPinned;
45

56
use pin_init::*;
67

@@ -22,7 +23,7 @@ impl Foo {
2223
marks: {
2324
let ptr = this.as_ptr();
2425
// SAFETY: project from the NonNull<Foo> to the buf field
25-
let ptr = unsafe { addr_of_mut!((*ptr).buf) }.cast::<u8>();
26+
let ptr = unsafe { &raw mut (*ptr).buf }.cast::<u8>();
2627
[ptr; MARKS]},
2728
..Zeroable::zeroed()
2829
})

0 commit comments

Comments
 (0)