mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Christian Schrefl <chrisi.schrefl@gmail.com>
To: "Benno Lossin" <benno.lossin@proton.me>,
	"Miguel Ojeda" <ojeda@kernel.org>,
	"Alex Gaynor" <alex.gaynor@gmail.com>,
	"Boqun Feng" <boqun.feng@gmail.com>,
	"Gary Guo" <gary@garyguo.net>,
	"Björn Roy Baron" <bjorn3_gh@protonmail.com>,
	"Andreas Hindborg" <a.hindborg@kernel.org>,
	"Alice Ryhl" <aliceryhl@google.com>,
	"Trevor Gross" <tmgross@umich.edu>,
	"Danilo Krummrich" <dakr@kernel.org>,
	"Fiona Behrens" <me@kloenk.dev>
Cc: rust-for-linux@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/8] rust: pin-init: add `cast_[pin_]init` functions to change the initialized type
Date: Tue, 22 Apr 2025 08:56:11 +0200	[thread overview]
Message-ID: <61dc2ae0-c15a-4243-bdb1-de06ba2e0000@gmail.com> (raw)
In-Reply-To: <20250421221728.528089-2-benno.lossin@proton.me>

On 22.04.25 12:17 AM, Benno Lossin wrote:
> These functions cast the given pointer from one type to another. They
> are particularly useful when initializing transparent wrapper types.
> 
> Link: https://github.com/Rust-for-Linux/pin-init/pull/39/commits/80c03ddee41b154f1099fd8cc7c2bbd8c80af0ad
> Signed-off-by: Benno Lossin <benno.lossin@proton.me>
> ---

Reviewed-by: Christian Schrefl <chrisi.schrefl@gmail.com>

>  rust/pin-init/src/lib.rs | 32 ++++++++++++++++++++++++++++++++
>  1 file changed, 32 insertions(+)
> 
> diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
> index 0806c689f693..a880c21d3f09 100644
> --- a/rust/pin-init/src/lib.rs
> +++ b/rust/pin-init/src/lib.rs
> @@ -1216,6 +1216,38 @@ unsafe fn __pinned_init(self, slot: *mut T) -> Result<(), E> {
>      __internal::InitClosure(f, PhantomData)
>  }
>  
> +/// Changes the to be initialized type.
> +///
> +/// # Safety
> +///
> +/// - `*mut U` must be castable to `*mut T` and any value of type `T` written through such a
> +///   pointer must result in a valid `U`.
> +#[expect(clippy::let_and_return)]
> +pub const unsafe fn cast_pin_init<T, U, E>(init: impl PinInit<T, E>) -> impl PinInit<U, E> {
> +    // SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
> +    // requirements.
> +    let res = unsafe { pin_init_from_closure(|ptr: *mut U| init.__pinned_init(ptr.cast::<T>())) };
> +    // FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a
> +    // cycle when computing the type returned by this function)
> +    res
> +}
> +
> +/// Changes the to be initialized type.
> +///
> +/// # Safety
> +///
> +/// - `*mut U` must be castable to `*mut T` and any value of type `T` written through such a
> +///   pointer must result in a valid `U`.
> +#[expect(clippy::let_and_return)]
> +pub const unsafe fn cast_init<T, U, E>(init: impl Init<T, E>) -> impl Init<U, E> {
> +    // SAFETY: initialization delegated to a valid initializer. Cast is valid by function safety
> +    // requirements.
> +    let res = unsafe { init_from_closure(|ptr: *mut U| init.__init(ptr.cast::<T>())) };
> +    // FIXME: remove the let statement once the nightly-MSRV allows it (1.78 otherwise encounters a
> +    // cycle when computing the type returned by this function)
> +    res
> +}
> +>  /// An initializer that leaves the memory uninitialized.
>  ///
>  /// The initializer is a no-op. The `slot` memory is not changed.


  reply	other threads:[~2025-04-22  6:56 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20250421221728.528089-1-benno.lossin@proton.me>
2025-04-21 22:17 ` Benno Lossin
2025-04-22  6:56   ` Christian Schrefl [this message]
2025-04-21 22:17 ` [PATCH 2/8] rust: pin-init: Add the `Wrapper` trait Benno Lossin
2025-04-22  4:44   ` Boqun Feng
2025-04-21 22:18 ` [PATCH 3/8] rust: pin-init: Implement `Wrapper` for `UnsafePinned` behind feature flag Benno Lossin
2025-04-22  9:42   ` Christian Schrefl
2025-04-22 11:21     ` Benno Lossin
2025-04-22 14:17       ` Christian Schrefl
2025-04-21 22:18 ` [PATCH 4/8] rust: pin-init: Update Changelog and Readme Benno Lossin
2025-04-21 22:18 ` [PATCH 5/8] rust: pin-init: Update the structural pinning link in readme Benno Lossin
2025-04-21 22:18 ` [PATCH 6/8] rust: pin-init: allow `pub` fields in `derive(Zeroable)` Benno Lossin
2025-04-22  4:55   ` Boqun Feng
2025-04-22  8:30     ` Benno Lossin
2025-04-22 14:14       ` Boqun Feng
2025-04-22 14:45         ` Benno Lossin
2025-04-22 21:11           ` Boqun Feng
2025-04-22 21:56             ` Benno Lossin
2025-04-21 22:18 ` [PATCH 7/8] rust: pin-init: allow `Zeroable` derive macro to also be applied to unions Benno Lossin
2025-04-21 22:18 ` [PATCH 8/8] rust: pin-init: add `MaybeZeroable` derive macro Benno Lossin
2025-04-22  4:54   ` Boqun Feng
2025-04-22  7:56     ` Benno Lossin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=61dc2ae0-c15a-4243-bdb1-de06ba2e0000@gmail.com \
    --to=chrisi.schrefl@gmail.com \
    --cc=a.hindborg@kernel.org \
    --cc=alex.gaynor@gmail.com \
    --cc=aliceryhl@google.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dakr@kernel.org \
    --cc=gary@garyguo.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=me@kloenk.dev \
    --cc=ojeda@kernel.org \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=tmgross@umich.edu \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®