compio_io/util/repeat.rs
1use std::mem::MaybeUninit;
2
3use compio_buf::{BufResult, IoVectoredBufMut};
4
5use crate::{AsyncBufRead, AsyncRead, IoResult};
6
7/// A reader that infinitely repeats one byte constructed via [`repeat`].
8///
9/// All reads from this reader will succeed by filling the specified buffer with
10/// the given byte.
11///
12/// # Examples
13///
14/// ```rust
15/// # futures_executor::block_on(async {
16/// use compio_io::{self, AsyncRead, AsyncReadExt};
17///
18/// let (len, buffer) = compio_io::repeat(42)
19/// .read(Vec::with_capacity(3))
20/// .await
21/// .unwrap();
22///
23/// assert_eq!(buffer.as_slice(), [42, 42, 42]);
24/// assert_eq!(len, 3);
25/// # })
26/// ```
27pub struct Repeat(u8);
28
29impl AsyncRead for Repeat {
30 async fn read<B: compio_buf::IoBufMut>(
31 &mut self,
32 mut buf: B,
33 ) -> compio_buf::BufResult<usize, B> {
34 let slice = buf.as_uninit();
35
36 let len = slice.len();
37 slice.fill(MaybeUninit::new(self.0));
38 // SAFETY: we just initialized exactly `len` bytes in `buf` from index 0.
39 unsafe { buf.advance_to(len) };
40
41 BufResult(Ok(len), buf)
42 }
43
44 async fn read_vectored<V: IoVectoredBufMut>(&mut self, mut buf: V) -> BufResult<usize, V> {
45 let mut len: usize = 0;
46 for slice in buf.iter_uninit_slice() {
47 len = len
48 .checked_add(slice.len())
49 .expect("total vectored buffer length overflow");
50 slice.fill(MaybeUninit::new(self.0));
51 }
52 debug_assert_eq!(len, buf.total_capacity());
53 // SAFETY: every byte counted in `len` is initialized in the loop above.
54 unsafe { buf.advance_vec_to(len) };
55
56 BufResult(Ok(len), buf)
57 }
58}
59
60impl AsyncBufRead for Repeat {
61 async fn fill_buf(&mut self) -> IoResult<&'_ [u8]> {
62 Ok(std::slice::from_ref(&self.0))
63 }
64
65 fn consume(&mut self, _: usize) {}
66}
67
68/// Creates a reader that infinitely repeats one byte.
69///
70/// All reads from this reader will succeed by filling the specified buffer with
71/// the given byte.
72///
73/// # Examples
74///
75/// ```rust
76/// # futures_executor::block_on(async {
77/// use compio_io::{self, AsyncRead, AsyncReadExt};
78///
79/// let ((), buffer) = compio_io::repeat(42)
80/// .read_exact(Vec::with_capacity(3))
81/// .await
82/// .unwrap();
83///
84/// assert_eq!(buffer.as_slice(), [42, 42, 42]);
85/// # })
86/// ```
87pub fn repeat(byte: u8) -> Repeat {
88 Repeat(byte)
89}