Skip to content

Commit

Permalink
Remove usage of try! macro, use ? instead (#3097)
Browse files Browse the repository at this point in the history
We converted the major part of the code a while ago, but some occurences were left. It's a compiler warning on nightly already.
  • Loading branch information
hashmap authored and quentinlesceller committed Oct 21, 2019
1 parent b4e42be commit 8a7da89
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
14 changes: 7 additions & 7 deletions core/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,30 +66,30 @@ macro_rules! tee {
/// Eliminate some of the boilerplate of deserialization (package ser) by
/// passing just the list of reader function (with optional single param)
/// Example before:
/// let foo = try!(reader.read_u64());
/// let bar = try!(reader.read_u32());
/// let fixed_byte_var = try!(reader.read_fixed_bytes(64));
/// let foo = reader.read_u64()?;
/// let bar = reader.read_u32()?;
/// let fixed_byte_var = reader.read_fixed_bytes(64)?;
/// Example after:
/// let (foo, bar, fixed_byte_var) = ser_multiread!(reader, read_u64,
/// read_u32, read_fixed_bytes(64));
#[macro_export]
macro_rules! ser_multiread {
($rdr:ident, $($read_call:ident $(($val:expr)),*),*) => {
( $(r#try!($rdr.$read_call($($val),*))),* )
( $($rdr.$read_call($($val),*)?),* )
}
}

/// Eliminate some of the boilerplate of serialization (package ser) by
/// passing directly pairs of writer function and data to write.
/// Example before:
/// try!(reader.write_u64(42));
/// try!(reader.write_u32(100));
/// reader.write_u64(42)?;
/// reader.write_u32(100)?;
/// Example after:
/// ser_multiwrite!(writer, [write_u64, 42], [write_u32, 100]);
#[macro_export]
macro_rules! ser_multiwrite {
($wrtr:ident, $([ $write_call:ident, $val:expr ]),* ) => {
$( r#try!($wrtr.$write_call($val)) );*
$($wrtr.$write_call($val)? );*
}
}

Expand Down
6 changes: 3 additions & 3 deletions core/tests/vec_backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ impl PMMRable for TestElem {

impl Writeable for TestElem {
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ser::Error> {
r#try!(writer.write_u32(self.0[0]));
r#try!(writer.write_u32(self.0[1]));
r#try!(writer.write_u32(self.0[2]));
writer.write_u32(self.0[0])?;
writer.write_u32(self.0[1])?;
writer.write_u32(self.0[2])?;
writer.write_u32(self.0[3])
}
}
Expand Down
7 changes: 3 additions & 4 deletions keychain/src/extkey_bip32.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl BIP32Hasher for BIP32GrinHasher {
b"IamVoldemort".to_owned()
}
fn init_sha512(&mut self, seed: &[u8]) {
self.hmac_sha512 = HmacSha512::new_varkey(seed).expect("HMAC can take key of any size");;
self.hmac_sha512 = HmacSha512::new_varkey(seed).expect("HMAC can take key of any size");
}
fn append_sha512(&mut self, value: &[u8]) {
self.hmac_sha512.input(value);
Expand Down Expand Up @@ -385,7 +385,7 @@ impl ExtendedPrivKey {
Err(e) => return Err(Error::MnemonicError(e)),
};
let mut hasher = BIP32GrinHasher::new(is_floo);
let key = r#try!(ExtendedPrivKey::new_master(secp, &mut hasher, &seed));
let key = ExtendedPrivKey::new_master(secp, &mut hasher, &seed)?;
Ok(key)
}

Expand Down Expand Up @@ -716,7 +716,7 @@ mod tests {
b"Bitcoin seed".to_owned()
}
fn init_sha512(&mut self, seed: &[u8]) {
self.hmac_sha512 = HmacSha512::new_varkey(seed).expect("HMAC can take key of any size");;
self.hmac_sha512 = HmacSha512::new_varkey(seed).expect("HMAC can take key of any size");
}
fn append_sha512(&mut self, value: &[u8]) {
self.hmac_sha512.input(value);
Expand Down Expand Up @@ -900,5 +900,4 @@ mod tests {
serde_round_trip!(ChildNumber::from_hardened_idx(1));
serde_round_trip!(ChildNumber::from_hardened_idx((1 << 31) - 1));
}

}
7 changes: 5 additions & 2 deletions keychain/src/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,10 @@ pub fn to_entropy(mnemonic: &str) -> Result<Vec<u8>, Error> {
}

// u11 vector of indexes for each word
let mut indexes: Vec<u16> = r#try!(words.iter().map(|x| search(x)).collect());
let mut indexes: Vec<u16> = words
.iter()
.map(|x| search(x))
.collect::<Result<Vec<_>, _>>()?;
let checksum_bits = words.len() / 3;
let mask = ((1 << checksum_bits) - 1) as u8;
let last = indexes.pop().unwrap();
Expand Down Expand Up @@ -155,7 +158,7 @@ where
Option<&'a str>: From<T>,
{
// make sure the mnemonic is valid
r#try!(to_entropy(mnemonic));
to_entropy(mnemonic)?;

let salt = ("mnemonic".to_owned() + Option::from(passphrase).unwrap_or("")).into_bytes();
let data = mnemonic.as_bytes();
Expand Down
4 changes: 2 additions & 2 deletions keychain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,8 @@ impl AsRef<[u8]> for Identifier {

impl ::std::fmt::Debug for Identifier {
fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
r#try!(write!(f, "{}(", stringify!(Identifier)));
r#try!(write!(f, "{}", self.to_hex()));
write!(f, "{}(", stringify!(Identifier))?;
write!(f, "{}", self.to_hex())?;
write!(f, ")")
}
}
Expand Down

0 comments on commit 8a7da89

Please sign in to comment.