1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use serde::{de, ser};
use std::convert::{TryFrom, TryInto};
use std::error::Error;
use std::fmt;
use std::num::{ParseIntError, TryFromIntError};
use std::ops::Deref;
use std::str::FromStr;
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct SafeLong(i64);
impl SafeLong {
#[inline]
pub fn min_value() -> SafeLong {
SafeLong(-(1 << 53) + 1)
}
#[inline]
pub fn max_value() -> SafeLong {
SafeLong((1 << 53) - 1)
}
#[inline]
pub fn new(value: i64) -> Result<SafeLong, BoundsError> {
if value >= *SafeLong::min_value() && value <= *SafeLong::max_value() {
Ok(SafeLong(value))
} else {
Err(BoundsError(()))
}
}
}
impl Deref for SafeLong {
type Target = i64;
#[inline]
fn deref(&self) -> &i64 {
&self.0
}
}
impl fmt::Display for SafeLong {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, fmt)
}
}
impl FromStr for SafeLong {
type Err = ParseError;
#[inline]
fn from_str(s: &str) -> Result<SafeLong, ParseError> {
let n = s
.parse()
.map_err(|e| ParseError(ParseErrorInner::Parse(e)))?;
SafeLong::new(n).map_err(|e| ParseError(ParseErrorInner::Bounds(e)))
}
}
impl ser::Serialize for SafeLong {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: ser::Serializer,
{
s.serialize_i64(self.0)
}
}
impl<'de> de::Deserialize<'de> for SafeLong {
fn deserialize<D>(d: D) -> Result<SafeLong, D::Error>
where
D: de::Deserializer<'de>,
{
let value = i64::deserialize(d)?;
SafeLong::new(value)
.map_err(|_| de::Error::invalid_value(de::Unexpected::Signed(value), &"a safe long"))
}
}
macro_rules! impl_from {
($($t:ty),*) => {
$(
impl From<$t> for SafeLong {
#[inline]
fn from(n: $t) -> SafeLong {
SafeLong(i64::from(n))
}
}
)*
}
}
impl_from!(u8, i8, u16, i16, u32, i32);
macro_rules! impl_into {
($($t:ty),*) => {
$(
impl From<SafeLong> for $t {
#[inline]
fn from(n: SafeLong) -> $t {
n.0.into()
}
}
)*
}
}
impl_into!(i64, i128);
macro_rules! impl_try_from {
($($t:ty),*) => {
$(
impl TryFrom<$t> for SafeLong {
type Error = BoundsError;
#[inline]
fn try_from(n: $t) -> Result<SafeLong, BoundsError> {
i64::try_from(n)
.map_err(|_| BoundsError(()))
.and_then(SafeLong::new)
}
}
)*
}
}
impl_try_from!(u64, i64, u128, i128, usize, isize);
macro_rules! impl_try_into {
($($t:ty),*) => {
$(
impl TryFrom<SafeLong> for $t {
type Error = TryFromIntError;
#[inline]
fn try_from(n: SafeLong) -> Result<$t, TryFromIntError> {
n.0.try_into()
}
}
)*
};
}
impl_try_into!(u8, i8, u16, i16, u32, i32, u64, u128, usize, isize);
#[derive(Debug, Clone)]
pub struct BoundsError(());
impl fmt::Display for BoundsError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("value was out of bounds of a safe long")
}
}
impl Error for BoundsError {}
#[derive(Debug, Clone)]
enum ParseErrorInner {
Parse(ParseIntError),
Bounds(BoundsError),
}
#[derive(Debug, Clone)]
pub struct ParseError(ParseErrorInner);
impl fmt::Display for ParseError {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
match &self.0 {
ParseErrorInner::Parse(e) => fmt::Display::fmt(e, fmt),
ParseErrorInner::Bounds(e) => fmt::Display::fmt(e, fmt),
}
}
}
impl Error for ParseError {}