embedded_io_async/impls/
vec_deque.rs

1//! Adapted from std.
2
3use alloc::collections::vec_deque::VecDeque;
4
5use crate::{BufRead, Read, ReadExactError, Write};
6
7/// Read is implemented for `VecDeque<u8>` by consuming bytes from the front of the `VecDeque`.
8#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
9impl Read for VecDeque<u8> {
10    /// Fill `buf` with the contents of the "front" slice as returned by
11    /// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are
12    /// discontiguous, multiple calls to `read` will be needed to read the entire content.
13    #[inline]
14    async fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
15        let (ref mut front, _) = self.as_slices();
16        let n = Read::read(front, buf).await?;
17        self.drain(..n);
18        Ok(n)
19    }
20
21    #[inline]
22    async fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), ReadExactError<Self::Error>> {
23        let (front, back) = self.as_slices();
24
25        // Use only the front buffer if it is big enough to fill `buf`, else use
26        // the back buffer too.
27        match buf.split_at_mut_checked(front.len()) {
28            None => buf.copy_from_slice(&front[..buf.len()]),
29            Some((buf_front, buf_back)) => match back.split_at_checked(buf_back.len()) {
30                Some((back, _)) => {
31                    buf_front.copy_from_slice(front);
32                    buf_back.copy_from_slice(back);
33                }
34                None => {
35                    self.clear();
36                    return Err(ReadExactError::UnexpectedEof);
37                }
38            },
39        }
40
41        self.drain(..buf.len());
42        Ok(())
43    }
44}
45
46/// BufRead is implemented for `VecDeque<u8>` by reading bytes from the front of the `VecDeque`.
47#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
48impl BufRead for VecDeque<u8> {
49    /// Returns the contents of the "front" slice as returned by
50    /// [`as_slices`][`VecDeque::as_slices`]. If the contained byte slices of the `VecDeque` are
51    /// discontiguous, multiple calls to `fill_buf` will be needed to read the entire content.
52    #[inline]
53    async fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
54        let (front, _) = self.as_slices();
55        Ok(front)
56    }
57
58    #[inline]
59    fn consume(&mut self, amt: usize) {
60        self.drain(..amt);
61    }
62}
63
64/// Write is implemented for `VecDeque<u8>` by appending to the `VecDeque`, growing it as needed.
65#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
66impl Write for VecDeque<u8> {
67    #[inline]
68    async fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
69        self.extend(buf);
70        Ok(buf.len())
71    }
72
73    #[inline]
74    async fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> {
75        self.extend(buf);
76        Ok(())
77    }
78
79    #[inline]
80    async fn flush(&mut self) -> Result<(), Self::Error> {
81        Ok(())
82    }
83}