Skip to content

Commit bbf992b

Browse files
authored
Merge pull request #105 from antonio-hickey/alt-unstable-feauture
replace `addr_of_mut!` with `&raw mut`
2 parents d316316 + e277630 commit bbf992b

File tree

15 files changed

+30
-22
lines changed

15 files changed

+30
-22
lines changed

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`

build.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use rustc_version::{version_meta, Channel, Version};
33
fn main() {
44
println!("cargo::rustc-check-cfg=cfg(USE_RUSTC_FEATURES)");
55
println!("cargo::rustc-check-cfg=cfg(CONFIG_RUSTC_HAS_UNSAFE_PINNED)");
6+
println!("cargo:rustc-check-cfg=cfg(RUSTC_RAW_REF_OP_IS_STABLE)");
67

78
let meta = version_meta().unwrap();
89

@@ -17,4 +18,7 @@ fn main() {
1718
if meta.semver >= Version::parse("1.89.0-nightly").unwrap() && use_feature {
1819
println!("cargo:rustc-cfg=CONFIG_RUSTC_HAS_UNSAFE_PINNED");
1920
}
21+
if meta.semver >= Version::parse("1.82.0").unwrap() && use_feature {
22+
println!("cargo:rustc-cfg=RUSTC_RAW_REF_OP_IS_STABLE");
23+
}
2024
}

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

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,

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::{

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 {

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::{

internal/src/init.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ fn init_fields(
290290
{
291291
#value_prep
292292
// SAFETY: TODO
293-
unsafe { #write(::core::ptr::addr_of_mut!((*#slot).#ident), #value_ident) };
293+
unsafe { #write(&raw mut (*#slot).#ident, #value_ident) };
294294
}
295295
#accessor
296296
}
@@ -307,7 +307,7 @@ fn init_fields(
307307
// return when an error/panic occurs.
308308
// - We also use `#data` to require the correct trait (`Init` or `PinInit`)
309309
// for `#ident`.
310-
unsafe { #data.#ident(::core::ptr::addr_of_mut!((*#slot).#ident), #init)? };
310+
unsafe { #data.#ident(&raw mut (*#slot).#ident, #init)? };
311311
},
312312
quote! {
313313
// SAFETY: TODO
@@ -322,7 +322,7 @@ fn init_fields(
322322
unsafe {
323323
::pin_init::Init::__init(
324324
#init,
325-
::core::ptr::addr_of_mut!((*#slot).#ident),
325+
&raw mut (*#slot).#ident,
326326
)?
327327
};
328328
},
@@ -367,7 +367,7 @@ fn init_fields(
367367
// SAFETY: We forget the guard later when initialization has succeeded.
368368
let #guard = unsafe {
369369
::pin_init::__internal::DropGuard::new(
370-
::core::ptr::addr_of_mut!((*slot).#ident)
370+
&raw mut (*slot).#ident
371371
)
372372
};
373373
});

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 {

tests/cfgs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![cfg_attr(USE_RUSTC_FEATURES, feature(lint_reasons))]
2+
#![cfg_attr(USE_RUSTC_FEATURES, feature(raw_ref_op))]
23

34
use pin_init::{pin_data, pin_init, PinInit};
45

0 commit comments

Comments
 (0)