Hoist common features to BinarySource

This commit is contained in:
Tony Garnock-Jones 2022-11-08 22:52:11 +01:00
parent 7a71f72491
commit 7a7469bed1
3 changed files with 16 additions and 17 deletions

View File

@ -1,5 +1,5 @@
use crate::{ValueClass, AtomClass, Atom}; use crate::{ValueClass, AtomClass, Atom};
use crate::error::{self, ExpectedKind, io_eof}; use crate::error::{self, ExpectedKind};
use num_bigint::BigInt; use num_bigint::BigInt;
use num_traits::cast::{FromPrimitive, ToPrimitive}; use num_traits::cast::{FromPrimitive, ToPrimitive};
@ -78,18 +78,6 @@ impl<'de, 'src, S: BinarySource<'de>> PackedReader<'de, 'src, S> {
PackedReader { source, phantom: PhantomData } PackedReader { source, phantom: PhantomData }
} }
#[inline(always)]
fn peek_noeof(&mut self) -> io::Result<u8> {
self.peek()?.ok_or_else(io_eof)
}
#[inline(always)]
fn read(&mut self) -> io::Result<u8> {
let v = self.peek_noeof()?;
self.skip()?;
Ok(v)
}
#[inline(always)] #[inline(always)]
fn varint(&mut self) -> io::Result<u64> { fn varint(&mut self) -> io::Result<u64> {
let mut shift = 0; let mut shift = 0;

View File

@ -17,6 +17,18 @@ pub trait BinarySource<'de>: Sized {
//--------------------------------------------------------------------------- //---------------------------------------------------------------------------
#[inline(always)]
fn peek_noeof(&mut self) -> io::Result<u8> {
self.peek()?.ok_or_else(io_eof)
}
#[inline(always)]
fn read(&mut self) -> io::Result<u8> {
let v = self.peek_noeof()?;
self.skip()?;
Ok(v)
}
fn syntax_error(&mut self, message: &str) -> io::Error { fn syntax_error(&mut self, message: &str) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, SyntaxError { io::Error::new(io::ErrorKind::InvalidData, SyntaxError {
position: match self.input_position() { position: match self.input_position() {

View File

@ -67,17 +67,16 @@ impl<'de, 'src, S: BinarySource<'de>> TextReader<'de, 'src, S>
#[inline(always)] #[inline(always)]
fn peek_noeof(&mut self) -> io::Result<u8> { fn peek_noeof(&mut self) -> io::Result<u8> {
self.source.peek()?.ok_or_else(io_eof) self.source.peek_noeof()
} }
fn skip(&mut self) -> io::Result<()> { fn skip(&mut self) -> io::Result<()> {
self.source.skip() self.source.skip()
} }
#[inline(always)]
fn next_byte(&mut self) -> io::Result<u8> { fn next_byte(&mut self) -> io::Result<u8> {
let b = self.peek_noeof()?; self.source.read()
self.source.skip()?;
Ok(b)
} }
fn skip_whitespace(&mut self) { fn skip_whitespace(&mut self) {