scsys_time/timestamp/
impl_timestamp.rs

1/*
2    appellation: impl_timestamp <module>
3    authors: @FL03
4*/
5use super::Timestamp;
6
7use crate::traits::{Now, RawTimestamp};
8
9impl<T> Timestamp<T>
10where
11    T: RawTimestamp,
12{
13    /// create a new instance of [`Timestamp`] with the given value.
14    pub const fn new(ts: T) -> Self {
15        Self(ts)
16    }
17    /// a convenience method to get the current timestamp; requires that the inner type
18    /// implement the [`Now`] trait.
19    pub fn now() -> Self
20    where
21        T: Now<Output = Self>,
22    {
23        <Self as Now>::now()
24    }
25    /// Get an immutable reference to the current timestamp.
26    pub const fn get(&self) -> &T {
27        &self.0
28    }
29    /// Get a mutable reference to the current timestamp.
30    pub const fn get_mut(&mut self) -> &mut T {
31        &mut self.0
32    }
33    /// consumes the current instance and returns the inner value.
34    #[inline]
35    pub fn into_inner(self) -> T {
36        self.0
37    }
38    /// [`replace`](core::mem::replace) the current value with a new one and return the old one
39    pub const fn replace(&mut self, value: T) -> T {
40        core::mem::replace(self.get_mut(), value)
41    }
42    /// update the current value and return a mutable reference to the current instance.
43    #[inline]
44    pub fn set(&mut self, ts: T) {
45        *self.get_mut() = ts
46    }
47    /// [`swap`](core::mem::swap) the current value with another and return a mutable reference to self
48    pub const fn swap(&mut self, ts: &mut T) {
49        core::mem::swap(self.get_mut(), ts)
50    }
51    /// [`take`](core::mem::take) the current value and return it, leaving the logical
52    /// [`default`](Default::default) in its place.
53    #[inline]
54    pub fn take(&mut self) -> T
55    where
56        T: Default,
57    {
58        core::mem::take(self.get_mut())
59    }
60    /// consumes the current instance to create another with the given value
61    #[inline]
62    pub fn with<U: RawTimestamp>(self, ts: U) -> Timestamp<U> {
63        Timestamp(ts)
64    }
65    /// applies a function onto the current value and returns a new instance with the result
66    pub fn map<U, F>(self, f: F) -> Timestamp<U>
67    where
68        F: FnOnce(T) -> U,
69        U: RawTimestamp,
70    {
71        Timestamp(f(self.into_inner()))
72    }
73    /// returns a new instance of the [`Timestamp`] with the current value updated using the given function
74    pub fn map_inplace<F>(&mut self, f: F) -> &mut Self
75    where
76        F: FnOnce(&mut T),
77    {
78        f(self.get_mut());
79        self
80    }
81    /// updates the timestamp to reflect _now_ and return the previous timestamp
82    pub fn update(&mut self) -> T
83    where
84        T: Now<Output = T>,
85    {
86        let now = T::now();
87        // replace the current value with the new one
88        self.replace(now)
89    }
90    /// returns a new instance of the [`Timestamp`] containing an immutable reference to the
91    /// inner value.
92    pub const fn view(&self) -> Timestamp<&T> {
93        Timestamp(self.get())
94    }
95    /// returns a new instance of the [`Timestamp`] containing a mutable reference to the inner
96    /// value.
97    pub const fn view_mut(&mut self) -> Timestamp<&mut T> {
98        Timestamp(self.get_mut())
99    }
100}
101
102impl<T> Default for Timestamp<T>
103where
104    T: Now<Output = Self> + RawTimestamp,
105{
106    fn default() -> Self {
107        Self::now()
108    }
109}
110
111impl<T> Now for Timestamp<T>
112where
113    T: Now<Output = Timestamp<T>> + RawTimestamp,
114{
115    type Output = Self;
116
117    fn now() -> Self::Output {
118        T::now()
119    }
120}
121
122impl<T> AsRef<T> for Timestamp<T> {
123    fn as_ref(&self) -> &T {
124        &self.0
125    }
126}
127
128impl<T> AsMut<T> for Timestamp<T> {
129    fn as_mut(&mut self) -> &mut T {
130        &mut self.0
131    }
132}
133
134impl<T> core::borrow::Borrow<T> for Timestamp<T> {
135    fn borrow(&self) -> &T {
136        &self.0
137    }
138}
139
140impl<T> core::borrow::BorrowMut<T> for Timestamp<T> {
141    fn borrow_mut(&mut self) -> &mut T {
142        &mut self.0
143    }
144}
145
146impl<T> core::ops::Deref for Timestamp<T> {
147    type Target = T;
148
149    fn deref(&self) -> &Self::Target {
150        &self.0
151    }
152}
153
154impl<T> core::ops::DerefMut for Timestamp<T> {
155    fn deref_mut(&mut self) -> &mut Self::Target {
156        &mut self.0
157    }
158}
159
160macro_rules! impl_fmt {
161    ($($trait:ident),* $(,)?) => {
162        $(
163            impl<T> ::core::fmt::$trait for Timestamp<T>
164            where
165                T: ::core::fmt::$trait,
166            {
167                fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
168                    ::core::fmt::$trait::fmt(&self.0, f)
169                }
170            }
171        )*
172    };
173}
174
175impl_fmt! {
176    Binary,
177    Octal,
178    LowerHex,
179    UpperHex,
180    Display,
181    Debug,
182    LowerExp,
183    UpperExp,
184    Pointer,
185}