generic_json/
number.rs

1/// JSON number.
2pub trait Number: Eq {
3	/// Returns this number as an `u32` if it can be exactly represented as such.
4	fn as_u32(&self) -> Option<u32>;
5
6	/// Returns this number as an `u64` if it can be exactly represented as such.
7	fn as_u64(&self) -> Option<u64>;
8
9	/// Returns this number as an `i32` if it can be exactly represented as such.
10	fn as_i32(&self) -> Option<i32>;
11
12	/// Returns this number as an `i64` if it can be exactly represented as such.
13	fn as_i64(&self) -> Option<i64>;
14
15	/// Returns this number as an `f32` if it can be exactly represented as such.
16	fn as_f32(&self) -> Option<f32>;
17
18	/// Returns this number as an `f32`, potentially losing precision in the process.
19	fn as_f32_lossy(&self) -> f32;
20
21	/// Returns this number as an `f64` if it can be exactly represented as such.
22	fn as_f64(&self) -> Option<f64>;
23
24	/// Returns this number as an `f64`, potentially losing precision in the process.
25	fn as_f64_lossy(&self) -> f64;
26}
27
28/// Zero number.
29///
30/// This is a dummy number type that can only represent the value `0.0`.
31#[derive(PartialEq, Eq, Hash, PartialOrd, Ord, Debug)]
32pub struct Zero;
33
34impl Number for Zero {
35	fn as_u32(&self) -> Option<u32> {
36		None
37	}
38
39	fn as_u64(&self) -> Option<u64> {
40		None
41	}
42
43	fn as_i32(&self) -> Option<i32> {
44		None
45	}
46
47	fn as_i64(&self) -> Option<i64> {
48		None
49	}
50
51	fn as_f32(&self) -> Option<f32> {
52		Some(0.0)
53	}
54
55	fn as_f32_lossy(&self) -> f32 {
56		0.0
57	}
58
59	fn as_f64(&self) -> Option<f64> {
60		Some(0.0)
61	}
62
63	fn as_f64_lossy(&self) -> f64 {
64		0.0
65	}
66}