Skip to content

Instantly share code, notes, and snippets.

@benluelo
Last active January 29, 2023 23:28
Show Gist options
  • Save benluelo/aa63c166398f5f74b123081ed7da7e58 to your computer and use it in GitHub Desktop.
Save benluelo/aa63c166398f5f74b123081ed7da7e58 to your computer and use it in GitHub Desktop.
Workaround for https://github.com/rust-lang/rust/issues/74935 to reduce the boilerplate of manual match statements.
// `?` is still unstable in const contexts :(
macro_rules! option_try {
($($opt:tt)+) => {
match $($opt)+ {
Some(t) => t,
None => return None,
}
};
}
impl Weight {
/// Checked [`Weight`] addition. Computes `self + rhs`, returning `None` if overflow occurred.
pub const fn checked_add(&self, rhs: &Self) -> Option<Self> {
Some(Self {
ref_time: option_try!(self.ref_time.checked_add(rhs.ref_time)),
proof_size: option_try!(self.proof_size.checked_add(rhs.proof_size)),
})
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment