mirror of
https://github.com/torvalds/linux.git
synced 2026-04-18 06:44:00 -04:00
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: https://github.com/Rust-for-Linux/linux/issues/1148
Closes: https://github.com/Rust-for-Linux/pin-init/issues/99
Signed-off-by: Antonio Hickey <contact@antoniohickey.com>
Link: e27763004e
[ 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>
This commit is contained in:
committed by
Benno Lossin
parent
aa9ec9460d
commit
09808839c7
@@ -160,7 +160,6 @@ actually does the initialization in the correct way. Here are the things to look
|
||||
```rust
|
||||
use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
|
||||
use core::{
|
||||
ptr::addr_of_mut,
|
||||
marker::PhantomPinned,
|
||||
cell::UnsafeCell,
|
||||
pin::Pin,
|
||||
@@ -199,7 +198,7 @@ impl RawFoo {
|
||||
unsafe {
|
||||
pin_init_from_closure(move |slot: *mut Self| {
|
||||
// `slot` contains uninit memory, avoid creating a reference.
|
||||
let foo = addr_of_mut!((*slot).foo);
|
||||
let foo = &raw mut (*slot).foo;
|
||||
let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
|
||||
|
||||
// Initialize the `foo`
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0 OR MIT
|
||||
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
|
||||
|
||||
use pin_init::*;
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#![allow(clippy::undocumented_unsafe_blocks)]
|
||||
#![cfg_attr(feature = "alloc", feature(allocator_api))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
|
||||
|
||||
use core::{
|
||||
cell::Cell,
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#![allow(clippy::undocumented_unsafe_blocks)]
|
||||
#![cfg_attr(feature = "alloc", feature(allocator_api))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
|
||||
#![allow(clippy::missing_safety_doc)]
|
||||
|
||||
use core::{
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#![allow(clippy::undocumented_unsafe_blocks)]
|
||||
#![cfg_attr(feature = "alloc", feature(allocator_api))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
|
||||
|
||||
#[cfg(not(windows))]
|
||||
mod pthread_mtx {
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#![allow(clippy::undocumented_unsafe_blocks)]
|
||||
#![cfg_attr(feature = "alloc", feature(allocator_api))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
|
||||
#![allow(unused_imports)]
|
||||
|
||||
use core::{
|
||||
|
||||
@@ -270,7 +270,7 @@ fn init_fields(
|
||||
{
|
||||
#value_prep
|
||||
// SAFETY: TODO
|
||||
unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
|
||||
unsafe { #write(&raw mut (*#slot).#ident, #value_ident) };
|
||||
}
|
||||
#(#cfgs)*
|
||||
#[allow(unused_variables)]
|
||||
@@ -293,7 +293,7 @@ fn init_fields(
|
||||
// return when an error/panic occurs.
|
||||
// - We also use `#data` to require the correct trait (`Init` or `PinInit`)
|
||||
// for `#ident`.
|
||||
unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
|
||||
unsafe { #data.#ident(&raw mut (*#slot).#ident, #init)? };
|
||||
},
|
||||
quote! {
|
||||
// SAFETY: TODO
|
||||
@@ -308,7 +308,7 @@ fn init_fields(
|
||||
unsafe {
|
||||
::pin_init::Init::__init(
|
||||
#init,
|
||||
::core::ptr::addr_of_mut!((*#slot).#ident),
|
||||
&raw mut (*#slot).#ident,
|
||||
)?
|
||||
};
|
||||
},
|
||||
@@ -348,7 +348,7 @@ fn init_fields(
|
||||
// SAFETY: We forget the guard later when initialization has succeeded.
|
||||
let #guard = unsafe {
|
||||
::pin_init::__internal::DropGuard::new(
|
||||
::core::ptr::addr_of_mut!((*slot).#ident)
|
||||
&raw mut (*slot).#ident
|
||||
)
|
||||
};
|
||||
});
|
||||
|
||||
@@ -172,7 +172,6 @@
|
||||
//! # #![feature(extern_types)]
|
||||
//! use pin_init::{pin_data, pinned_drop, PinInit, PinnedDrop, pin_init_from_closure};
|
||||
//! use core::{
|
||||
//! ptr::addr_of_mut,
|
||||
//! marker::PhantomPinned,
|
||||
//! cell::UnsafeCell,
|
||||
//! pin::Pin,
|
||||
@@ -211,7 +210,7 @@
|
||||
//! unsafe {
|
||||
//! pin_init_from_closure(move |slot: *mut Self| {
|
||||
//! // `slot` contains uninit memory, avoid creating a reference.
|
||||
//! let foo = addr_of_mut!((*slot).foo);
|
||||
//! let foo = &raw mut (*slot).foo;
|
||||
//! let foo = UnsafeCell::raw_get(foo).cast::<bindings::foo>();
|
||||
//!
|
||||
//! // Initialize the `foo`
|
||||
@@ -265,6 +264,7 @@
|
||||
//! [Rust-for-Linux]: https://rust-for-linux.com/
|
||||
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
|
||||
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
|
||||
#![cfg_attr(
|
||||
all(any(feature = "alloc", feature = "std"), USE_RUSTC_FEATURES),
|
||||
feature(new_uninit)
|
||||
@@ -754,7 +754,7 @@ macro_rules! stack_try_pin_init {
|
||||
///
|
||||
/// ```rust
|
||||
/// # use pin_init::*;
|
||||
/// # use core::{ptr::addr_of_mut, marker::PhantomPinned};
|
||||
/// # use core::marker::PhantomPinned;
|
||||
/// #[pin_data]
|
||||
/// #[derive(Zeroable)]
|
||||
/// struct Buf {
|
||||
@@ -768,7 +768,7 @@ macro_rules! stack_try_pin_init {
|
||||
/// let init = pin_init!(&this in Buf {
|
||||
/// buf: [0; 64],
|
||||
/// // SAFETY: TODO.
|
||||
/// ptr: unsafe { addr_of_mut!((*this.as_ptr()).buf).cast() },
|
||||
/// ptr: unsafe { (&raw mut (*this.as_ptr()).buf).cast() },
|
||||
/// pin: PhantomPinned,
|
||||
/// });
|
||||
/// let init = pin_init!(Buf {
|
||||
|
||||
Reference in New Issue
Block a user