scsys_time/timestamp/
impl_timestamp.rs1use super::Timestamp;
6
7use crate::traits::{Now, RawTimestamp};
8
9impl<T> Timestamp<T>
10where
11 T: RawTimestamp,
12{
13 pub const fn new(ts: T) -> Self {
15 Self(ts)
16 }
17 pub fn now() -> Self
20 where
21 T: Now<Output = Self>,
22 {
23 <Self as Now>::now()
24 }
25 pub const fn get(&self) -> &T {
27 &self.0
28 }
29 pub const fn get_mut(&mut self) -> &mut T {
31 &mut self.0
32 }
33 #[inline]
35 pub fn into_inner(self) -> T {
36 self.0
37 }
38 pub const fn replace(&mut self, value: T) -> T {
40 core::mem::replace(self.get_mut(), value)
41 }
42 #[inline]
44 pub fn set(&mut self, ts: T) {
45 *self.get_mut() = ts
46 }
47 pub const fn swap(&mut self, ts: &mut T) {
49 core::mem::swap(self.get_mut(), ts)
50 }
51 #[inline]
54 pub fn take(&mut self) -> T
55 where
56 T: Default,
57 {
58 core::mem::take(self.get_mut())
59 }
60 #[inline]
62 pub fn with<U: RawTimestamp>(self, ts: U) -> Timestamp<U> {
63 Timestamp(ts)
64 }
65 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 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 pub fn update(&mut self) -> T
83 where
84 T: Now<Output = T>,
85 {
86 let now = T::now();
87 self.replace(now)
89 }
90 pub const fn view(&self) -> Timestamp<&T> {
93 Timestamp(self.get())
94 }
95 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}