rust: avoid `box_uninit_write` feature
Subsystems (2)
Miguel Ojeda
maintainerPATCH 1/1 rust: avoid `box_uninit_write` feature
Like commit 0903b9e2a46c ("rust: alloc: eschew Box<MaybeUninit<T>>::write"), but for the new rbtree and alloc code.
That is, feature(new_uninit) [1] got partially stabilized [2] for Rust 1.82.0 (expected to be released on 2024-10-17), but it did not include Box<MaybeUninit<T>>::write, which got split into feature(box_uninit_write) [3].
To avoid relying on a new unstable feature, rewrite the write + assume_init pair manually.
--- rust/kernel/alloc/box_ext.rs | 6 ++++-- rust/kernel/rbtree.rs | 17 ++++++++--------- 2 files changed, 12 insertions(+), 11 deletions(-)
rust/kernel/alloc/box_ext.rs +4 -2
@@ -26,9 +26,11 @@ pub trait BoxExt<T>: Sized {/// use kernel::alloc::{flags, box_ext::BoxExt}; /// let value = Box::new([0; 32], flags::GFP_KERNEL)?; /// assert_eq!(*value, [0; 32]); -/// let value = Box::drop_contents(value); +/// let mut value = Box::drop_contents(value); /// // Now we can re-use `value`: -/// let value = Box::write(value, [1; 32]); +/// value.write([1; 32]); +/// // SAFETY: We just wrote to it. +/// let value = unsafe { value.assume_init() }; /// assert_eq!(*value, [1; 32]); /// # Ok::<(), Error>(()) /// ```
rust/kernel/rbtree.rs +8 -9
@@ -1059,15 +1059,14 @@ impl<K, V> RBTreeNodeReservation<K, V> {/// Initialises a node reservation. /// /// It then becomes an [`RBTreeNode`] that can be inserted into a tree. -pub fn into_node (self, key:K , value:V ) ->RBTreeNode <K ,V > { -let node =Box ::write ( - self.node, -Node { - key, - value, - links: bindings::rb_node::default (), - }, - ); +pub fn into_node (mut self, key:K , value:V ) ->RBTreeNode <K ,V > { + self.node.write (Node { + key, + value, + links: bindings::rb_node::default (), + }); +// SAFETY: We just wrote to it. +let node =unsafe { self.node.assume_init () };RBTreeNode { node } } }
base-commit: 68d3b6aa08708bb3907c2c13eaf4b3ccf4805160
Signature · 1 line
2.46.0
Quoted 9 lines · Miguel Ojeda
Like commit
0903b9e2a46c("rust: alloc: eschewBox<MaybeUninit<T>>::write"), but for the newrbtreeandalloccode.That is,
feature(new_uninit)[1] got partially stabilized [2] for Rust 1.82.0 (expected to be released on 2024-10-17), but it did not includeBox<MaybeUninit<T>>::write, which got split intofeature(box_uninit_write)[3].
Quoted 7 lines
To avoid relying on a new unstable feature, rewrite the
write+assume_initpair manually.
Good catch!
Matt Gilbride
Reviewed-byQuoted 9 lines · Miguel Ojeda
Like commit
0903b9e2a46c("rust: alloc: eschewBox<MaybeUninit<T>>::write"), but for the newrbtreeandalloccode.That is,
feature(new_uninit)[1] got partially stabilized [2] for Rust 1.82.0 (expected to be released on 2024-10-17), but it did not includeBox<MaybeUninit<T>>::write, which got split intofeature(box_uninit_write)[3].
Quoted 7 lines
To avoid relying on a new unstable feature, rewrite the
write+assume_initpair manually.
Miguel Ojeda
AppliedQuoted 9 lines · Miguel Ojeda
Like commit
0903b9e2a46c("rust: alloc: eschewBox<MaybeUninit<T>>::write"), but for the newrbtreeandalloccode.That is,
feature(new_uninit)[1] got partially stabilized [2] for Rust 1.82.0 (expected to be released on 2024-10-17), but it did not includeBox<MaybeUninit<T>>::write, which got split intofeature(box_uninit_write)[3].
Quoted 7 lines
To avoid relying on a new unstable feature, rewrite the
write+assume_initpair manually.
Applied to rust-next -- thanks Alice and Matt for the quick reviews!
Cheers,
Miguel