scalc

Crates.ioscalc
lib.rsscalc
version0.2.1
sourcesrc
created_at2019-07-27 11:36:33.408238
updated_at2019-08-02 05:23:51.024737
descriptionA type that ensures calculation safety and prevents itself from overflow
homepage
repositoryhttps://github.com/XCH-CEB/xch-project
max_upload_size
id152037
size10,663
Ying Kanyang (Harry Ying) (LEXUGE)

documentation

README

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.

Commit count: 91

cargo fmt