Skip to content

Commit

Permalink
Mark function as const
Browse files Browse the repository at this point in the history
  • Loading branch information
yancyribbens committed Nov 12, 2024
1 parent 73e33e5 commit f0e3ca6
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions units/src/weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,15 @@ 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> {
let weight_unit = vb.checked_mul(Self::WITNESS_SCALE_FACTOR);
// replace with ? when MSRV supports it in const context
// https://github.com/rust-lang/rust/issues/74935
if let Some(wu) = weight_unit {
Some(Weight::from_wu(wu))
} else {
None
}
}

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

0 comments on commit f0e3ca6

Please sign in to comment.