Re: [PATCH 16/22] rust: pin-init: add `std` and `alloc` support from the user-space version
From: Andreas Hindborg
Date: Wed Mar 05 2025 - 07:23:15 EST
"Benno Lossin" <benno.lossin@xxxxxxxxx> writes:
> To synchronize the kernel's version of pin-init with the user-space
> version, introduce support for `std` and `alloc`. While the kernel uses
> neither, the user-space version has to support both. Thus include the
> required `#[cfg]`s and additional code.
>
> Signed-off-by: Benno Lossin <benno.lossin@xxxxxxxxx>
> ---
> rust/pin-init/src/__internal.rs | 27 ++++++
> rust/pin-init/src/alloc.rs | 158 ++++++++++++++++++++++++++++++++
> rust/pin-init/src/lib.rs | 17 ++--
> 3 files changed, 196 insertions(+), 6 deletions(-)
> create mode 100644 rust/pin-init/src/alloc.rs
>
> diff --git a/rust/pin-init/src/__internal.rs b/rust/pin-init/src/__internal.rs
> index 74086365a18a..27d4a8619c04 100644
> --- a/rust/pin-init/src/__internal.rs
> +++ b/rust/pin-init/src/__internal.rs
> @@ -186,6 +186,33 @@ pub fn init<E>(self: Pin<&mut Self>, init: impl PinInit<T, E>) -> Result<Pin<&mu
> }
> }
>
> +#[test]
I think the kunit support we have in the pipeline will pick this up?
> +fn stack_init_reuse() {
> + use ::std::{borrow::ToOwned, println, string::String};
> + use core::pin::pin;
> +
> + #[derive(Debug)]
> + struct Foo {
> + a: usize,
> + b: String,
> + }
> + let mut slot: Pin<&mut StackInit<Foo>> = pin!(StackInit::uninit());
> + let value: Result<Pin<&mut Foo>, core::convert::Infallible> =
> + slot.as_mut().init(crate::init!(Foo {
> + a: 42,
> + b: "Hello".to_owned(),
> + }));
> + let value = value.unwrap();
> + println!("{value:?}");
> + let value: Result<Pin<&mut Foo>, core::convert::Infallible> =
> + slot.as_mut().init(crate::init!(Foo {
> + a: 24,
> + b: "world!".to_owned(),
> + }));
> + let value = value.unwrap();
> + println!("{value:?}");
> +}
> +
[...]
> diff --git a/rust/pin-init/src/lib.rs b/rust/pin-init/src/lib.rs
> index 55d8953620f0..1fdca35906a0 100644
> --- a/rust/pin-init/src/lib.rs
> +++ b/rust/pin-init/src/lib.rs
> @@ -204,8 +204,8 @@
> //! [structurally pinned fields]:
> //! https://doc.rust-lang.org/std/pin/index.html#pinning-is-structural-for-field
> //! [stack]: crate::stack_pin_init
> -//! [`Arc<T>`]: ../kernel/sync/struct.Arc.html
> -//! [`Box<T>`]: ../kernel/alloc/struct.KBox.html
> +//! [`Arc<T>`]: https://doc.rust-lang.org/stable/alloc/sync/struct.Arc.html
> +//! [`Box<T>`]: https://doc.rust-lang.org/stable/alloc/boxed/struct.Box.html
Now these will render incorrect in the kernel docs, right?
> //! [`impl PinInit<Foo>`]: PinInit
> //! [`impl PinInit<T, E>`]: PinInit
> //! [`impl Init<T, E>`]: Init
> @@ -239,6 +239,11 @@
> #[doc(hidden)]
> pub mod macros;
>
> +#[cfg(any(feature = "std", feature = "alloc"))]
> +mod alloc;
> +#[cfg(any(feature = "std", feature = "alloc"))]
> +pub use alloc::InPlaceInit;
Do we really need to have this entire file sitting dead in the kernel
tree? If you are not building the user space version from the kernel
sources, I don't think we need it here. Even when you want to sync
between the two repositories, it should be easy to handle an entire file
being not present on one side.
Best regards,
Andreas Hindborg