layered_io/
slice_reader.rs

1use crate::{Bufferable, ReadLayered, Status};
2use std::io::{self, IoSliceMut, Read};
3
4/// Adapts an `&[u8]` to implement [`ReadLayered`].
5pub struct SliceReader<'slice> {
6    slice: &'slice [u8],
7}
8
9impl<'slice> SliceReader<'slice> {
10    /// Construct a new `SliceReader` which wraps `slice`.
11    #[inline]
12    pub fn new(slice: &'slice [u8]) -> Self {
13        Self { slice }
14    }
15}
16
17impl<'slice> ReadLayered for SliceReader<'slice> {
18    #[inline]
19    fn read_with_status(&mut self, buf: &mut [u8]) -> io::Result<(usize, Status)> {
20        let size = Read::read(&mut self.slice, buf)?;
21        Ok((
22            size,
23            if self.slice.is_empty() {
24                Status::End
25            } else {
26                Status::active()
27            },
28        ))
29    }
30
31    #[inline]
32    fn read_vectored_with_status(
33        &mut self,
34        bufs: &mut [IoSliceMut<'_>],
35    ) -> io::Result<(usize, Status)> {
36        let size = Read::read_vectored(&mut self.slice, bufs)?;
37        Ok((
38            size,
39            if self.slice.is_empty() {
40                Status::End
41            } else {
42                Status::active()
43            },
44        ))
45    }
46}
47
48impl<'slice> Bufferable for SliceReader<'slice> {
49    #[inline]
50    fn abandon(&mut self) {
51        self.slice = &[];
52    }
53
54    #[inline]
55    fn suggested_buffer_size(&self) -> usize {
56        // This is just writing values to memory, so no need to buffer.
57        0
58    }
59}
60
61impl<'slice> Read for SliceReader<'slice> {
62    #[inline]
63    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
64        Read::read(&mut self.slice, buf)
65    }
66
67    #[inline]
68    fn read_vectored(&mut self, bufs: &mut [io::IoSliceMut<'_>]) -> io::Result<usize> {
69        Read::read_vectored(&mut self.slice, bufs)
70    }
71
72    #[cfg(can_vector)]
73    #[inline]
74    fn is_read_vectored(&self) -> bool {
75        Read::is_read_vectored(&self.slice)
76    }
77
78    #[inline]
79    fn read_to_end(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
80        Read::read_to_end(&mut self.slice, buf)
81    }
82
83    #[inline]
84    fn read_to_string(&mut self, buf: &mut String) -> io::Result<usize> {
85        Read::read_to_string(&mut self.slice, buf)
86    }
87
88    #[inline]
89    fn read_exact(&mut self, buf: &mut [u8]) -> io::Result<()> {
90        Read::read_exact(&mut self.slice, buf)
91    }
92}
93
94#[test]
95fn test_slice_read_with_status() {
96    let mut reader = SliceReader::new(b"hello world!");
97    let mut buf = vec![0; 5];
98    assert_eq!(
99        reader.read_with_status(&mut buf).unwrap(),
100        (5, Status::active())
101    );
102    assert_eq!(buf, b"hello");
103    assert_eq!(
104        reader.read_with_status(&mut buf).unwrap(),
105        (5, Status::active())
106    );
107    assert_eq!(buf, b" worl");
108    assert_eq!(reader.read_with_status(&mut buf).unwrap(), (2, Status::End));
109    assert_eq!(&buf[..2], b"d!");
110    assert_eq!(reader.read_with_status(&mut buf).unwrap(), (0, Status::End));
111}
112
113#[test]
114fn test_slice_read() {
115    let mut reader = SliceReader::new(b"hello world!");
116    let mut buf = vec![0; 5];
117    assert_eq!(reader.read(&mut buf).unwrap(), 5);
118    assert_eq!(buf, b"hello");
119    assert_eq!(reader.read(&mut buf).unwrap(), 5);
120    assert_eq!(buf, b" worl");
121    assert_eq!(reader.read(&mut buf).unwrap(), 2);
122    assert_eq!(&buf[..2], b"d!");
123    assert_eq!(reader.read(&mut buf).unwrap(), 0);
124}