use super::Timestamp;
use crate::traits::{Now, RawTimestamp};
impl<T> Timestamp<T>
where
T: RawTimestamp,
{
pub const fn new(ts: T) -> Self {
Self(ts)
}
pub fn now() -> Self
where
T: Now<Output = Self>,
{
<Self as Now>::now()
}
pub const fn get(&self) -> &T {
&self.0
}
pub const fn get_mut(&mut self) -> &mut T {
&mut self.0
}
#[inline]
pub fn into_inner(self) -> T {
self.0
}
pub const fn replace(&mut self, value: T) -> T {
core::mem::replace(self.get_mut(), value)
}
#[inline]
pub fn set(&mut self, ts: T) {
*self.get_mut() = ts
}
pub const fn swap(&mut self, ts: &mut T) {
core::mem::swap(self.get_mut(), ts)
}
#[inline]
pub fn take(&mut self) -> T
where
T: Default,
{
core::mem::take(self.get_mut())
}
#[inline]
pub fn with<U: RawTimestamp>(self, ts: U) -> Timestamp<U> {
Timestamp(ts)
}
pub fn map<U, F>(self, f: F) -> Timestamp<U>
where
F: FnOnce(T) -> U,
U: RawTimestamp,
{
Timestamp(f(self.into_inner()))
}
pub fn map_inplace<F>(&mut self, f: F) -> &mut Self
where
F: FnOnce(&mut T),
{
f(self.get_mut());
self
}
pub fn update(&mut self) -> T
where
T: Now<Output = T>,
{
let now = T::now();
self.replace(now)
}
pub const fn view(&self) -> Timestamp<&T> {
Timestamp(self.get())
}
pub const fn view_mut(&mut self) -> Timestamp<&mut T> {
Timestamp(self.get_mut())
}
}
impl<T> Default for Timestamp<T>
where
T: Now<Output = Self> + RawTimestamp,
{
fn default() -> Self {
Self::now()
}
}
impl<T> Now for Timestamp<T>
where
T: Now<Output = Timestamp<T>> + RawTimestamp,
{
type Output = Self;
fn now() -> Self::Output {
T::now()
}
}
impl<T> AsRef<T> for Timestamp<T> {
fn as_ref(&self) -> &T {
&self.0
}
}
impl<T> AsMut<T> for Timestamp<T> {
fn as_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> core::borrow::Borrow<T> for Timestamp<T> {
fn borrow(&self) -> &T {
&self.0
}
}
impl<T> core::borrow::BorrowMut<T> for Timestamp<T> {
fn borrow_mut(&mut self) -> &mut T {
&mut self.0
}
}
impl<T> core::ops::Deref for Timestamp<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> core::ops::DerefMut for Timestamp<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
macro_rules! impl_fmt {
($($trait:ident),* $(,)?) => {
$(
impl<T> ::core::fmt::$trait for Timestamp<T>
where
T: ::core::fmt::$trait,
{
fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
::core::fmt::$trait::fmt(&self.0, f)
}
}
)*
};
}
impl_fmt! {
Binary,
Octal,
LowerHex,
UpperHex,
Display,
Debug,
LowerExp,
UpperExp,
Pointer,
}