3个不稳定版本
0.2.1 | 2019年8月2日 |
---|---|
0.2.0 | 2019年8月2日 |
0.1.0 | 2019年7月27日 |
#22 in #overflow
10KB
159 行
SCalc
一种确保计算安全并防止自身溢出的类型
入门
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(())
}
您还可以结合使用 新类型惯用语法 和 derive_more
来获得更好的体验。
lib.rs
:
一种确保计算安全并防止自身溢出的类型
入门
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(())
}
您还可以结合使用 新类型惯用语法 和 derive_more
来获得更好的体验。
依赖项
~165KB