Re: [PATCH v3 08/13] rust: init: add `stack_pin_init!` macro

From: Alice Ryhl
Date: Thu Mar 30 2023 - 11:08:03 EST


On 3/30/23 13:06, Gary Guo wrote:
+impl<T> StackInit<T> {
+ /// Creates a new [`StackInit<T>`] that is uninitialized. Use [`stack_pin_init`] instead of this
+ /// primitive.
+ ///
+ /// [`stack_pin_init`]: kernel::stack_pin_init
+ #[inline]
+ pub fn uninit() -> Self {
+ Self(MaybeUninit::uninit(), false)
+ }
+
+ /// Initializes the contents and returns the result.
+ ///
+ /// # Safety
+ ///
+ /// The caller ensures that `self` is on the stack and not accessible in any other way, if this
+ /// function returns `Ok`.
+ #[inline]
+ pub unsafe fn init<E>(&mut self, init: impl PinInit<T, E>) -> Result<Pin<&mut T>, E> {

Could this be made safe if the signature takes `self: Pin<&mut Self>`
instead?

The std `pin!` macro is stable in
1.68 so we can just `core::pin::pin!(StackInit::uninit())` and then
call `init` on it.

Best,
Gary

Yeah, I think that would work. If it's marked safe, then it will be possible to call `init` several times, but this is fine if `init` transitions the `StackInit` back into its uninitialized state before attempting to initialize it again.