pint_abi/
read.rs

1use crate::types::essential::Word;
2
3/// Allows for reading words from a source, largely inspired by [`std::io::Read`].
4pub trait Read {
5    /// A type representing any error that might occur while reading words.
6    type Error: core::fmt::Debug + core::fmt::Display;
7
8    /// Read words from this source into the specified buffer.
9    ///
10    /// Returns how many words were read.
11    ///
12    /// Repeated calls to `read` should use the same cursor, i.e. the same words
13    /// should not be yielded twice.
14    fn read(&mut self, buf: &mut [Word]) -> Result<usize, Self::Error>;
15}
16
17impl<T: Read> Read for &'_ mut T {
18    type Error = T::Error;
19    fn read(&mut self, buf: &mut [Word]) -> Result<usize, Self::Error> {
20        (*self).read(buf)
21    }
22}
23
24impl Read for &'_ [Word] {
25    type Error = core::convert::Infallible;
26    fn read(&mut self, buf: &mut [Word]) -> Result<usize, Self::Error> {
27        let amt = core::cmp::min(self.len(), buf.len());
28        let (a, b) = self.split_at(amt);
29        buf[..amt].copy_from_slice(a);
30        *self = b;
31        Ok(amt)
32    }
33}