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
use std::fmt;
use std::ops::Deref;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Hash, Debug, Clone, Copy, PartialEq, Eq)]
pub struct ID(pub i64);
impl fmt::Display for ID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl ID {
pub fn as_int(self) -> i64 {
self.0
}
}
impl From<i32> for ID {
fn from(x: i32) -> Self {
ID(i64::from(x))
}
}
impl From<i64> for ID {
fn from(x: i64) -> Self {
ID(x)
}
}
impl Deref for ID {
type Target = i64;
fn deref(&self) -> &i64 {
&self.0
}
}
#[cfg(test)]
mod tests {
use super::*;
fn foo(_arg: &i64) {}
#[test]
fn test_flexible_id() {
assert_eq!(*ID(234), 234);
assert_eq!(ID(234), 234.into());
foo(&ID(234));
}
}