Skip to content

refactor to use &raw mut #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<bindings::foo>();

// Initialize the `foo`
Expand Down
2 changes: 2 additions & 0 deletions examples/big_struct_in_place.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

#![feature(raw_ref_op)]

use pin_init::*;

// Struct with size over 1GiB
Expand Down
1 change: 1 addition & 0 deletions examples/pthread_mutex.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: Apache-2.0 OR MIT

// inspired by <https://github.com/nbdd0121/pin-init/blob/trunk/examples/pthread_mutex.rs>
#![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))]
Expand Down
7 changes: 3 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
28 changes: 14 additions & 14 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -1215,15 +1215,15 @@ 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.
// We use `paste!` to create new hygiene for `$field`.
$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):
Expand All @@ -1246,15 +1246,15 @@ 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.
// We use `paste!` to create new hygiene for `$field`.
$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():
Expand All @@ -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:
//
Expand All @@ -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)?):
Expand Down
14 changes: 4 additions & 10 deletions tests/ring_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -50,8 +44,8 @@ impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
// 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::<T>() },
tail: unsafe { addr_of_mut!((*this.as_ptr()).buffer).cast::<T>() },
head: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::<T>() },
tail: unsafe { (&raw mut (*this.as_ptr()).buffer).cast::<T>() },
_pin: PhantomPinned,
})
}
Expand Down Expand Up @@ -112,7 +106,7 @@ impl<T, const SIZE: usize> RingBuffer<T, SIZE> {
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::<T>();
let offset = unsafe { ptr.offset_from(origin) };
if offset >= SIZE as isize {
Expand Down
5 changes: 3 additions & 2 deletions tests/zeroing.rs
Original file line number Diff line number Diff line change
@@ -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::*;

Expand All @@ -22,7 +23,7 @@ impl Foo {
marks: {
let ptr = this.as_ptr();
// SAFETY: project from the NonNull<Foo> to the buf field
let ptr = unsafe { addr_of_mut!((*ptr).buf) }.cast::<u8>();
let ptr = unsafe { &raw mut (*ptr).buf }.cast::<u8>();
[ptr; MARKS]},
..Zeroable::zeroed()
})
Expand Down
Loading