solana-math
Checked math and numeric conversion traits that return explicit errors instead of panicking.
What it provides
SafeMath: checkedadd/sub/mul/divSafeMathAssign: checked*_assignhelpersSafeConvert: checked integer conversionsSafeMathErrorwithOverflow,Underflow, andConversionFailed
Install
Features
Numeric trait impls are gated by features:
- Unsigned:
u8,u16,u32,u64,u128 u256,u512: enableruint::aliases::{U256, U512}support- Signed:
i8,i16,i32,i64,i128 decimal: enablerust_decimal::Decimalsupportfix: enable no-stdfix::Fixsupport (requires nightly Rust)basis-points: enablebasis_points::BasisPointssupportbrine-fp: enablebrine_fp::{UnsignedNumeric, SignedNumeric}supportanchor: convertSafeMathErrorinto AnchorProgramError/Errorquasar: convertSafeMathErrorinto QuasarProgramError, and enablezeropodPOD integer safe math
Example
use SafeMath;
let x: u64 = 10;
assert_eq!;
# Ok::