From b0d971e65206a75735c4c302490e8d3bb5e03728 Mon Sep 17 00:00:00 2001 From: Josh Triplett Date: Wed, 12 Mar 2025 00:30:38 -0700 Subject: [PATCH] DRAFT: Support `Buffer` on `&mut MaybeUninit` This returns an output of `Option<&mut T>`: `Some` for `.assume_init(1)`, and `None` for `.assume_init(0)`. This is a draft because it's not clear yet whether `Option` is the best possible return value. For syscalls that will *always* initialize the buffer if they return successfully, this isn't ideal, because it requires handling an `Ok(None)` case that'll never happen. However, for syscalls that *can* return zero output successfully, like `read`, we'd need some way to handle that case. --- src/buffer.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/buffer.rs b/src/buffer.rs index 1ab0ee1ae..a8b307d4e 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -154,6 +154,25 @@ impl private::Sealed for &mut Vec { } } +impl<'a, T> private::Sealed for &'a mut MaybeUninit { + type Output = Option<&'a mut T>; + + #[inline] + fn parts_mut(&mut self) -> (*mut T, usize) { + (self.as_mut_ptr(), 1) + } + + #[inline] + unsafe fn assume_init(self, len: usize) -> Self::Output { + if len == 0 { + None + } else { + // SAFETY: The user asserts that the object is now initialized. + Some(MaybeUninit::assume_init_mut(self)) + } + } +} + impl<'a, T> private::Sealed for &'a mut [MaybeUninit] { type Output = (&'a mut [T], &'a mut [MaybeUninit]);