Re: [PATCH v2 07/12] rust: init: Add functions to create array initializers

From: Martin Rodriguez Reboredo
Date: Thu Jul 20 2023 - 20:26:21 EST


On 7/19/23 11:20, Benno Lossin wrote:
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@xxxxxxxxxxxxx>
Reviewed-by: Björn Roy Baron <bjorn3_gh@xxxxxxxxxxxxxx>
Signed-off-by: Benno Lossin <benno.lossin@xxxxxxxxx>
---
[...]
+/// Initializes an array by initializing each element via the provided initializer.
+///
+/// # Examples
+///
+/// ```rust
+/// use kernel::{error::Error, init::init_array_from_fn};
+/// let array: Box<[usize; 1_000_000_000]>= Box::init::<Error>(init_array_from_fn(|i| i)).unwrap();
+/// pr_info!("{array:?}");
+/// ```

Rather than debug printing the array I'd suggest to assert the struct
size or its elements instead.

[...]
+
+/// Initializes an array by initializing each element via the provided initializer.
+///
+/// # Examples
+///
+/// ```rust
+/// use kernel::{sync::{Arc, Mutex}, init::pin_init_array_from_fn, new_mutex};
+/// let array: Arc<[Mutex<usize>; 1_000_000_000]>=
+/// Arc::pin_init(pin_init_array_from_fn(|i| new_mutex!(i))).unwrap();
+/// pr_info!("{}", array.len());
+/// ```
[...]
Same as above.