Skip to content

Commit 0980883

Browse files
antonio-hickeyBennoLossin
authored andcommitted
rust: pin-init: replace addr_of_mut! with &raw mut
`feature(raw_ref_op)` became stable in Rust 1.82.0 which is the current MSRV of pin-init with no default features. Earlier Rust versions will now need to enable `raw_ref_op` to continue to work with pin-init. This reduces visual complexity and improves consistency with existing reference syntax. Suggested-by: Benno Lossin <lossin@kernel.org> Link: Rust-for-Linux/linux#1148 Closes: Rust-for-Linux/pin-init#99 Signed-off-by: Antonio Hickey <contact@antoniohickey.com> Link: Rust-for-Linux/pin-init@e277630 [ Reworded commit message. - Benno ] Reviewed-by: Gary Guo <gary@garyguo.net> Link: https://patch.msgid.link/20260319093542.3756606-6-lossin@kernel.org Signed-off-by: Benno Lossin <lossin@kernel.org>
1 parent aa9ec94 commit 0980883

8 files changed

Lines changed: 14 additions & 10 deletions

File tree

rust/pin-init/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,6 @@ actually does the initialization in the correct way. Here are the things to look
160160
```rust
161161
use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
162162
use core::{
163-
ptr::addr_of_mut,
164163
marker::PhantomPinned,
165164
cell::UnsafeCell,
166165
pin::Pin,
@@ -199,7 +198,7 @@ impl RawFoo {
199198
unsafe {
200199
pin_init_from_closure(move |slot: *mut Self| {
201200
// `slot` contains uninit memory, avoid creating a reference.
202-
let foo = addr_of_mut!((*slot).foo);
201+
let foo = &raw mut (*slot).foo;
203202
let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
204203

205204
// Initialize the `foo`

rust/pin-init/examples/big_struct_in_place.rs

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

33
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
4+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
45

56
use pin_init::*;
67

rust/pin-init/examples/linked_list.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::undocumented_unsafe_blocks)]
44
#![cfg_attr(feature = "alloc", feature(allocator_api))]
55
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
6+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
67

78
use core::{
89
cell::Cell,

rust/pin-init/examples/mutex.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::undocumented_unsafe_blocks)]
44
#![cfg_attr(feature = "alloc", feature(allocator_api))]
55
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
6+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
67
#![allow(clippy::missing_safety_doc)]
78

89
use core::{

rust/pin-init/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(USE_RUSTC_FEATURES, feature(lint_reasons))]
7+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
78

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

rust/pin-init/examples/static_init.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#![allow(clippy::undocumented_unsafe_blocks)]
44
#![cfg_attr(feature = "alloc", feature(allocator_api))]
55
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
6+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
67
#![allow(unused_imports)]
78

89
use core::{

rust/pin-init/internal/src/init.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ fn init_fields(
270270
{
271271
#value_prep
272272
// SAFETY: TODO
273-
unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
273+
unsafe { #write(&raw mut (*#slot).#ident, #value_ident) };
274274
}
275275
#(#cfgs)*
276276
#[allow(unused_variables)]
@@ -293,7 +293,7 @@ fn init_fields(
293293
// return when an error/panic occurs.
294294
// - We also use `#data` to require the correct trait (`Init` or `PinInit`)
295295
// for `#ident`.
296-
unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
296+
unsafe { #data.#ident(&raw mut (*#slot).#ident, #init)? };
297297
},
298298
quote! {
299299
// SAFETY: TODO
@@ -308,7 +308,7 @@ fn init_fields(
308308
unsafe {
309309
::pin_init::Init::__init(
310310
#init,
311-
::core::ptr::addr_of_mut!((*#slot).#ident),
311+
&raw mut (*#slot).#ident,
312312
)?
313313
};
314314
},
@@ -348,7 +348,7 @@ fn init_fields(
348348
// SAFETY: We forget the guard later when initialization has succeeded.
349349
let #guard = unsafe {
350350
::pin_init::__internal::DropGuard::new(
351-
::core::ptr::addr_of_mut!((*slot).#ident)
351+
&raw mut (*slot).#ident
352352
)
353353
};
354354
});

rust/pin-init/src/lib.rs

Lines changed: 4 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`
@@ -265,6 +264,7 @@
265264
//! [Rust-for-Linux]: https://rust-for-linux.com/
266265
267266
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
267+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
268268
#![cfg_attr(
269269
all(any(feature = "alloc", feature = "std"), USE_RUSTC_FEATURES),
270270
feature(new_uninit)
@@ -754,7 +754,7 @@ macro_rules! stack_try_pin_init {
754754
///
755755
/// ```rust
756756
/// # use pin_init::*;
757-
/// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
757+
/// # use core::marker::PhantomPinned;
758758
/// #[pin_data]
759759
/// #[derive(Zeroable)]
760760
/// struct Buf {
@@ -768,7 +768,7 @@ macro_rules! stack_try_pin_init {
768768
/// let init = pin_init!(&this in Buf {
769769
/// buf: [0; 64],
770770
/// // SAFETY: TODO.
771-
/// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
771+
/// ptr: unsafe { (&raw mut (*this.as_ptr()).buf).cast() },
772772
/// pin: PhantomPinned,
773773
/// });
774774
/// let init = pin_init!(Buf {

0 commit comments

Comments
 (0)