Skip to content

Commit

Permalink
Add helper macro for AddAssigning with saturating arithmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
t-nelson committed Jan 8, 2022
1 parent 299a59f commit a672877
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,34 @@ macro_rules! program_stubs {
() => {};
}

/// Convenience macro for `AddAssign` with saturating arithmetic.
/// Replace by `std::num::Saturating` once stable
#[macro_export]
macro_rules! saturating_add_assign {
($i:expr, $v:expr) => {{
$i = $i.saturating_add($v)
}};
}

#[macro_use]
extern crate serde_derive;
pub extern crate bs58;
extern crate log as logger;

#[macro_use]
extern crate solana_frozen_abi_macro;

#[cfg(test)]
mod tests {
#[test]
fn test_saturating_add_assign() {
let mut i = 0u64;
let v = 1;
saturating_add_assign!(i, v);
assert_eq!(i, 1);

i = u64::MAX;
saturating_add_assign!(i, v);
assert_eq!(i, u64::MAX);
}
}

0 comments on commit a672877

Please sign in to comment.