Skip to content

Commit

Permalink
Using io::ErorKind instead of io::Error; removing deprecated cause
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Feb 21, 2021
1 parent 76eba1d commit cee0a54
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 41 deletions.
30 changes: 7 additions & 23 deletions src/consensus/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ use network::address::{Address, AddrV2Message};
#[derive(Debug)]
pub enum Error {
/// And I/O error
Io(io::Error),
Io(io::ErrorKind),
/// PSBT-related error
Psbt(psbt::Error),
/// Network magic was not expected
Expand Down Expand Up @@ -85,7 +85,7 @@ pub enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Io(ref e) => write!(f, "I/O error: {}", e),
Error::Io(ref e) => write!(f, "I/O error: {}", io::Error::from(*e)),
Error::Psbt(ref e) => write!(f, "PSBT error: {}", e),
Error::UnexpectedNetworkMagic { expected: ref e, actual: ref a } => write!(f,
"unexpected network magic: expected {}, actual {}", e, a),
Expand All @@ -102,26 +102,10 @@ impl fmt::Display for Error {
}
}

impl error::Error for Error {
fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Io(ref e) => Some(e),
Error::Psbt(ref e) => Some(e),
Error::UnexpectedNetworkMagic { .. }
| Error::OversizedVectorAllocation { .. }
| Error::InvalidChecksum { .. }
| Error::NonMinimalVarInt
| Error::UnknownNetworkMagic(..)
| Error::ParseFailed(..)
| Error::UnsupportedSegwitFlag(..) => None,
}
}
}

#[doc(hidden)]
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Io(error)
Error::Io(error.kind())
}
}

Expand Down Expand Up @@ -240,7 +224,7 @@ macro_rules! decoder_fn {
fn $name(&mut self) -> Result<$val_type, Error> {
debug_assert_eq!(::std::mem::size_of::<$val_type>(), $byte_len); // size_of isn't a constfn in 1.22
let mut val = [0; $byte_len];
self.read_exact(&mut val[..]).map_err(Error::Io)?;
self.read_exact(&mut val[..]).map_err(Error::from)?;
Ok(endian::$readfn(&val))
}
}
Expand Down Expand Up @@ -298,7 +282,7 @@ impl<R: Read> ReadExt for R {
}
#[inline]
fn read_slice(&mut self, slice: &mut [u8]) -> Result<(), Error> {
self.read_exact(slice).map_err(Error::Io)
self.read_exact(slice).map_err(Error::from)
}
}

Expand Down Expand Up @@ -953,7 +937,7 @@ mod tests {
// found by cargo fuzz
assert!(deserialize::<Vec<u64>>(&[0xff,0xff,0xff,0xff,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0x6b,0xa,0xa,0x3a]).is_err());

let rand_io_err = Error::Io(io::Error::new(io::ErrorKind::Other, ""));
let rand_io_err = Error::Io(io::ErrorKind::Other);

// Check serialization that `if len > MAX_VEC_SIZE {return err}` isn't inclusive,
// by making sure it fails with IO Error and not an `OversizedVectorAllocation` Error.
Expand All @@ -974,7 +958,7 @@ mod tests {
}

fn test_len_is_max_vec<T>() where Vec<T>: Decodable, T: fmt::Debug {
let rand_io_err = Error::Io(io::Error::new(io::ErrorKind::Other, ""));
let rand_io_err = Error::Io(io::ErrorKind::Other);
let varint = VarInt((super::MAX_VEC_SIZE / mem::size_of::<T>()) as u64);
let err = deserialize::<Vec<T>>(&serialize(&varint)).unwrap_err();
assert_eq!(discriminant(&err), discriminant(&rand_io_err));
Expand Down
16 changes: 3 additions & 13 deletions src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub mod stream_reader;
#[derive(Debug)]
pub enum Error {
/// And I/O error
Io(io::Error),
Io(io::ErrorKind),
/// Socket mutex was poisoned
SocketMutexPoisoned,
/// Not connected to peer
Expand All @@ -46,7 +46,7 @@ pub enum Error {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::Io(ref e) => fmt::Display::fmt(e, f),
Error::Io(ref e) => fmt::Display::fmt(&io::Error::from(*e), f),
Error::SocketMutexPoisoned => f.write_str("socket mutex was poisoned"),
Error::SocketNotConnectedToPeer => f.write_str("not connected to peer"),
}
Expand All @@ -56,16 +56,6 @@ impl fmt::Display for Error {
#[doc(hidden)]
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}

impl error::Error for Error {

fn cause(&self) -> Option<&dyn error::Error> {
match *self {
Error::Io(ref e) => Some(e),
Error::SocketMutexPoisoned | Error::SocketNotConnectedToPeer => None,
}
Error::Io(err.kind())
}
}
4 changes: 2 additions & 2 deletions src/network/stream_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ impl<R: Read> StreamReader<R> {
loop {
match encode::deserialize_partial::<D>(&self.unparsed) {
// In this case we just have an incomplete data, so we need to read more
Err(encode::Error::Io(ref err)) if err.kind () == io::ErrorKind::UnexpectedEof => {
Err(encode::Error::Io(ref err)) if *err == io::ErrorKind::UnexpectedEof => {
let count = self.stream.read(&mut self.data)?;
if count > 0 {
self.unparsed.extend(self.data[0..count].iter());
}
else {
return Err(encode::Error::Io(io::Error::from(io::ErrorKind::UnexpectedEof)));
return Err(encode::Error::Io(io::ErrorKind::UnexpectedEof));
}
},
Err(err) => return Err(err),
Expand Down
6 changes: 3 additions & 3 deletions src/util/bip158.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub enum Error {
/// missing UTXO, can not calculate script filter
UtxoMissing(OutPoint),
/// some IO error reading or writing binary serialization of the filter
Io(io::Error),
Io(io::ErrorKind),
}

impl error::Error for Error {}
Expand All @@ -81,14 +81,14 @@ impl Display for Error {
fn fmt(&self, f: &mut Formatter) -> Result<(), fmt::Error> {
match *self {
Error::UtxoMissing(ref coin) => write!(f, "unresolved UTXO {}", coin),
Error::Io(ref io) => write!(f, "{}", io)
Error::Io(ref kind) => write!(f, "{}", io::Error::from(*kind))
}
}
}

impl From<io::Error> for Error {
fn from(io: io::Error) -> Self {
Error::Io(io)
Error::Io(io.kind())
}
}

Expand Down

0 comments on commit cee0a54

Please sign in to comment.