havok_types/
ulong.rs

1use parse_display::{Display, FromStr};
2
3/// Ptr size is the same as the ptr size.
4/// - 32bit for hkx for 32bit
5/// - 64bit for hkx for 64bit
6///
7/// # Why not use [`usize`]?
8/// Because if this code is compiled as a 32-bit application, the usize will be 32-bit, and 32-bit will not be able to reserve the size for 64-bit.
9#[cfg_attr(feature = "json_schema", derive(schemars::JsonSchema))]
10#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
11#[cfg_attr(feature = "serde", serde(transparent))]
12#[repr(transparent)]
13#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Display, FromStr)]
14#[display("{0}")]
15pub struct Ulong(u64);
16
17impl Ulong {
18    /// Creates a new `Ulong`
19    #[inline]
20    pub const fn new(num: u64) -> Self {
21        Self(num)
22    }
23
24    /// Get inner value.
25    #[inline]
26    pub const fn get(&self) -> u64 {
27        self.0
28    }
29}
30
31impl From<u64> for Ulong {
32    #[inline]
33    fn from(value: u64) -> Self {
34        Self(value)
35    }
36}
37
38impl From<Ulong> for u64 {
39    #[inline]
40    fn from(value: Ulong) -> Self {
41        value.0
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_create_new_ulong() {
51        let num = 12345_u64;
52        let ulong = Ulong::new(num);
53        assert_eq!(ulong.get(), num);
54    }
55
56    #[test]
57    fn test_from_u64() {
58        let num = 67890_u64;
59        let ulong: Ulong = num.into();
60        assert_eq!(ulong.get(), num);
61    }
62
63    #[test]
64    fn test_into_u64() {
65        let num = 54321_u64;
66        let ulong = Ulong::new(num);
67        let value: u64 = ulong.into();
68        assert_eq!(value, num);
69    }
70
71    #[test]
72    fn test_display() {
73        let ulong = Ulong::new(12345_u64);
74        assert_eq!(format!("{ulong}"), "12345");
75    }
76
77    #[test]
78    fn test_from_str() {
79        let num_str = "67890";
80        let ulong = num_str.parse::<Ulong>();
81        assert_eq!(ulong, Ok(Ulong::new(67890)));
82    }
83
84    #[test]
85    fn test_default() {
86        let default_ulong = Ulong::default();
87        assert_eq!(default_ulong.get(), 0);
88    }
89
90    #[cfg(feature = "serde")]
91    #[test]
92    fn test_serde() {
93        let ulong = Ulong::new(12345_u64);
94
95        let serialized = serde_json::to_string(&ulong).unwrap();
96        assert_eq!(serialized, "12345");
97
98        let deserialized: Ulong = serde_json::from_str(&serialized).unwrap();
99        assert_eq!(deserialized, ulong);
100    }
101}