1#![allow(unsafe_code)]
4
5#[cfg(feature = "alloc")]
6use alloc::vec::Vec;
7use core::mem::MaybeUninit;
8use core::slice;
9
10pub trait Buffer<T>: private::Sealed<T> {}
98
99impl<T> Buffer<T> for &mut [T] {}
101impl<T, const N: usize> Buffer<T> for &mut [T; N] {}
102#[cfg(feature = "alloc")]
103impl<T> Buffer<T> for &mut Vec<T> {}
104impl<T> Buffer<T> for &mut [MaybeUninit<T>] {}
105impl<T, const N: usize> Buffer<T> for &mut [MaybeUninit<T>; N] {}
106#[cfg(feature = "alloc")]
107impl<T> Buffer<T> for &mut Vec<MaybeUninit<T>> {}
108#[cfg(feature = "alloc")]
109impl<'a, T> Buffer<T> for SpareCapacity<'a, T> {}
110
111impl<T> private::Sealed<T> for &mut [T] {
112 type Output = usize;
113
114 #[inline]
115 fn parts_mut(&mut self) -> (*mut T, usize) {
116 (self.as_mut_ptr(), self.len())
117 }
118
119 #[inline]
120 unsafe fn assume_init(self, len: usize) -> Self::Output {
121 len
122 }
123}
124
125impl<T, const N: usize> private::Sealed<T> for &mut [T; N] {
126 type Output = usize;
127
128 #[inline]
129 fn parts_mut(&mut self) -> (*mut T, usize) {
130 (self.as_mut_ptr(), N)
131 }
132
133 #[inline]
134 unsafe fn assume_init(self, len: usize) -> Self::Output {
135 len
136 }
137}
138
139#[cfg(feature = "alloc")]
143impl<T> private::Sealed<T> for &mut Vec<T> {
144 type Output = usize;
145
146 #[inline]
147 fn parts_mut(&mut self) -> (*mut T, usize) {
148 (self.as_mut_ptr(), self.len())
149 }
150
151 #[inline]
152 unsafe fn assume_init(self, len: usize) -> Self::Output {
153 len
154 }
155}
156
157impl<'a, T> private::Sealed<T> for &'a mut [MaybeUninit<T>] {
158 type Output = (&'a mut [T], &'a mut [MaybeUninit<T>]);
159
160 #[inline]
161 fn parts_mut(&mut self) -> (*mut T, usize) {
162 (self.as_mut_ptr().cast(), self.len())
163 }
164
165 #[inline]
166 unsafe fn assume_init(self, len: usize) -> Self::Output {
167 let (init, uninit) = self.split_at_mut(len);
168
169 let init = slice::from_raw_parts_mut(init.as_mut_ptr().cast::<T>(), init.len());
171
172 (init, uninit)
173 }
174}
175
176impl<'a, T, const N: usize> private::Sealed<T> for &'a mut [MaybeUninit<T>; N] {
177 type Output = (&'a mut [T], &'a mut [MaybeUninit<T>]);
178
179 #[inline]
180 fn parts_mut(&mut self) -> (*mut T, usize) {
181 (self.as_mut_ptr().cast(), self.len())
182 }
183
184 #[inline]
185 unsafe fn assume_init(self, len: usize) -> Self::Output {
186 let (init, uninit) = self.split_at_mut(len);
187
188 let init = slice::from_raw_parts_mut(init.as_mut_ptr().cast::<T>(), init.len());
190
191 (init, uninit)
192 }
193}
194
195#[cfg(feature = "alloc")]
196impl<'a, T> private::Sealed<T> for &'a mut Vec<MaybeUninit<T>> {
197 type Output = (&'a mut [T], &'a mut [MaybeUninit<T>]);
198
199 #[inline]
200 fn parts_mut(&mut self) -> (*mut T, usize) {
201 (self.as_mut_ptr().cast(), self.len())
202 }
203
204 #[inline]
205 unsafe fn assume_init(self, len: usize) -> Self::Output {
206 let (init, uninit) = self.split_at_mut(len);
207
208 let init = slice::from_raw_parts_mut(init.as_mut_ptr().cast::<T>(), init.len());
210
211 (init, uninit)
212 }
213}
214
215#[cfg(feature = "alloc")]
223#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
224pub struct SpareCapacity<'a, T>(&'a mut Vec<T>);
225
226#[cfg(feature = "alloc")]
256#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
257pub fn spare_capacity<'a, T>(v: &'a mut Vec<T>) -> SpareCapacity<'a, T> {
258 debug_assert_ne!(
259 v.capacity(),
260 0,
261 "`extend` uses spare capacity, and never allocates new memory, so the `Vec` passed to it \
262 should have some spare capacity."
263 );
264
265 SpareCapacity(v)
266}
267
268#[cfg(feature = "alloc")]
269impl<'a, T> private::Sealed<T> for SpareCapacity<'a, T> {
270 type Output = usize;
274
275 #[inline]
276 fn parts_mut(&mut self) -> (*mut T, usize) {
277 let spare = self.0.spare_capacity_mut();
278 (spare.as_mut_ptr().cast(), spare.len())
279 }
280
281 #[inline]
282 unsafe fn assume_init(self, len: usize) -> Self::Output {
283 self.0.set_len(self.0.len() + len);
285 len
286 }
287}
288
289mod private {
290 pub trait Sealed<T> {
291 type Output;
293
294 fn parts_mut(&mut self) -> (*mut T, usize);
305
306 #[must_use]
312 unsafe fn assume_init(self, len: usize) -> Self::Output;
313 }
314}
315
316#[cfg(test)]
317mod tests {
318 #[allow(unused_imports)]
319 use super::*;
320
321 #[cfg(not(windows))]
322 #[test]
323 fn test_compilation() {
324 use crate::io::read;
325 use core::mem::MaybeUninit;
326
327 let input = std::fs::File::open("src/buffer.rs").unwrap();
329
330 let mut buf = vec![0_u8; 3];
331 buf.reserve(32);
332 let _x: usize = read(&input, spare_capacity(&mut buf)).unwrap();
333 let _x: (&mut [u8], &mut [MaybeUninit<u8>]) =
334 read(&input, buf.spare_capacity_mut()).unwrap();
335 let _x: usize = read(&input, &mut buf).unwrap();
336 let _x: usize = read(&input, &mut *buf).unwrap();
337 let _x: usize = read(&input, &mut buf[..]).unwrap();
338 let _x: usize = read(&input, &mut (*buf)[..]).unwrap();
339
340 let mut buf = [0, 0, 0];
341 let _x: usize = read(&input, &mut buf).unwrap();
342 let _x: usize = read(&input, &mut buf[..]).unwrap();
343
344 let mut buf = [
345 MaybeUninit::uninit(),
346 MaybeUninit::uninit(),
347 MaybeUninit::uninit(),
348 ];
349 let _x: (&mut [u8], &mut [MaybeUninit<u8>]) = read(&input, &mut buf).unwrap();
350 let _x: (&mut [u8], &mut [MaybeUninit<u8>]) = read(&input, &mut buf[..]).unwrap();
351
352 let mut buf = vec![
353 MaybeUninit::uninit(),
354 MaybeUninit::uninit(),
355 MaybeUninit::uninit(),
356 ];
357 let _x: (&mut [u8], &mut [MaybeUninit<u8>]) = read(&input, &mut buf).unwrap();
358 let _x: (&mut [u8], &mut [MaybeUninit<u8>]) = read(&input, &mut buf[..]).unwrap();
359 }
360
361 #[cfg(not(windows))]
362 #[test]
363 fn test_slice() {
364 use crate::io::read;
365 use std::io::{Seek, SeekFrom};
366
367 let mut input = std::fs::File::open("src/buffer.rs").unwrap();
370
371 let mut buf = [0_u8; 64];
372 let nread = read(&input, &mut buf).unwrap();
373 assert_eq!(nread, buf.len());
374 assert_eq!(
375 &buf[..58],
376 b"//! Utilities for functions that return data via buffers.\n"
377 );
378 input.seek(SeekFrom::End(-1)).unwrap();
379 let nread = read(&input, &mut buf).unwrap();
380 assert_eq!(nread, 1);
381 input.seek(SeekFrom::End(0)).unwrap();
382 let nread = read(&input, &mut buf).unwrap();
383 assert_eq!(nread, 0);
384 }
385
386 #[cfg(not(windows))]
387 #[test]
388 fn test_slice_uninit() {
389 use crate::io::read;
390 use core::mem::MaybeUninit;
391 use std::io::{Seek, SeekFrom};
392
393 let mut input = std::fs::File::open("src/buffer.rs").unwrap();
396
397 let mut buf = [MaybeUninit::<u8>::uninit(); 64];
398 let (init, uninit) = read(&input, &mut buf).unwrap();
399 assert_eq!(uninit.len(), 0);
400 assert_eq!(
401 &init[..58],
402 b"//! Utilities for functions that return data via buffers.\n"
403 );
404 assert_eq!(init.len(), buf.len());
405 assert_eq!(
406 unsafe { core::mem::transmute::<&mut [MaybeUninit<u8>], &mut [u8]>(&mut buf[..58]) },
407 b"//! Utilities for functions that return data via buffers.\n"
408 );
409 input.seek(SeekFrom::End(-1)).unwrap();
410 let (init, uninit) = read(&input, &mut buf).unwrap();
411 assert_eq!(init.len(), 1);
412 assert_eq!(uninit.len(), buf.len() - 1);
413 input.seek(SeekFrom::End(0)).unwrap();
414 let (init, uninit) = read(&input, &mut buf).unwrap();
415 assert_eq!(init.len(), 0);
416 assert_eq!(uninit.len(), buf.len());
417 }
418
419 #[cfg(not(windows))]
420 #[test]
421 fn test_spare_capacity() {
422 use crate::io::read;
423 use std::io::{Seek, SeekFrom};
424
425 let mut input = std::fs::File::open("src/buffer.rs").unwrap();
428
429 let mut buf = Vec::with_capacity(64);
430 let nread = read(&input, spare_capacity(&mut buf)).unwrap();
431 assert_eq!(nread, buf.capacity());
432 assert_eq!(nread, buf.len());
433 assert_eq!(
434 &buf[..58],
435 b"//! Utilities for functions that return data via buffers.\n"
436 );
437 buf.clear();
438 input.seek(SeekFrom::End(-1)).unwrap();
439 let nread = read(&input, spare_capacity(&mut buf)).unwrap();
440 assert_eq!(nread, 1);
441 assert_eq!(buf.len(), 1);
442 buf.clear();
443 input.seek(SeekFrom::End(0)).unwrap();
444 let nread = read(&input, spare_capacity(&mut buf)).unwrap();
445 assert_eq!(nread, 0);
446 assert!(buf.is_empty());
447 }
448}