Replace usage of io::Error::new(io::ErrorKind::Other, with io::Error::other

Newly possible with Rust 1.74.
This commit is contained in:
Luke Parker
2023-11-19 18:01:13 -05:00
parent 05b975dff9
commit 797604ad73
25 changed files with 114 additions and 154 deletions

View File

@@ -184,8 +184,7 @@ impl TransactionTrait<Bitcoin> for Transaction {
buf
}
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
Transaction::consensus_decode(reader)
.map_err(|e| io::Error::new(io::ErrorKind::Other, format!("{e}")))
Transaction::consensus_decode(reader).map_err(|e| io::Error::other(format!("{e}")))
}
#[cfg(test)]
@@ -223,12 +222,10 @@ impl EventualityTrait for Eventuality {
}
fn read<R: io::Read>(reader: &mut R) -> io::Result<Self> {
let plan_binding_input = OutPoint::consensus_decode(reader).map_err(|_| {
io::Error::new(io::ErrorKind::Other, "couldn't decode outpoint in eventuality")
})?;
let outputs = Vec::<TxOut>::consensus_decode(reader).map_err(|_| {
io::Error::new(io::ErrorKind::Other, "couldn't decode outputs in eventuality")
})?;
let plan_binding_input = OutPoint::consensus_decode(reader)
.map_err(|_| io::Error::other("couldn't decode outpoint in eventuality"))?;
let outputs = Vec::<TxOut>::consensus_decode(reader)
.map_err(|_| io::Error::other("couldn't decode outputs in eventuality"))?;
Ok(Eventuality { plan_binding_input, outputs })
}
fn serialize(&self) -> Vec<u8> {

View File

@@ -94,7 +94,7 @@ impl OutputType {
1 => OutputType::Branch,
2 => OutputType::Change,
3 => OutputType::Forwarded,
_ => Err(io::Error::new(io::ErrorKind::Other, "invalid OutputType"))?,
_ => Err(io::Error::other("invalid OutputType"))?,
})
}
}

View File

@@ -34,7 +34,7 @@ impl<N: Network> Payment<N> {
.address
.clone()
.try_into()
.map_err(|_| io::Error::new(io::ErrorKind::Other, "address couldn't be serialized"))?;
.map_err(|_| io::Error::other("address couldn't be serialized"))?;
writer.write_all(&u32::try_from(address.len()).unwrap().to_le_bytes())?;
writer.write_all(&address)?;
@@ -52,8 +52,7 @@ impl<N: Network> Payment<N> {
reader.read_exact(&mut buf)?;
let mut address = vec![0; usize::try_from(u32::from_le_bytes(buf)).unwrap()];
reader.read_exact(&mut address)?;
let address = N::Address::try_from(address)
.map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid address"))?;
let address = N::Address::try_from(address).map_err(|_| io::Error::other("invalid address"))?;
let mut buf = [0; 1];
reader.read_exact(&mut buf)?;
@@ -68,7 +67,7 @@ impl<N: Network> Payment<N> {
};
let balance = Balance::decode(&mut scale::IoReader(reader))
.map_err(|_| io::Error::new(io::ErrorKind::Other, "invalid balance"))?;
.map_err(|_| io::Error::other("invalid balance"))?;
Ok(Payment { address, data, balance })
}
@@ -152,13 +151,10 @@ impl<N: Network> Plan<N> {
// TODO: Have Plan construction fail if change cannot be serialized
let change = if let Some(change) = &self.change {
change.clone().try_into().map_err(|_| {
io::Error::new(
io::ErrorKind::Other,
format!(
"an address we said to use as change couldn't be convered to a Vec<u8>: {}",
change.to_string(),
),
)
io::Error::other(format!(
"an address we said to use as change couldn't be convered to a Vec<u8>: {}",
change.to_string(),
))
})?
} else {
vec![]
@@ -188,16 +184,14 @@ impl<N: Network> Plan<N> {
reader.read_exact(&mut len)?;
let mut change = vec![0; usize::from(len[0])];
reader.read_exact(&mut change)?;
let change = if change.is_empty() {
None
} else {
Some(N::Address::try_from(change).map_err(|_| {
io::Error::new(
io::ErrorKind::Other,
"couldn't deserialize an Address serialized into a Plan",
)
})?)
};
let change =
if change.is_empty() {
None
} else {
Some(N::Address::try_from(change).map_err(|_| {
io::Error::other("couldn't deserialize an Address serialized into a Plan")
})?)
};
Ok(Plan { key, inputs, payments, change })
}