Skip to content

Commit

Permalink
Merge rust-bitcoin#3605: Mark function as const
Browse files Browse the repository at this point in the history
1db4b72 Mark function as const (yancy)

Pull request description:

  We discussed marking from_vb as const here: rust-bitcoin#3602

  However, from what I can tell, map() isn't const and I don't see a tracking issue for changing it.  Also, the question mark operator isn't available in const context either, although there is a tracking issue for that: rust-lang/rust#74935.  It will be a long while before that makes it into this projects MSRV if/when it lands.

  There are some other functions in this module that could use the same re-write to make them const as well it looks like.

ACKs for top commit:
  tcharding:
    ACK 1db4b72
  apoelstra:
    ACK 1db4b72; successfully ran local tests

Tree-SHA512: 62b612791dd3ce2f6ebf27f586a1262633a46566b9fd3583984171f62209714ad979439345fe86d8ef87d2f78a2cee21d619e2eb3621689faf59d81640e9f77c
  • Loading branch information
apoelstra committed Nov 13, 2024
2 parents 7085223 + 1db4b72 commit a9bc1c7
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions units/src/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,12 @@ impl Weight {
pub fn from_kwu(wu: u64) -> Option<Self> { wu.checked_mul(1000).map(Weight) }

/// Constructs a new [`Weight`] from virtual bytes, returning [`None`] if an overflow occurred.
pub fn from_vb(vb: u64) -> Option<Self> {
vb.checked_mul(Self::WITNESS_SCALE_FACTOR).map(Weight::from_wu)
pub const fn from_vb(vb: u64) -> Option<Self> {
// No `map()` in const context.
match vb.checked_mul(Self::WITNESS_SCALE_FACTOR) {
Some(wu) => Some(Weight::from_wu(wu)),
None => None,
}
}

/// Constructs a new [`Weight`] from virtual bytes panicking if an overflow occurred.
Expand Down

0 comments on commit a9bc1c7

Please sign in to comment.