#calculations #overflow #ensure #safety #s-cell

scalc

A type that ensures calculation safety and prevents itself from overflow

3 unstable releases

0.2.1 Aug 2, 2019
0.2.0 Aug 2, 2019
0.1.0 Jul 27, 2019

#18 in #overflow

43 downloads per month

GPL-3.0 license

10KB
159 lines

SCalc

A type that ensures calculation safety and prevents itself from overflow

Get started

use scalc::SCell;

fn main() -> Result<(), String> {
    let a = SCell::<i32>::new(12) * SCell::<i32>::new(3);
    assert_eq!(*a.ok_or("overflow")?.get_data(), 36);

    // Addition will result in `None` in the presence of overflow behavior(s)
    let a = SCell::<i32>::new(std::i32::MAX) + SCell::<i32>::new(1);
    assert!(a.is_none());
    Ok(())
}

You can also use New Type Idiom in combination of derive_more to have better experience.


lib.rs:

A type that ensures calculation safety and prevents itself from overflow

Get started

use scalc::SCell;

fn main() -> Result<(), String> {
   let a = SCell::<i32>::new(12) * SCell::<i32>::new(3);
   assert_eq!(*a.ok_or("overflow")?.get_data(), 36);

   // Addition will result in `None` in the presence of overflow behavior(s)
   let a = SCell::<i32>::new(std::i32::MAX) + SCell::<i32>::new(1);
   assert!(a.is_none());
   Ok(())
}

You can also use New Type Idiom in combination of derive_more to have better experience.

Dependencies

~165KB