Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add saturating accrue
  • Loading branch information
shawntabrizi committed Aug 31, 2022
commit d42dae3e3ff8e7cf3a462363f0c93c3032a7aa75
7 changes: 7 additions & 0 deletions frame/support/src/weights/weight_v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,13 @@ impl Weight {
Self { ref_time: self.ref_time.saturating_pow(exp) }
}

/// Increment [`Weight`] by `amount` via saturating addition.
pub fn saturating_accrue(&mut self, amount: Self) {
let mut o = Self::zero();
sp_std::mem::swap(&mut o, self);
*self = o.saturating_add(amount);
}

/// Checked [`Weight`] addition. Computes `self + rhs`, returning `None` if overflow occurred.
pub const fn checked_add(&self, rhs: &Self) -> Option<Self> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you use standalone functions instead of implementing Checked and Saturating traits?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i do implement the checked trait below (where it was possible), but these are const functions.

Saturating implies too many things, and it did not make sense for me to implement all of them

match self.ref_time.checked_add(rhs.ref_time) {
Expand Down