Crates.io | num-sign |
lib.rs | num-sign |
version | 0.1.2 |
source | src |
created_at | 2022-12-03 06:13:56.822335 |
updated_at | 2022-12-15 15:27:11.731736 |
description | enum Sign { Positive = 1, Negative = -1 } |
homepage | |
repository | https://github.com/u1roh/num-sign |
max_upload_size | |
id | 728928 |
size | 13,966 |
Sign
enum#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum Sign {
Positive = 1,
Negative = -1,
}
pub use Sign::*;
Signed
traitpub 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
featureCargo.toml:
[dependencies]
num-sign = { version = "...", features = ["serde"] }
Test:
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());