All of lore.kernel.org
 help / color / mirror / Atom feed
From: Alice Ryhl <aliceryhl@google.com>
To: benno.lossin@proton.me
Cc: alex.gaynor@gmail.com, aliceryhl@google.com,
	bjorn3_gh@protonmail.com,  boqun.feng@gmail.com,
	gary@garyguo.net, lina@asahilina.net,
	 linux-kernel@vger.kernel.org, nmi@metaspace.dk,
	ojeda@kernel.org,  patches@lists.linux.dev,
	rust-for-linux@vger.kernel.org, wedsonaf@gmail.com
Subject: [PATCH 6/7] rust: init: Add functions to create array initializers
Date: Mon,  3 Jul 2023 12:03:31 +0000	[thread overview]
Message-ID: <20230703120331.2384427-1-aliceryhl@google.com> (raw)
In-Reply-To: <20230624092330.157338-6-benno.lossin@proton.me>

Benno Lossin <benno.lossin@proton.me> writes:
> Add two functions `pin_init_array_from_fn` and `init_array_from_fn` that
> take a function that generates initializers for `T` from usize, the added
> functions then return an initializer for `[T; N]` where every element is
> initialized by an element returned from the generator function.
> 
> Suggested-by: Asahi Lina <lina@asahilina.net>
> Signed-off-by: Benno Lossin <benno.lossin@proton.me>
> ---
> +/// Initializes an array by initializing each element via the provided initializer.
> +///
> +/// # Examples
> +///
> +/// ```rust
> +/// let array: Box<[usize; 1000_000_000]>= Box::init(init_array_from_fn(|i| i)).unwrap();
> +/// println!("{array:?}");
> +/// ```
> +pub fn init_array_from_fn<I, const N: usize, T, E>(
> +    mut make_init: impl FnMut(usize) -> I,
> +) -> impl Init<[T; N], E>
> +where
> +    I: Init<T, E>,
> +{
> +    let init = move |slot: *mut [T; N]| {
> +        let slot = slot.cast::<T>();
> +        for i in 0..N {
> +            let init = make_init(i);
> +            // SAFETY: since 0 <= `i` < N, it is still in bounds of `[T; N]`.
> +            let ptr = unsafe { slot.add(i) };
> +            // SAFETY: The pointer is derived from `slot` and thus satisfies the `__init`
> +            // requirements.
> +            match unsafe { init.__init(ptr) } {
> +                Ok(()) => {}
> +                Err(e) => {
> +                    // We now free every element that has been initialized before:
> +                    for j in 0..i {
> +                        let ptr = unsafe { slot.add(j) };
> +                        // SAFETY: The value was initialized in a previous iteration of the loop
> +                        // and since we return `Err` below, the caller will consider the memory at
> +                        // `slot` as uninitialized.
> +                        unsafe { ptr::drop_in_place(ptr) };
> +                    }

The loop can be simplified like this:

ptr::drop_in_place(ptr::slice_from_raw_parts(slot, i));

Yes, this actually works and will run the destructor of each value.

Alice


  parent reply	other threads:[~2023-07-03 12:03 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-24  9:24 [PATCH 1/7] rust: init: consolidate init macros Benno Lossin
2023-06-24  9:25 ` [PATCH 2/7] rust: add derive macro for `Zeroable` Benno Lossin
2023-06-24 14:55   ` Björn Roy Baron
2023-06-25 20:46   ` Gary Guo
2023-07-03 11:50   ` Alice Ryhl
2023-06-24  9:25 ` [PATCH 3/7] rust: init: make guards in the init macros hygienic Benno Lossin
2023-06-24 14:58   ` Björn Roy Baron
2023-06-25 20:54   ` Gary Guo
2023-06-28 11:41     ` Benno Lossin
2023-06-28 16:48       ` Gary Guo
2023-06-24  9:25 ` [PATCH 4/7] rust: init: wrap type checking struct initializers in a closure Benno Lossin
2023-06-24 15:03   ` Björn Roy Baron
2023-06-24 21:05     ` Benno Lossin
2023-06-24  9:25 ` [PATCH 5/7] rust: init: add `..Zeroable::zeroed()` syntax for zeroing all missing fields Benno Lossin
2023-06-24 15:11   ` Björn Roy Baron
2023-06-24 21:14     ` Benno Lossin
2023-06-25 12:56       ` Björn Roy Baron
2023-06-25 13:07         ` Benno Lossin
2023-06-25 14:17           ` Björn Roy Baron
2023-06-25 16:46             ` Benno Lossin
2023-07-03 11:58   ` Alice Ryhl
2023-07-03 18:15   ` Boqun Feng
2023-07-05 17:48     ` Gary Guo
2023-07-05 21:44       ` Benno Lossin
2023-06-24  9:25 ` [PATCH 6/7] rust: init: Add functions to create array initializers Benno Lossin
2023-06-24 15:17   ` Björn Roy Baron
2023-07-03 12:03   ` Alice Ryhl [this message]
2023-06-24  9:25 ` [PATCH 7/7] rust: init: add support for arbitrary paths in init macros Benno Lossin
2023-06-24 15:20   ` Björn Roy Baron
2023-06-25 21:01   ` Gary Guo
2023-06-28 11:26     ` Benno Lossin
2023-06-28 17:13       ` Gary Guo
2023-06-24 14:49 ` [PATCH 1/7] rust: init: consolidate " Björn Roy Baron

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=20230703120331.2384427-1-aliceryhl@google.com \
    --to=aliceryhl@google.com \
    --cc=alex.gaynor@gmail.com \
    --cc=benno.lossin@proton.me \
    --cc=bjorn3_gh@protonmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=gary@garyguo.net \
    --cc=lina@asahilina.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nmi@metaspace.dk \
    --cc=ojeda@kernel.org \
    --cc=patches@lists.linux.dev \
    --cc=rust-for-linux@vger.kernel.org \
    --cc=wedsonaf@gmail.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.