Skip to content

Commit

Permalink
Rename Read trait to be less confusing
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewhickman committed Jan 29, 2021
1 parent 9bba59d commit d583d52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/lib.rs
Expand Up @@ -15,7 +15,7 @@ mod read;
pub use model::*;
pub use failure::Error;

use read::{Read, IoReader, SliceReader};
use read::{BufReadExact, IoReader, SliceReader};

use memchr::memchr;

Expand All @@ -25,7 +25,7 @@ use std::path::PathBuf;

pub type Result<T> = std::result::Result<T, Error>;

struct Reader<R: Read> {
struct Reader<R: BufReadExact> {
rdr: R,
}

Expand All @@ -41,7 +41,7 @@ impl<'a> Reader<SliceReader<'a>> {
}
}

impl<R: Read> Reader<R> {
impl<R: BufReadExact> Reader<R> {
fn read_model(&mut self) -> Result<Model> {
let header = self.read_header()?;
let vertices = self.read_vertices()?;
Expand Down Expand Up @@ -374,7 +374,7 @@ impl<R: Read> Reader<R> {
}

fn read_string(&mut self, len: usize) -> Result<String> {
Ok(str::from_utf8(self.rdr.read(len)?)?.to_owned())
Ok(str::from_utf8(self.rdr.buf_read_exact(len)?)?.to_owned())
}

fn read_vec<T, F>(&mut self, len: usize, f: F) -> Result<Vec<T>>
Expand All @@ -398,7 +398,7 @@ impl<R: Read> Reader<R> {

unsafe fn read_type<T>(&mut self) -> Result<T> {
Ok(ptr::read_unaligned(
self.rdr.read(mem::size_of::<T>())? as *const [u8] as *const T,
self.rdr.buf_read_exact(mem::size_of::<T>())? as *const [u8] as *const T,
))
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/read.rs
@@ -1,7 +1,7 @@
use std::io;

pub(crate) trait Read {
fn read(&mut self, len: usize) -> io::Result<&[u8]>;
pub(crate) trait BufReadExact {
fn buf_read_exact(&mut self, len: usize) -> io::Result<&[u8]>;
}

pub(crate) struct IoReader<R: io::Read> {
Expand All @@ -15,8 +15,8 @@ impl<R: io::Read> IoReader<R> {
}
}

impl<R: io::Read> Read for IoReader<R> {
fn read(&mut self, len: usize) -> io::Result<&[u8]> {
impl<R: io::Read> BufReadExact for IoReader<R> {
fn buf_read_exact(&mut self, len: usize) -> io::Result<&[u8]> {
unsafe {
self.buf.reserve(len);
let slice = self.buf.get_unchecked_mut(..len);
Expand All @@ -36,8 +36,8 @@ impl<'a> SliceReader<'a> {
}
}

impl<'a> Read for SliceReader<'a> {
fn read(&mut self, len: usize) -> io::Result<&[u8]> {
impl<'a> BufReadExact for SliceReader<'a> {
fn buf_read_exact(&mut self, len: usize) -> io::Result<&[u8]> {
if len > self.slice.len() {
return Err(io::ErrorKind::UnexpectedEof.into())
}
Expand Down

0 comments on commit d583d52

Please sign in to comment.