aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLuke Steensen <luke.steensen@gmail.com>2016-01-21 20:55:43 -0600
committerLuke Steensen <luke.steensen@gmail.com>2016-03-11 12:21:45 -0600
commite429dfb9ccb74e32c5f1800a20318ea2162ef262 (patch)
tree6289db10c6a27beb2d5e1f09540bd8c1549a65f3 /src
parent1d40ec03ecefcbad7bb9922e7c6f62b077569354 (diff)
downloadbyteorder-e429dfb9ccb74e32c5f1800a20318ea2162ef262.tar.gz
remove custom error type and read_full
Rust 1.6 stabilized `read_exact`, which gives us the same functionality without having to wrap `std::io::Error`. Removing the custom error type makes this a breaking change, and users will have to replace uses of `byteorder::Error` with `std::io::Error`. [breaking-change]
Diffstat (limited to 'src')
-rw-r--r--src/lib.rs2
-rw-r--r--src/new.rs102
2 files changed, 14 insertions, 90 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 7eea1e3..aba9113 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -45,7 +45,7 @@ use std::mem::transmute;
use std::ptr::copy_nonoverlapping;
#[cfg(not(feature = "no-std"))]
-pub use new::{ReadBytesExt, WriteBytesExt, Error, Result};
+pub use new::{ReadBytesExt, WriteBytesExt};
#[cfg(not(feature = "no-std"))]
mod new;
diff --git a/src/new.rs b/src/new.rs
index 54ee6a7..eb4bdf2 100644
--- a/src/new.rs
+++ b/src/new.rs
@@ -1,70 +1,7 @@
-use std::error;
-use std::fmt;
-use std::io;
-use std::result;
+use std::io::{self, Result};
use ByteOrder;
-/// A short-hand for `result::Result<T, byteorder::Error>`.
-pub type Result<T> = result::Result<T, Error>;
-
-/// An error type for reading bytes.
-///
-/// This is a thin wrapper over the standard `io::Error` type. Namely, it
-/// adds one additional error case: an unexpected EOF.
-///
-/// Note that this error is also used for the `write` methods to keep things
-/// consistent.
-#[derive(Debug)]
-pub enum Error {
- /// An unexpected EOF.
- ///
- /// This occurs when a call to the underlying reader returns `0` bytes,
- /// but more bytes are required to decode a meaningful value.
- UnexpectedEOF,
- /// Any underlying IO error that occurs while reading bytes.
- Io(io::Error),
-}
-
-impl From<io::Error> for Error {
- fn from(err: io::Error) -> Error { Error::Io(err) }
-}
-
-impl From<Error> for io::Error {
- fn from(err: Error) -> io::Error {
- match err {
- Error::Io(err) => err,
- Error::UnexpectedEOF => io::Error::new(io::ErrorKind::Other,
- "unexpected EOF")
- }
- }
-}
-
-impl fmt::Display for Error {
- fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
- match *self {
- Error::UnexpectedEOF => write!(f, "Unexpected end of file."),
- Error::Io(ref err) => err.fmt(f),
- }
- }
-}
-
-impl error::Error for Error {
- fn description(&self) -> &str {
- match *self {
- Error::UnexpectedEOF => "Unexpected end of file.",
- Error::Io(ref err) => error::Error::description(err),
- }
- }
-
- fn cause(&self) -> Option<&error::Error> {
- match *self {
- Error::UnexpectedEOF => None,
- Error::Io(ref err) => err.cause(),
- }
- }
-}
-
/// Extends `Read` with methods for reading numbers. (For `std::io`.)
///
/// Most of the methods defined here have an unconstrained type parameter that
@@ -91,7 +28,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_u8(&mut self) -> Result<u8> {
let mut buf = [0; 1];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(buf[0])
}
@@ -102,7 +39,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_i8(&mut self) -> Result<i8> {
let mut buf = [0; 1];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(buf[0] as i8)
}
@@ -110,7 +47,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_u16<T: ByteOrder>(&mut self) -> Result<u16> {
let mut buf = [0; 2];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(T::read_u16(&buf))
}
@@ -118,7 +55,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_i16<T: ByteOrder>(&mut self) -> Result<i16> {
let mut buf = [0; 2];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(T::read_i16(&buf))
}
@@ -126,7 +63,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_u32<T: ByteOrder>(&mut self) -> Result<u32> {
let mut buf = [0; 4];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(T::read_u32(&buf))
}
@@ -134,7 +71,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_i32<T: ByteOrder>(&mut self) -> Result<i32> {
let mut buf = [0; 4];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(T::read_i32(&buf))
}
@@ -142,7 +79,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_u64<T: ByteOrder>(&mut self) -> Result<u64> {
let mut buf = [0; 8];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(T::read_u64(&buf))
}
@@ -150,7 +87,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_i64<T: ByteOrder>(&mut self) -> Result<i64> {
let mut buf = [0; 8];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(T::read_i64(&buf))
}
@@ -158,7 +95,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_uint<T: ByteOrder>(&mut self, nbytes: usize) -> Result<u64> {
let mut buf = [0; 8];
- try!(read_full(self, &mut buf[..nbytes]));
+ try!(self.read_exact(&mut buf[..nbytes]));
Ok(T::read_uint(&buf[..nbytes], nbytes))
}
@@ -166,7 +103,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_int<T: ByteOrder>(&mut self, nbytes: usize) -> Result<i64> {
let mut buf = [0; 8];
- try!(read_full(self, &mut buf[..nbytes]));
+ try!(self.read_exact(&mut buf[..nbytes]));
Ok(T::read_int(&buf[..nbytes], nbytes))
}
@@ -175,7 +112,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_f32<T: ByteOrder>(&mut self) -> Result<f32> {
let mut buf = [0; 4];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(T::read_f32(&buf))
}
@@ -184,7 +121,7 @@ pub trait ReadBytesExt: io::Read {
#[inline]
fn read_f64<T: ByteOrder>(&mut self) -> Result<f64> {
let mut buf = [0; 8];
- try!(read_full(self, &mut buf));
+ try!(self.read_exact(&mut buf));
Ok(T::read_f64(&buf))
}
}
@@ -193,19 +130,6 @@ pub trait ReadBytesExt: io::Read {
/// for free.
impl<R: io::Read + ?Sized> ReadBytesExt for R {}
-fn read_full<R: io::Read + ?Sized>(rdr: &mut R, buf: &mut [u8]) -> Result<()> {
- let mut nread = 0usize;
- while nread < buf.len() {
- match rdr.read(&mut buf[nread..]) {
- Ok(0) => return Err(Error::UnexpectedEOF),
- Ok(n) => nread += n,
- Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {},
- Err(e) => return Err(From::from(e))
- }
- }
- Ok(())
-}
-
fn write_all<W: io::Write + ?Sized>(wtr: &mut W, buf: &[u8]) -> Result<()> {
wtr.write_all(buf).map_err(From::from)
}