From 8d12df353c2ec3da385aa1a60dc71faafacfdab3 Mon Sep 17 00:00:00 2001 From: Roman Zeyde Date: Sun, 12 Sep 2021 16:17:39 +0300 Subject: [PATCH] Use `try_from` for explicit type conversion (instead of `try_into`) --- src/mempool.rs | 4 ++-- src/tracker.rs | 4 ++-- src/types.rs | 18 +++++++++--------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/mempool.rs b/src/mempool.rs index 362229e..fc1954b 100644 --- a/src/mempool.rs +++ b/src/mempool.rs @@ -1,7 +1,7 @@ use anyhow::Result; use std::collections::{BTreeSet, HashMap, HashSet}; -use std::convert::TryInto; +use std::convert::TryFrom; use std::iter::FromIterator; use std::ops::Bound; @@ -180,7 +180,7 @@ impl Histogram { let mut bins = [0; Self::SIZE]; for (fee, vsize) in items { let fee_rate = fee.as_sat() / vsize; - let index: usize = fee_rate.leading_zeros().try_into().unwrap(); + let index = usize::try_from(fee_rate.leading_zeros()).unwrap(); // skip transactions with too low fee rate (<1 sat/vB) if let Some(bin) = bins.get_mut(index) { *bin += vsize diff --git a/src/tracker.rs b/src/tracker.rs index 177fedf..7bdaedd 100644 --- a/src/tracker.rs +++ b/src/tracker.rs @@ -1,7 +1,7 @@ use anyhow::{Context, Result}; use bitcoin::{BlockHash, OutPoint, Txid}; -use std::convert::TryInto; +use std::convert::TryFrom; use std::path::Path; use crate::{ @@ -80,7 +80,7 @@ impl Tracker { let get_amount_fn = |outpoint: OutPoint| { cache .get_tx(&outpoint.txid, |tx| { - let vout: usize = outpoint.vout.try_into().unwrap(); + let vout = usize::try_from(outpoint.vout).unwrap(); bitcoin::Amount::from_sat(tx.output[vout].value) }) .expect("missing tx") diff --git a/src/types.rs b/src/types.rs index 1f21126..812d7b4 100644 --- a/src/types.rs +++ b/src/types.rs @@ -1,6 +1,6 @@ use anyhow::Result; -use std::convert::TryInto; +use std::convert::TryFrom; use bitcoin::{ consensus::encode::{deserialize, serialize, Decodable, Encodable}, @@ -84,7 +84,7 @@ impl ScriptHashRow { pub(crate) fn new(scripthash: ScriptHash, height: usize) -> Self { Self { prefix: scripthash.prefix(), - height: height.try_into().expect("invalid height"), + height: Height::try_from(height).expect("invalid height"), } } @@ -97,7 +97,7 @@ impl ScriptHashRow { } pub(crate) fn height(&self) -> usize { - self.height.try_into().expect("invalid height") + usize::try_from(self.height).expect("invalid height") } } @@ -121,8 +121,8 @@ struct SpendingPrefix { impl_consensus_encoding!(SpendingPrefix, prefix); fn spending_prefix(prev: OutPoint) -> SpendingPrefix { - let txid_prefix = &prev.txid[..HASH_PREFIX_LEN]; - let value = u64::from_be_bytes(txid_prefix.try_into().unwrap()); + let txid_prefix = <[u8; HASH_PREFIX_LEN]>::try_from(&prev.txid[..HASH_PREFIX_LEN]).unwrap(); + let value = u64::from_be_bytes(txid_prefix); let value = value.wrapping_add(prev.vout.into()); SpendingPrefix { prefix: value.to_be_bytes(), @@ -145,7 +145,7 @@ impl SpendingPrefixRow { pub(crate) fn new(outpoint: OutPoint, height: usize) -> Self { Self { prefix: spending_prefix(outpoint), - height: height.try_into().expect("invalid height"), + height: Height::try_from(height).expect("invalid height"), } } @@ -158,7 +158,7 @@ impl SpendingPrefixRow { } pub(crate) fn height(&self) -> usize { - self.height.try_into().expect("invalid height") + usize::try_from(self.height).expect("invalid height") } } @@ -193,7 +193,7 @@ impl TxidRow { pub(crate) fn new(txid: Txid, height: usize) -> Self { Self { prefix: txid_prefix(&txid), - height: height.try_into().expect("invalid height"), + height: Height::try_from(height).expect("invalid height"), } } @@ -206,7 +206,7 @@ impl TxidRow { } pub(crate) fn height(&self) -> usize { - self.height.try_into().expect("invalid height") + usize::try_from(self.height).expect("invalid height") } }