use rinia::{
Scalard, Scalarf,
numeric::{IsFinite, Zero},
};
use crate::{Duration, Error, HasDuration, common};
impl<T> Duration<T> {
#[inline]
pub const fn new(seconds: T) -> Self {
Self(seconds)
}
#[inline]
pub fn try_from_seconds(seconds: T) -> Result<Self, Error>
where
T: Copy + IsFinite,
{
if seconds.is_finite() {
Ok(Self(seconds))
} else {
Err(Error::InvalidValue("Duration cannot be created from NaN or infinite values"))
}
}
#[inline]
pub fn from_seconds(seconds: T) -> Self
where
T: Copy + IsFinite,
{
Self::try_from_seconds(seconds).unwrap_or_else(|err| panic!("{err}"))
}
#[inline]
pub const fn seconds(&self) -> T
where
T: Copy,
{
self.0
}
#[inline]
pub fn into_seconds(self) -> T {
self.0
}
}
impl<T> Default for Duration<T>
where
Self: Zero,
{
fn default() -> Self {
Self::ZERO
}
}
impl<T> From<T> for Duration<T> {
fn from(seconds: T) -> Self {
Self(seconds)
}
}
impl From<Duration<Scalarf>> for Scalarf {
fn from(duration: Duration<Scalarf>) -> Self {
duration.0
}
}
impl From<Duration<Scalard>> for Scalard {
fn from(duration: Duration<Scalard>) -> Self {
duration.0
}
}
impl<T> HasDuration<T> for Duration<T>
where
T: Copy,
{
fn duration(&self) -> Duration<T> {
*self
}
}
rinia::impl_approx_eq_wrapper!([T], impl: Duration<T>, item: T);
common::impl_bytemuck_basic!(
[T],
Duration<T>,
item: T,
);