Merge rust-bitcoin/rust-bitcoin#3605: Mark function as const

1db4b723dc Mark function as const (yancy)

Pull request description:

  We discussed marking from_vb as const here: https://github.com/rust-bitcoin/rust-bitcoin/issues/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: https://github.com/rust-lang/rust/issues/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 1db4b723dc
  apoelstra:
    ACK 1db4b723dc59568c14c76598e7c63c58651ed1cd; successfully ran local tests

Tree-SHA512: 62b612791dd3ce2f6ebf27f586a1262633a46566b9fd3583984171f62209714ad979439345fe86d8ef87d2f78a2cee21d619e2eb3621689faf59d81640e9f77c
This commit is contained in:
merge-script 2024-11-13 02:01:09 +00:00
commit a9bc1c7db5
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
1 changed files with 6 additions and 2 deletions

View File

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