use crate::{Decoder, Error, Result};
pub trait Reader: Clone {
fn read<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]>;
fn remaining_len(&self) -> usize;
fn into_decoder(self) -> Result<Decoder<Self>> {
Decoder::new(self)
}
}
impl Reader for &[u8] {
fn read<'o>(&mut self, out: &'o mut [u8]) -> Result<&'o [u8]> {
if self.len() >= out.len() {
let (head, tail) = self.split_at(out.len());
*self = tail;
out.copy_from_slice(head);
Ok(out)
} else {
Err(Error::Length)
}
}
fn remaining_len(&self) -> usize {
self.len()
}
}