Use try_from for explicit type conversion (instead of try_into)

This commit is contained in:
Roman Zeyde 2021-09-12 16:17:39 +03:00
parent 1423031931
commit 8d12df353c
3 changed files with 13 additions and 13 deletions

View file

@ -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

View file

@ -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")

View file

@ -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")
}
}