diff --git a/README.md b/README.md index 2d0cda9..e7c724b 100644 --- a/README.md +++ b/README.md @@ -148,7 +148,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, @@ -187,7 +186,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::(); // Initialize the `foo` diff --git a/examples/big_struct_in_place.rs b/examples/big_struct_in_place.rs index b0ee793..8a5e9e3 100644 --- a/examples/big_struct_in_place.rs +++ b/examples/big_struct_in_place.rs @@ -1,5 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT +#![feature(raw_ref_op)] + use pin_init::*; // Struct with size over 1GiB diff --git a/examples/pthread_mutex.rs b/examples/pthread_mutex.rs index 6c4d182..222eadd 100644 --- a/examples/pthread_mutex.rs +++ b/examples/pthread_mutex.rs @@ -1,6 +1,7 @@ // SPDX-License-Identifier: Apache-2.0 OR MIT // inspired by +#![feature(raw_ref_op)] #![allow(clippy::undocumented_unsafe_blocks)] #![cfg_attr(feature = "alloc", feature(allocator_api))] #![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] diff --git a/src/lib.rs b/src/lib.rs index 9ab3403..8f89e8d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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::(); //! //! // Initialize the `foo` @@ -750,7 +749,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 { @@ -764,7 +763,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 { diff --git a/src/macros.rs b/src/macros.rs index 935d777..ce0ff76 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -244,25 +244,25 @@ //! struct __InitOk; //! // This is the expansion of `t,`, which is syntactic sugar for `t: t,`. //! { -//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).t), t) }; +//! unsafe { ::core::ptr::write(&raw mut (*slot).t, t) }; //! } //! // Since initialization could fail later (not in this case, since the //! // error type is `Infallible`) we will need to drop this field if there //! // is an error later. This `DropGuard` will drop the field when it gets //! // dropped and has not yet been forgotten. //! let __t_guard = unsafe { -//! ::pin_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).t)) +//! ::pin_init::__internal::DropGuard::new(&raw mut (*slot).t) //! }; //! // Expansion of `x: 0,`: //! // Since this can be an arbitrary expression we cannot place it inside //! // of the `unsafe` block, so we bind it here. //! { //! let x = 0; -//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).x), x) }; +//! unsafe { ::core::ptr::write(&raw mut (*slot).x, x) }; //! } //! // We again create a `DropGuard`. //! let __x_guard = unsafe { -//! ::pin_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).x)) +//! ::pin_init::__internal::DropGuard::new(&raw mut (*slot).x) //! }; //! // Since initialization has successfully completed, we can now forget //! // the guards. This is not `mem::forget`, since we only have @@ -459,15 +459,15 @@ //! { //! struct __InitOk; //! { -//! unsafe { ::core::ptr::write(::core::addr_of_mut!((*slot).a), a) }; +//! unsafe { ::core::ptr::write(&raw mut (*slot).a, a) }; //! } //! let __a_guard = unsafe { -//! ::pin_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).a)) +//! ::pin_init::__internal::DropGuard::new(&raw mut (*slot).a) //! }; //! let init = Bar::new(36); -//! unsafe { data.b(::core::addr_of_mut!((*slot).b), b)? }; +//! unsafe { data.b(&raw mut (*slot).b, b)? }; //! let __b_guard = unsafe { -//! ::pin_init::__internal::DropGuard::new(::core::addr_of_mut!((*slot).b)) +//! ::pin_init::__internal::DropGuard::new(&raw mut (*slot).b) //! }; //! ::core::mem::forget(__b_guard); //! ::core::mem::forget(__a_guard); @@ -1215,7 +1215,7 @@ macro_rules! __init_internal { // SAFETY: `slot` is valid, because we are inside of an initializer closure, we // return when an error/panic occurs. // We also use the `data` to require the correct trait (`Init` or `PinInit`) for `$field`. - unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), init)? }; + unsafe { $data.$field(&raw mut (*$slot).$field, init)? }; // Create the drop guard: // // We rely on macro hygiene to make it impossible for users to access this local variable. @@ -1223,7 +1223,7 @@ macro_rules! __init_internal { $crate::macros::paste! { // SAFETY: We forget the guard later when initialization has succeeded. let [< __ $field _guard >] = unsafe { - $crate::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field)) + $crate::__internal::DropGuard::new(&raw mut (*$slot).$field) }; $crate::__init_internal!(init_slot($use_data): @@ -1246,7 +1246,7 @@ macro_rules! __init_internal { // // SAFETY: `slot` is valid, because we are inside of an initializer closure, we // return when an error/panic occurs. - unsafe { $crate::Init::__init(init, ::core::ptr::addr_of_mut!((*$slot).$field))? }; + unsafe { $crate::Init::__init(init, &raw mut (*$slot).$field)? }; // Create the drop guard: // // We rely on macro hygiene to make it impossible for users to access this local variable. @@ -1254,7 +1254,7 @@ macro_rules! __init_internal { $crate::macros::paste! { // SAFETY: We forget the guard later when initialization has succeeded. let [< __ $field _guard >] = unsafe { - $crate::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field)) + $crate::__internal::DropGuard::new(&raw mut (*$slot).$field) }; $crate::__init_internal!(init_slot(): @@ -1277,7 +1277,7 @@ macro_rules! __init_internal { // Initialize the field. // // SAFETY: The memory at `slot` is uninitialized. - unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) }; + unsafe { ::core::ptr::write(&raw mut (*$slot).$field, $field) }; } // Create the drop guard: // @@ -1286,7 +1286,7 @@ macro_rules! __init_internal { $crate::macros::paste! { // SAFETY: We forget the guard later when initialization has succeeded. let [< __ $field _guard >] = unsafe { - $crate::__internal::DropGuard::new(::core::ptr::addr_of_mut!((*$slot).$field)) + $crate::__internal::DropGuard::new(&raw mut (*$slot).$field) }; $crate::__init_internal!(init_slot($($use_data)?): diff --git a/tests/ring_buf.rs b/tests/ring_buf.rs index 9e3635b..ba00ee7 100644 --- a/tests/ring_buf.rs +++ b/tests/ring_buf.rs @@ -4,13 +4,7 @@ #[cfg(all(not(feature = "std"), feature = "alloc"))] use alloc::sync::Arc; -use core::{ - convert::Infallible, - marker::PhantomPinned, - mem::MaybeUninit, - pin::Pin, - ptr::{self, addr_of_mut}, -}; +use core::{convert::Infallible, marker::PhantomPinned, mem::MaybeUninit, pin::Pin, ptr}; use pin_init::*; #[cfg(feature = "std")] use std::sync::Arc; @@ -50,8 +44,8 @@ impl RingBuffer { // SAFETY: The elements of the array can be uninitialized. buffer <- unsafe { init_from_closure(|_| Ok::<_, Infallible>(())) }, // SAFETY: `this` is a valid pointer. - head: unsafe { addr_of_mut!((*this.as_ptr()).buffer).cast::() }, - tail: unsafe { addr_of_mut!((*this.as_ptr()).buffer).cast::() }, + head: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::() }, + tail: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::() }, _pin: PhantomPinned, }) } @@ -112,7 +106,7 @@ impl RingBuffer { unsafe fn advance(&mut self, ptr: *mut T) -> *mut T { // SAFETY: ptr's offset from buffer is < SIZE let ptr = unsafe { ptr.add(1) }; - let origin: *mut _ = addr_of_mut!(self.buffer); + let origin: *mut _ = &raw mut (self.buffer); let origin = origin.cast::(); let offset = unsafe { ptr.offset_from(origin) }; if offset >= SIZE as isize { diff --git a/tests/zeroing.rs b/tests/zeroing.rs index e823dfc..563a051 100644 --- a/tests/zeroing.rs +++ b/tests/zeroing.rs @@ -1,6 +1,7 @@ +#![feature(raw_ref_op)] #![cfg_attr(not(RUSTC_LINT_REASONS_IS_STABLE), feature(lint_reasons))] -use core::{marker::PhantomPinned, ptr::addr_of_mut}; +use std::marker::PhantomPinned; use pin_init::*; @@ -22,7 +23,7 @@ impl Foo { marks: { let ptr = this.as_ptr(); // SAFETY: project from the NonNull to the buf field - let ptr = unsafe { addr_of_mut!((*ptr).buf) }.cast::(); + let ptr = unsafe { &raw mut (*ptr).buf }.cast::(); [ptr; MARKS]}, ..Zeroable::zeroed() })