Merge rust-bitcoin/rust-bitcoin#3738: Remove Weight::from_wu_usize function
3c8c956511
Remove Weight::from_wu_usize function (Tobin C. Harding)
Pull request description:
This constructor is an anomaly in this repo. Also it is ugly to have the type parameter hard coded in the function name.
The problem the constructor is trying to solve is that we don't like casts that don't obviously loose data. We have a solution for that already in `internals::ToU64`. This trait cannot be used in const contexts though so we do introduce a single cast in this patch.
ACKs for top commit:
apoelstra:
ACK 3c8c95651169fb9329ceb380162721c2d2f9b3aa; successfully ran local tests
Tree-SHA512: 168196edb7c151378d425b96ea3c9cdb0a4f6a879543e89facd02ed1fdf9bc69bde8ef862ffa0959b7c5ca21d6f4fe5ae38a933c379e7e88a946ca7cb68d61ec
This commit is contained in:
commit
ee8430a527
|
@ -10,7 +10,7 @@
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
|
|
||||||
use hashes::{sha256d, HashEngine};
|
use hashes::{sha256d, HashEngine};
|
||||||
use internals::compact_size;
|
use internals::{compact_size, ToU64};
|
||||||
use io::{BufRead, Write};
|
use io::{BufRead, Write};
|
||||||
|
|
||||||
use super::Weight;
|
use super::Weight;
|
||||||
|
@ -263,7 +263,7 @@ impl BlockCheckedExt for Block<Checked> {
|
||||||
fn weight(&self) -> Weight {
|
fn weight(&self) -> Weight {
|
||||||
// This is the exact definition of a weight unit, as defined by BIP-141 (quote above).
|
// This is the exact definition of a weight unit, as defined by BIP-141 (quote above).
|
||||||
let wu = block_base_size(self.transactions()) * 3 + self.total_size();
|
let wu = block_base_size(self.transactions()) * 3 + self.total_size();
|
||||||
Weight::from_wu_usize(wu)
|
Weight::from_wu(wu.to_u64())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn total_size(&self) -> usize {
|
fn total_size(&self) -> usize {
|
||||||
|
|
|
@ -343,7 +343,7 @@ impl TransactionExt for Transaction {
|
||||||
fn weight(&self) -> Weight {
|
fn weight(&self) -> Weight {
|
||||||
// This is the exact definition of a weight unit, as defined by BIP-141 (quote above).
|
// This is the exact definition of a weight unit, as defined by BIP-141 (quote above).
|
||||||
let wu = self.base_size() * 3 + self.total_size();
|
let wu = self.base_size() * 3 + self.total_size();
|
||||||
Weight::from_wu_usize(wu)
|
Weight::from_wu(wu.to_u64())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn base_size(&self) -> usize {
|
fn base_size(&self) -> usize {
|
||||||
|
@ -1142,7 +1142,9 @@ impl InputWeightPrediction {
|
||||||
///
|
///
|
||||||
/// See also [`InputWeightPrediction::total_weight`]
|
/// See also [`InputWeightPrediction::total_weight`]
|
||||||
pub const fn witness_weight(&self) -> Weight {
|
pub const fn witness_weight(&self) -> Weight {
|
||||||
Weight::from_wu_usize(self.script_size * 4 + self.witness_size)
|
let wu = self.script_size * 4 + self.witness_size;
|
||||||
|
let wu = wu as u64; // Can't use `ToU64` in const context.
|
||||||
|
Weight::from_wu(wu)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -48,9 +48,6 @@ impl Weight {
|
||||||
/// Constructs a new [`Weight`] from weight units.
|
/// Constructs a new [`Weight`] from weight units.
|
||||||
pub const fn from_wu(wu: u64) -> Self { Weight(wu) }
|
pub const fn from_wu(wu: u64) -> Self { Weight(wu) }
|
||||||
|
|
||||||
/// Constructs a new [`Weight`] from usize weight units.
|
|
||||||
pub const fn from_wu_usize(wu: usize) -> Self { Weight(wu as u64) }
|
|
||||||
|
|
||||||
/// Constructs a new [`Weight`] from kilo weight units returning [`None`] if an overflow occurred.
|
/// Constructs a new [`Weight`] from kilo weight units returning [`None`] if an overflow occurred.
|
||||||
pub fn from_kwu(wu: u64) -> Option<Self> { wu.checked_mul(1000).map(Weight) }
|
pub fn from_kwu(wu: u64) -> Option<Self> { wu.checked_mul(1000).map(Weight) }
|
||||||
|
|
||||||
|
@ -254,7 +251,6 @@ mod tests {
|
||||||
#[test]
|
#[test]
|
||||||
fn weight_constructor() {
|
fn weight_constructor() {
|
||||||
assert_eq!(Weight::ZERO, Weight::from_wu(0));
|
assert_eq!(Weight::ZERO, Weight::from_wu(0));
|
||||||
assert_eq!(Weight::ZERO, Weight::from_wu_usize(0_usize));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|
Loading…
Reference in New Issue