wallabag_api/types/
common.rs1use std::fmt;
5use std::ops::Deref;
6
7use serde::{Deserialize, Serialize};
8
9#[derive(Serialize, Deserialize, Hash, Debug, Clone, Copy, PartialEq, Eq)]
11pub struct ID(pub i64);
12
13impl fmt::Display for ID {
14 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
15 write!(f, "{}", self.0)
16 }
17}
18
19impl ID {
20 pub fn as_int(self) -> i64 {
21 self.0
22 }
23}
24
25impl From<i32> for ID {
27 fn from(x: i32) -> Self {
28 ID(i64::from(x))
29 }
30}
31
32impl From<i64> for ID {
34 fn from(x: i64) -> Self {
35 ID(x)
36 }
37}
38
39impl Deref for ID {
43 type Target = i64;
44
45 fn deref(&self) -> &i64 {
46 &self.0
47 }
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 fn foo(_arg: &i64) {}
55
56 #[test]
57 fn test_flexible_id() {
58 assert_eq!(*ID(234), 234);
59 assert_eq!(ID(234), 234.into());
60 foo(&ID(234));
62 }
63}