3个版本
0.1.2 | 2022年12月15日 |
---|---|
0.1.1 | 2022年12月3日 |
0.1.0 | 2022年12月3日 |
1188 在 Rust模式
10KB
165 行
符号类型
Sign
枚举
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Sign {
Positive = 1,
Negative = -1,
}
pub use Sign::*;
Signed
特性
pub trait Signed {
fn sign(&self) -> Option<Sign>;
}
测试
use num_sign::*;
assert_eq!(Positive as i32, 1);
assert_eq!(Negative as i32, -1);
assert!(Negative < Positive);
assert_eq!(Positive.cmp(&Negative), std::cmp::Ordering::Greater);
assert_eq!(1_i32, Positive.into());
assert_eq!(-1_i32, Negative.into());
assert_eq!(1.0, Positive.into());
assert_eq!(-1.0, Negative.into());
assert_eq!(-Positive, Negative);
assert_eq!(Positive, -Negative);
assert_eq!(Positive * Positive, Positive);
assert_eq!(Positive * Negative, Negative);
assert_eq!(Negative * Positive, Negative);
assert_eq!(Negative * Negative, Positive);
assert_eq!(Positive * 123, 123);
assert_eq!(Negative * 123, -123);
assert_eq!(Positive * 3.14, 3.14);
assert_eq!(Negative * 3.14, -3.14);
assert_eq!((123).sign(), Some(Positive));
assert_eq!((-123).sign(), Some(Negative));
assert_eq!((0).sign(), None);
assert_eq!((3.14).sign(), Some(Positive));
assert_eq!((-3.14).sign(), Some(Negative));
assert_eq!((0.0).sign(), Some(Positive));
assert_eq!((-0.0).sign(), Some(Negative));
assert_eq!(std::f64::NAN.sign(), None);
assert_eq!(std::f64::INFINITY.sign(), Some(Positive));
assert_eq!(std::f64::NEG_INFINITY.sign(), Some(Negative));
assert_eq!("+".parse::<Sign>().unwrap(), Positive);
assert_eq!("-".parse::<Sign>().unwrap(), Negative);
assert_eq!(Positive.to_string(), "+");
assert_eq!(Negative.to_string(), "-");
serde
功能
Cargo.toml
[dependencies]
num-sign = { version = "...", features = ["serde"] }
测试
assert_eq!(serde_json::to_string(&Positive).unwrap(), "\"+\"");
assert_eq!(serde_json::to_string(&Negative).unwrap(), "\"-\"");
assert_eq!(Positive, serde_json::from_str("\"+\"").unwrap());
assert_eq!(Negative, serde_json::from_str("\"-\"").unwrap());
依赖
~175KB