#math #overflow #operation #enums #fields #macro #variant

safe_math

Math without overflow, creates an enum and a macro to help mitigate any overflow while multiplying and adding

3 releases (1 stable)

1.1.1 Oct 8, 2024
1.1.0 Oct 6, 2024
0.2.2 Sep 18, 2024
0.1.1 May 25, 2023

#382 in Rust patterns

30 downloads per month

GPL-3.0-only

1MB
135 lines

Contains (WOFF font, 400KB) NanumBarunGothic-0f09457c7a19b7c6.ttf.woff2, (WOFF font, 135KB) FiraSans-Medium-8f9a781e4970d388.woff2, (WOFF font, 130KB) FiraSans-Regular-018c141bf0843ffd.woff2, (WOFF font, 82KB) SourceSerif4-Bold-a2c9cd1067f8b328.ttf.woff2, (WOFF font, 77KB) SourceSerif4-Regular-46f98efaafac5295.ttf.woff2, (WOFF font, 45KB) SourceCodePro-It-1cc31594bf4f1f79.ttf.woff2 and 3 more.

Safe Math

Math without overflow!

Adds a macro and an enum safe_math!() and Operation

src/safe_math.rs

Operation:

pub enum Operation {
  ...
}

Has two variants Add and Multiply with no fields, used in the safe_math!() macro to determine whether you are adding or multiplying the inputted numbers

safe_math!():

#[macro_export]
macro_rules! safe_math {
  ($in_x:expr, $in_y:expr, $operation:expr)
}

This takes three expression arguments, the first two being the numbers you wish to use and the third being the operation you wish to apply to the numbers whether that be addition or multiplication. The first two expressions should be below the unsigned 64 bit integer limit and the third should be a variant of the Operation enum. Most errors will just return the larger of the two number input variables in the Err() variant of the Result enum

No runtime deps