Rust for Linux List
 help / color / mirror / Atom feed
* [PATCH] rust: Eschew `Box<MaybeUninit<T>>::write`
@ 2024-08-23  5:03 Jubilee Young
  2024-08-23  5:44 ` Trevor Gross
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Jubilee Young @ 2024-08-23  5:03 UTC (permalink / raw)
  To: Miguel Ojeda, Alex Gaynor, Wedson Almeida Filho, Boqun Feng,
	Gary Guo, Björn Roy Baron, Benno Lossin, Andreas Hindborg,
	Alice Ryhl
  Cc: rust-for-linux, Jubilee Young

T-libs-api has consensus for stabilizing some of `feature(new_uninit)`,
but not for `Box<MaybeUninit<T>>::write`. Instead, we can use
`MaybeUninit<T>::write`, so Rust for Linux can drop the feature after
stabilization. That will happen after merging, as the FCP has completed:
https://github.com/rust-lang/rust/issues/63291#issuecomment-2183022955

This is required before stabilization because remaining-unstable API
will be divided into new features. This code doesn't know about those
yet. It can't: they haven't landed, as the relevant PR is blocked on
rustc's CI testing Rust-for-Linux without this patch.
 
Signed-off-by: Jubilee Young <workingjubilee@gmail.com>
---
 rust/kernel/alloc/box_ext.rs | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/rust/kernel/alloc/box_ext.rs b/rust/kernel/alloc/box_ext.rs
index b68ade26a42d..6179d893ff3a 100644
--- a/rust/kernel/alloc/box_ext.rs
+++ b/rust/kernel/alloc/box_ext.rs
@@ -37,8 +37,10 @@ pub trait BoxExt<T>: Sized {
 
 impl<T> BoxExt<T> for Box<T> {
     fn new(x: T, flags: Flags) -> Result<Self, AllocError> {
-        let b = <Self as BoxExt<_>>::new_uninit(flags)?;
-        Ok(Box::write(b, x))
+        let mut b = <Self as BoxExt<_>>::new_uninit(flags)?;
+        b.write(x);
+        // SAFETY: We just wrote to it.
+        Ok(unsafe { b.assume_init() })
     }
 
     #[cfg(any(test, testlib))]
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-08-27  7:34 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-23  5:03 [PATCH] rust: Eschew `Box<MaybeUninit<T>>::write` Jubilee Young
2024-08-23  5:44 ` Trevor Gross
2024-08-23  6:11   ` Miguel Ojeda
2024-08-23  6:24     ` Trevor Gross
2024-08-23 11:23       ` Miguel Ojeda
2024-08-23 11:10 ` Alice Ryhl
2024-08-23 11:23   ` Miguel Ojeda
2024-08-27  7:34 ` Miguel Ojeda

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox