core/io/borrowed_buf.rs
1#![unstable(feature = "core_io_borrowed_buf", issue = "117693")]
2
3use crate::fmt::{self, Debug, Formatter};
4use crate::mem::{self, MaybeUninit};
5use crate::ptr;
6
7/// A borrowed buffer of initially uninitialized elements, which is incrementally filled.
8///
9/// This type makes it safer to work with `MaybeUninit` buffers, such as to read into a buffer
10/// without having to initialize it first. It tracks the region of elements that have been filled
11/// and whether the unfilled region was initialized.
12///
13/// In summary, the contents of the buffer can be visualized as:
14/// ```not_rust
15/// [ capacity ]
16/// [ filled | unfilled (may be initialized) ]
17/// ```
18///
19/// A `BorrowedBuf` is created around some existing elements (or capacity for elements) via a unique
20/// reference (`&mut`). The `BorrowedBuf` can be configured (e.g., using `clear` or `set_init`), but
21/// cannot be directly written. To write into the buffer, use `unfilled` to create a
22/// `BorrowedCursor`. The cursor has write-only access to the unfilled portion of the buffer (you
23/// can think of it as a write-only iterator).
24///
25/// The lifetime `'data` is a bound on the lifetime of the underlying elements.
26///
27/// The type is most commonly used to manage bytes, but can manage any type of elements.
28pub struct BorrowedBuf<'data, T> {
29 /// The buffer's underlying elements.
30 buf: &'data mut [MaybeUninit<T>],
31 /// The number of elements of `self.buf` that are known to be filled.
32 filled: usize,
33 /// Whether the entire unfilled part of `self.buf` has explicitly been initialized.
34 init: bool,
35}
36
37impl<T> Debug for BorrowedBuf<'_, T> {
38 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
39 f.debug_struct("BorrowedBuf")
40 .field("init", &self.init)
41 .field("filled", &self.filled)
42 .field("capacity", &self.capacity())
43 .finish()
44 }
45}
46
47/// Creates a new `BorrowedBuf` from a fully initialized slice.
48impl<'data, T: Copy> From<&'data mut [T]> for BorrowedBuf<'data, T> {
49 #[inline]
50 fn from(slice: &'data mut [T]) -> BorrowedBuf<'data, T> {
51 BorrowedBuf {
52 // SAFETY: no initialized element is ever uninitialized as per `BorrowedBuf`'s invariant
53 buf: unsafe { &mut *(slice as *mut [T] as *mut [MaybeUninit<T>]) },
54 filled: 0,
55 init: true,
56 }
57 }
58}
59
60/// Creates a new `BorrowedBuf` from an uninitialized buffer.
61impl<'data, T: Copy> From<&'data mut [MaybeUninit<T>]> for BorrowedBuf<'data, T> {
62 #[inline]
63 fn from(buf: &'data mut [MaybeUninit<T>]) -> BorrowedBuf<'data, T> {
64 BorrowedBuf { buf, filled: 0, init: false }
65 }
66}
67
68/// Creates a new `BorrowedBuf` from a cursor.
69///
70/// Use `BorrowedCursor::with_unfilled_buf` instead for a safer alternative.
71impl<'data, T: Copy> From<BorrowedCursor<'data, T>> for BorrowedBuf<'data, T> {
72 #[inline]
73 fn from(buf: BorrowedCursor<'data, T>) -> BorrowedBuf<'data, T> {
74 BorrowedBuf {
75 // SAFETY: no initialized element is ever uninitialized as per `BorrowedBuf`'s invariant
76 buf: unsafe { buf.buf.buf.get_unchecked_mut(buf.buf.filled..) },
77 filled: 0,
78 init: buf.buf.init,
79 }
80 }
81}
82
83impl<'data, T> BorrowedBuf<'data, T> {
84 /// Returns the total capacity of the buffer.
85 #[inline]
86 pub fn capacity(&self) -> usize {
87 self.buf.len()
88 }
89
90 /// Returns the length of the filled part of the buffer.
91 #[inline]
92 pub fn len(&self) -> usize {
93 self.filled
94 }
95
96 /// Returns `true` if the buffer is initialized.
97 #[unstable(feature = "borrowed_buf_init", issue = "78485")]
98 #[inline]
99 pub fn is_init(&self) -> bool {
100 self.init
101 }
102}
103
104impl<'data, T: Copy> BorrowedBuf<'data, T> {
105 /// Returns a shared reference to the filled portion of the buffer.
106 #[inline]
107 pub fn filled(&self) -> &[T] {
108 // SAFETY: We only slice the filled part of the buffer, which is always valid
109 unsafe {
110 let buf = self.buf.get_unchecked(..self.filled);
111 buf.assume_init_ref()
112 }
113 }
114
115 /// Returns a mutable reference to the filled portion of the buffer.
116 #[inline]
117 pub fn filled_mut(&mut self) -> &mut [T] {
118 // SAFETY: We only slice the filled part of the buffer, which is always valid
119 unsafe {
120 let buf = self.buf.get_unchecked_mut(..self.filled);
121 buf.assume_init_mut()
122 }
123 }
124
125 /// Returns a shared reference to the filled portion of the buffer with its original lifetime.
126 #[inline]
127 pub fn into_filled(self) -> &'data [T] {
128 // SAFETY: We only slice the filled part of the buffer, which is always valid
129 unsafe {
130 let buf = self.buf.get_unchecked(..self.filled);
131 buf.assume_init_ref()
132 }
133 }
134
135 /// Returns a mutable reference to the filled portion of the buffer with its original lifetime.
136 #[inline]
137 pub fn into_filled_mut(self) -> &'data mut [T] {
138 // SAFETY: We only slice the filled part of the buffer, which is always valid
139 unsafe {
140 let buf = self.buf.get_unchecked_mut(..self.filled);
141 buf.assume_init_mut()
142 }
143 }
144
145 /// Returns a cursor over the unfilled part of the buffer.
146 #[inline]
147 pub fn unfilled<'this>(&'this mut self) -> BorrowedCursor<'this, T> {
148 BorrowedCursor {
149 // SAFETY: we never assign into `BorrowedCursor::buf`, so treating its
150 // lifetime covariantly is safe.
151 buf: unsafe {
152 mem::transmute::<&'this mut BorrowedBuf<'data, T>, &'this mut BorrowedBuf<'this, T>>(
153 self,
154 )
155 },
156 }
157 }
158
159 /// Clears the buffer, resetting the filled region to empty.
160 ///
161 /// The contents of the buffer are not modified.
162 #[inline]
163 pub fn clear(&mut self) -> &mut Self {
164 self.filled = 0;
165 self
166 }
167
168 /// Asserts that the unfilled part of the buffer is initialized.
169 ///
170 /// # Safety
171 ///
172 /// All the elements of the buffer must be initialized.
173 #[unstable(feature = "borrowed_buf_init", issue = "78485")]
174 #[inline]
175 pub unsafe fn set_init(&mut self) -> &mut Self {
176 self.init = true;
177 self
178 }
179}
180
181/// A writeable view of the unfilled portion of a [`BorrowedBuf`].
182///
183/// The unfilled portion may be uninitialized; see [`BorrowedBuf`] for details.
184///
185/// Data can be written directly to the cursor by using [`append`](BorrowedCursor::append) or
186/// indirectly by getting a slice of part or all of the cursor and writing into the slice. In the
187/// indirect case, the caller must call [`advance`](BorrowedCursor::advance) after writing to inform
188/// the cursor how many elements have been written.
189///
190/// Once elements are written to the cursor, they become part of the filled portion of the
191/// underlying `BorrowedBuf` and can no longer be accessed or re-written by the cursor. In other
192/// words, the cursor tracks the unfilled part of the underlying `BorrowedBuf`.
193///
194/// The lifetime `'a` is a bound on the lifetime of the underlying buffer (which means it is a bound
195/// on the elements in that buffer by transitivity).
196pub struct BorrowedCursor<'a, T> {
197 /// The underlying buffer.
198 // Safety invariant: we treat the type of buf as covariant in the lifetime of `BorrowedBuf` when
199 // we create a `BorrowedCursor`. This is only safe if we never replace `buf` by assigning into
200 // it, so don't do that!
201 buf: &'a mut BorrowedBuf<'a, T>,
202}
203
204impl<T> Debug for BorrowedCursor<'_, T> {
205 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
206 f.debug_struct("BorrowedCursor").field("buf", &self.buf).finish()
207 }
208}
209
210impl<'a, T: Copy> BorrowedCursor<'a, T> {
211 /// Reborrows this cursor by cloning it with a smaller lifetime.
212 ///
213 /// Since a cursor maintains unique access to its underlying buffer, the borrowed cursor is
214 /// not accessible while the new cursor exists.
215 #[inline]
216 pub fn reborrow<'this>(&'this mut self) -> BorrowedCursor<'this, T> {
217 BorrowedCursor {
218 // SAFETY: we never assign into `BorrowedCursor::buf`, so treating its
219 // lifetime covariantly is safe.
220 buf: unsafe {
221 mem::transmute::<&'this mut BorrowedBuf<'a, T>, &'this mut BorrowedBuf<'this, T>>(
222 self.buf,
223 )
224 },
225 }
226 }
227
228 /// Returns the available space in the cursor.
229 #[inline]
230 pub fn capacity(&self) -> usize {
231 self.buf.capacity() - self.buf.filled
232 }
233
234 /// Returns the number of elements written to the `BorrowedBuf` this cursor was created from.
235 ///
236 /// In particular, the count returned is shared by all reborrows of the cursor.
237 #[inline]
238 pub fn written(&self) -> usize {
239 self.buf.filled
240 }
241
242 /// Returns `true` if the buffer is initialized.
243 #[unstable(feature = "borrowed_buf_init", issue = "78485")]
244 #[inline]
245 pub fn is_init(&self) -> bool {
246 self.buf.init
247 }
248
249 /// Set the buffer as fully initialized.
250 ///
251 /// # Safety
252 ///
253 /// All the elements of the cursor must be initialized.
254 #[unstable(feature = "borrowed_buf_init", issue = "78485")]
255 #[inline]
256 pub unsafe fn set_init(&mut self) {
257 self.buf.init = true;
258 }
259
260 /// Returns a mutable reference to the whole cursor.
261 ///
262 /// # Safety
263 ///
264 /// The caller must not uninitialize any elements of the cursor if it is initialized.
265 #[inline]
266 pub unsafe fn as_mut(&mut self) -> &mut [MaybeUninit<T>] {
267 // SAFETY: always in bounds
268 unsafe { self.buf.buf.get_unchecked_mut(self.buf.filled..) }
269 }
270
271 /// Advances the cursor by asserting that `n` elements have been filled.
272 ///
273 /// After advancing, the `n` elements are no longer accessible via the cursor and can only be
274 /// accessed via the underlying buffer. I.e., the buffer's filled portion grows by `n` elements
275 /// and its unfilled portion (and the capacity of this cursor) shrinks by `n` elements.
276 ///
277 /// If less than `n` elements initialized (by the cursor's point of view), `set_init` should be
278 /// called first.
279 ///
280 /// # Panics
281 ///
282 /// Panics if there are less than `n` elements initialized.
283 #[unstable(feature = "borrowed_buf_init", issue = "78485")]
284 #[inline]
285 pub fn advance_checked(&mut self, n: usize) -> &mut Self {
286 // The subtraction cannot underflow by invariant of this type.
287 let init_unfilled = if self.buf.init { self.buf.buf.len() - self.buf.filled } else { 0 };
288 assert!(n <= init_unfilled);
289
290 self.buf.filled += n;
291 self
292 }
293
294 /// Advances the cursor by asserting that `n` elements have been filled.
295 ///
296 /// After advancing, the `n` elements are no longer accessible via the cursor and can only be
297 /// accessed via the underlying buffer. I.e., the buffer's filled portion grows by `n` elements
298 /// and its unfilled portion (and the capacity of this cursor) shrinks by `n` elements.
299 ///
300 /// # Safety
301 ///
302 /// The caller must ensure that the first `n` elements of the cursor have been initialized.
303 #[inline]
304 pub unsafe fn advance(&mut self, n: usize) -> &mut Self {
305 self.buf.filled += n;
306 self
307 }
308
309 /// Append elements to the cursor, advancing position within its buffer.
310 ///
311 /// # Panics
312 ///
313 /// Panics if `self.capacity()` is less than `buf.len()`.
314 #[inline]
315 pub fn append(&mut self, buf: &[T]) {
316 assert!(self.capacity() >= buf.len());
317
318 // SAFETY: we do not de-initialize any of the elements of the slice
319 unsafe {
320 self.as_mut()[..buf.len()].write_copy_of_slice(buf);
321 }
322
323 self.buf.filled += buf.len();
324 }
325
326 /// Runs the given closure with a `BorrowedBuf` containing the unfilled part
327 /// of the cursor.
328 ///
329 /// This enables inspecting what was written to the cursor.
330 ///
331 /// # Panics
332 ///
333 /// Panics if the `BorrowedBuf` given to the closure is replaced by another
334 /// one.
335 pub fn with_unfilled_buf<R>(&mut self, f: impl FnOnce(&mut BorrowedBuf<'_, T>) -> R) -> R {
336 let mut buf = BorrowedBuf::from(self.reborrow());
337 let prev_ptr = buf.buf as *const _;
338 let res = f(&mut buf);
339
340 // Check that the caller didn't replace the `BorrowedBuf`.
341 // This is necessary for the safety of the code below: if the check wasn't
342 // there, one could mark some elements as initialized even though they aren't.
343 assert!(core::ptr::eq(prev_ptr, buf.buf));
344
345 let filled = buf.filled;
346 let init = buf.init;
347
348 // Update `init` and `filled` fields with what was written to the buffer.
349 // `self.buf.filled` was the starting length of the `BorrowedBuf`.
350 //
351 // SAFETY: These elements were initialized/filled in the `BorrowedBuf`, and therefore they
352 // are initialized/filled in the cursor too, because the buffer wasn't replaced.
353 self.buf.init = init;
354 self.buf.filled += filled;
355
356 res
357 }
358}
359
360impl<'a> BorrowedCursor<'a, u8> {
361 /// Initializes all bytes in the cursor and returns them.
362 #[unstable(feature = "borrowed_buf_init", issue = "78485")]
363 #[inline]
364 pub fn ensure_init(&mut self) -> &mut [u8] {
365 // SAFETY: always in bounds and we never uninitialize these bytes.
366 let unfilled = unsafe { self.buf.buf.get_unchecked_mut(self.buf.filled..) };
367
368 if !self.buf.init {
369 // SAFETY: 0 is a valid value for MaybeUninit<u8> and the length matches the allocation
370 // since it is comes from a slice reference.
371 unsafe {
372 ptr::write_bytes(unfilled.as_mut_ptr(), 0, unfilled.len());
373 }
374 self.buf.init = true;
375 }
376
377 // SAFETY: these bytes have just been initialized if they weren't before
378 unsafe { unfilled.assume_init_mut() }
379 }
380}