From 3ff5909e9f3b611329fdf5904116d867795a752f Mon Sep 17 00:00:00 2001 From: Abhinav Date: Thu, 25 Jun 2026 17:38:15 +0530 Subject: [PATCH] feat: add initialized_unfilled to ReadBufCursor --- src/rt/io.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/rt/io.rs b/src/rt/io.rs index 21c8ed9c7c..447774c6c0 100644 --- a/src/rt/io.rs +++ b/src/rt/io.rs @@ -382,6 +382,46 @@ impl ReadBufCursor<'_> { } self.buf.filled = end; } + + /// Returns a mutable reference to the unfilled part of the buffer, ensuring it is fully initialized. + #[inline] + pub fn initialize_unfilled(&mut self) -> &mut [u8] { + self.initialize_unfilled_to(self.remaining()) + } + + /// Returns a mutable reference to the first `n` bytes of the unfilled part of the buffer, ensuring it is + /// fully initialized. + /// + /// # Panics + /// + /// Panics if `self.remaining()` is less than `n`. + #[inline] + pub fn initialize_unfilled_to(&mut self, n: usize) -> &mut [u8] { + assert!(self.remaining() >= n, "n overflows remaining"); + + // This can't overflow as the assert above would have panicked otherwise. + let end = self.buf.filled + n; + + if self.buf.init < end { + // SAFETY: + // 1. The raw pointer is not null and not dangling either as the slice outlives the + // pointer's usage. The pointer is also properly aligned. + // 2. Further, `end - self.buf.init` is inbounds and hence safe to write + // to. + unsafe { + self.buf.raw[self.buf.init..end] + .as_mut_ptr() + .write_bytes(0, end - self.buf.init); + } + self.buf.init = end; + } + + let slice = &mut self.buf.raw[self.buf.filled..end]; + + // SAFETY: `MaybeUninit` has the same memory layout as u8 and we properly initialized + // the slice above. + unsafe { &mut *(slice as *mut [MaybeUninit] as *mut [u8]) } + } } macro_rules! deref_async_read {