Skip to content

Commit 31d95c2

Browse files
asahilinaojeda
authored andcommitted
rust: sync: arc: Add UniqueArc<MaybeUninit<T>::assume_init()
We can already create `UniqueArc<MaybeUninit<T>>` instances with `UniqueArc::try_new_uninit()` and write to them with `write()`. Add the missing unsafe `assume_init()` function to promote it to `UniqueArc<T>`, so users can do piece-wise initialization of the contents instead of doing it all at once as long as they keep the invariants (the same requirements as `MaybeUninit::assume_init()`). This mirrors the std `Arc::assume_init()` function. In the kernel, since we have `UniqueArc`, arguably this only belongs there since most use cases will initialize it immediately after creating it, before demoting it to `Arc` to share it. [ Miguel: The "Rust pin-init API for pinned initialization of structs" patch series [1] from Benno Lossin contains a very similar patch: rust: sync: add `assume_init` to `UniqueArc` Adds the `assume_init` function to `UniqueArc<MaybeUninit<T>>` that unsafely assumes the value to be initialized and yields a value of type `UniqueArc<T>`. This function is used when manually initializing the pointee of an `UniqueArc`. To make that patch a noop and thus drop it, I adjusted the `SAFETY` comment here to be the same as in the current latest version of that series (v7). I have also brought the `Reviewed-by`s there into here, and reworded the `Co-authored-by` into `Co-developed-by`. ] Link: https://lore.kernel.org/r/[email protected] [1] Co-developed-by: Benno Lossin <[email protected]> Signed-off-by: Benno Lossin <[email protected]> Signed-off-by: Asahi Lina <[email protected]> Reviewed-by: Gary Guo <[email protected]> Reviewed-by: Wedson Almeida Filho <[email protected]> Reviewed-by: Andreas Hindborg <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent 1edd033 commit 31d95c2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

rust/kernel/sync/arc.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,17 @@ impl<T> UniqueArc<MaybeUninit<T>> {
492492
/// Converts a `UniqueArc<MaybeUninit<T>>` into a `UniqueArc<T>` by writing a value into it.
493493
pub fn write(mut self, value: T) -> UniqueArc<T> {
494494
self.deref_mut().write(value);
495+
// SAFETY: We just wrote the value to be initialized.
496+
unsafe { self.assume_init() }
497+
}
498+
499+
/// Unsafely assume that `self` is initialized.
500+
///
501+
/// # Safety
502+
///
503+
/// The caller guarantees that the value behind this pointer has been initialized. It is
504+
/// *immediate* UB to call this when the value is not initialized.
505+
pub unsafe fn assume_init(self) -> UniqueArc<T> {
495506
let inner = ManuallyDrop::new(self).inner.ptr;
496507
UniqueArc {
497508
// SAFETY: The new `Arc` is taking over `ptr` from `self.inner` (which won't be

0 commit comments

Comments
 (0)