From 3c8c95651169fb9329ceb380162721c2d2f9b3aa Mon Sep 17 00:00:00 2001 From: "Tobin C. Harding" Date: Thu, 12 Dec 2024 10:50:22 +1100 Subject: [PATCH] Remove Weight::from_wu_usize function 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. --- bitcoin/src/blockdata/block.rs | 4 ++-- bitcoin/src/blockdata/transaction.rs | 6 ++++-- units/src/weight.rs | 4 ---- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/bitcoin/src/blockdata/block.rs b/bitcoin/src/blockdata/block.rs index 88971c608..f83cb413e 100644 --- a/bitcoin/src/blockdata/block.rs +++ b/bitcoin/src/blockdata/block.rs @@ -10,7 +10,7 @@ use core::fmt; use hashes::{sha256d, HashEngine}; -use internals::compact_size; +use internals::{compact_size, ToU64}; use io::{BufRead, Write}; use super::Weight; @@ -263,7 +263,7 @@ impl BlockCheckedExt for Block { fn weight(&self) -> Weight { // 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(); - Weight::from_wu_usize(wu) + Weight::from_wu(wu.to_u64()) } fn total_size(&self) -> usize { diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index 35c0ff606..0c49549c4 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -352,7 +352,7 @@ impl TransactionExt for Transaction { fn weight(&self) -> Weight { // 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(); - Weight::from_wu_usize(wu) + Weight::from_wu(wu.to_u64()) } fn base_size(&self) -> usize { @@ -1151,7 +1151,9 @@ impl InputWeightPrediction { /// /// See also [`InputWeightPrediction::total_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) } } diff --git a/units/src/weight.rs b/units/src/weight.rs index 4e0b5026d..8c8827df7 100644 --- a/units/src/weight.rs +++ b/units/src/weight.rs @@ -48,9 +48,6 @@ impl Weight { /// Constructs a new [`Weight`] from weight units. 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. pub fn from_kwu(wu: u64) -> Option { wu.checked_mul(1000).map(Weight) } @@ -254,7 +251,6 @@ mod tests { #[test] fn weight_constructor() { assert_eq!(Weight::ZERO, Weight::from_wu(0)); - assert_eq!(Weight::ZERO, Weight::from_wu_usize(0_usize)); } #[test]