2434afc40d Make Weigth::from_kwu const (Tobin C. Harding)
1fe984552e Improve Weight rustdocs (Tobin C. Harding)

Pull request description:

  Do two minor improvements to the `weight` module. Docs and add `const`.

ACKs for top commit:
  apoelstra:
    ACK 2434afc40dadd213f8695c5ce91d397c03f337be; successfully ran local tests
  Kixunil:
    ACK 2434afc40d

Tree-SHA512: e3e9653d5fcd060c27a2313e642d7b96f51b9342953505a30a9748cb7f0c19a87bcb1faadb1b07ecc770aaec45496e5a750ac48e3e9141e379c554f0875df6a1
This commit is contained in:
merge-script 2025-03-06 15:58:59 +00:00
commit 181b7482ca
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 9 additions and 3 deletions

View File

@ -12,10 +12,10 @@ use serde::{Deserialize, Serialize};
/// The factor that non-witness serialization data is multiplied by during weight calculation.
pub const WITNESS_SCALE_FACTOR: usize = 4;
/// Represents block weight - the weight of a transaction or block.
/// Represents weight - the weight of a transaction or block.
///
/// This is an integer newtype representing [`Weight`] in `wu`. It provides protection against mixing
/// up the types as well as basic formatting features.
/// up types as well as basic formatting features.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
@ -48,7 +48,13 @@ impl Weight {
pub const fn from_wu(wu: u64) -> Self { Weight(wu) }
/// 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 const fn from_kwu(wu: u64) -> Option<Self> {
// No `map()` in const context.
match wu.checked_mul(1000) {
Some(wu) => Some(Weight::from_wu(wu)),
None => None,
}
}
/// Constructs a new [`Weight`] from virtual bytes, returning [`None`] if an overflow occurred.
pub const fn from_vb(vb: u64) -> Option<Self> {