Skip to content

Commit 3c67eed

Browse files
committedSep 4, 2024
rust: avoid box_uninit_write feature
Like commit 0903b9e ("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. Link: rust-lang/rust#63291 [1] Link: rust-lang/rust#129401 [2] Link: rust-lang/rust#129397 [3] Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 68d3b6a commit 3c67eed

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed
 

‎rust/kernel/alloc/box_ext.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ pub trait BoxExt<T>: Sized {
2626
/// use kernel::alloc::{flags, box_ext::BoxExt};
2727
/// let value = Box::new([0; 32], flags::GFP_KERNEL)?;
2828
/// assert_eq!(*value, [0; 32]);
29-
/// let value = Box::drop_contents(value);
29+
/// let mut value = Box::drop_contents(value);
3030
/// // Now we can re-use `value`:
31-
/// let value = Box::write(value, [1; 32]);
31+
/// value.write([1; 32]);
32+
/// // SAFETY: We just wrote to it.
33+
/// let value = unsafe { value.assume_init() };
3234
/// assert_eq!(*value, [1; 32]);
3335
/// # Ok::<(), Error>(())
3436
/// ```

‎rust/kernel/rbtree.rs

+8-9
Original file line numberDiff line numberDiff line change
@@ -1059,15 +1059,14 @@ impl<K, V> RBTreeNodeReservation<K, V> {
10591059
/// Initialises a node reservation.
10601060
///
10611061
/// It then becomes an [`RBTreeNode`] that can be inserted into a tree.
1062-
pub fn into_node(self, key: K, value: V) -> RBTreeNode<K, V> {
1063-
let node = Box::write(
1064-
self.node,
1065-
Node {
1066-
key,
1067-
value,
1068-
links: bindings::rb_node::default(),
1069-
},
1070-
);
1062+
pub fn into_node(mut self, key: K, value: V) -> RBTreeNode<K, V> {
1063+
self.node.write(Node {
1064+
key,
1065+
value,
1066+
links: bindings::rb_node::default(),
1067+
});
1068+
// SAFETY: We just wrote to it.
1069+
let node = unsafe { self.node.assume_init() };
10711070
RBTreeNode { node }
10721071
}
10731072
}

0 commit comments

Comments
 (0)
Please sign in to comment.