pub struct Ttl(_);
Expand description
A span of time, typically used to describe the time a given DNS record is valid.
Ttl
implements many common traits, including core::ops::Add
, core::ops::Sub
, and other core::ops
traits. It implements Default by returning a zero-length Ttl
.
Why not std::time::Duration
?
Two reasons make std::time::Duration
not suited for representing DNS TTL values:
- According to RFC 2181 TTL values have second-level precision while
std::time::Duration
can represent time down to the nanosecond level. This amount of precision is simply not needed and might cause confusion when sendingDuration
s over the network. - When working with DNS TTL values it’s common to want to know a time to live in minutes or hours.
std::time::Duration
does not expose easy to use methods for this purpose, whileTtl
does.
Ttl
provides two methods Ttl::from_duration_lossy
and Ttl::into_duration
to convert between Duration
and Ttl
.
Implementations§
source§impl Ttl
impl Ttl
sourcepub const MAX_MINUTES: u32 = 71_582_788u32
pub const MAX_MINUTES: u32 = 71_582_788u32
The maximum number of minutes that a Ttl
can represent.
pub const COMPOSE_LEN: u16 = 4u16
sourcepub const fn as_secs(&self) -> u32
pub const fn as_secs(&self) -> u32
Returns the total time to live in seconds.
Examples
use domain::base::Ttl;
let ttl = Ttl::from_secs(120);
assert_eq!(ttl.as_secs(), 120);
sourcepub const fn as_minutes(&self) -> u32
pub const fn as_minutes(&self) -> u32
Returns the total time to live in minutes.
Examples
use domain::base::Ttl;
let ttl = Ttl::from_secs(120);
assert_eq!(ttl.as_minutes(), 2);
sourcepub const fn as_hours(&self) -> u32
pub const fn as_hours(&self) -> u32
Returns the total time to live in hours.
Examples
use domain::base::Ttl;
let ttl = Ttl::from_secs(7200);
assert_eq!(ttl.as_hours(), 2);
sourcepub const fn as_days(&self) -> u16
pub const fn as_days(&self) -> u16
Returns the total time to live in days.
Examples
use domain::base::Ttl;
let ttl = Ttl::from_secs(172800);
assert_eq!(ttl.as_days(), 2);
sourcepub const fn into_duration(&self) -> Duration
pub const fn into_duration(&self) -> Duration
Converts a Ttl
into a std::time::Duration
.
Examples
use domain::base::Ttl;
use std::time::Duration;
let ttl = Ttl::from_mins(2);
let duration = ttl.into_duration();
assert_eq!(duration.as_secs(), 120);
sourcepub const fn from_secs(secs: u32) -> Self
pub const fn from_secs(secs: u32) -> Self
Creates a new Ttl
from the specified number of seconds.
sourcepub const fn from_mins(minutes: u32) -> Self
pub const fn from_mins(minutes: u32) -> Self
Creates a new Ttl
from the specified number of minutes.
Panics
The maximum number of days that a Ttl
can represent is 71582788
.
This method will panic if it is being called with a value greater than that.
sourcepub const fn from_hours(hours: u32) -> Self
pub const fn from_hours(hours: u32) -> Self
Creates a new Ttl
from the specified number of hours.
Panics
The maximum number of hours that a Ttl
can represent is 1193046
.
This method will panic if it is being called with a value greater than that.
sourcepub const fn from_days(days: u16) -> Self
pub const fn from_days(days: u16) -> Self
Creates a new Ttl
from the specified number of days.
Panics
The maximum number of days that a Ttl
can represent is 49710
.
This method will panic if it is being called with a value greater than that.
sourcepub const fn from_duration_lossy(duration: Duration) -> Self
pub const fn from_duration_lossy(duration: Duration) -> Self
Creates a new Ttl
from a std::time::Duration
.
This operation is lossy as Duration
stores seconds as u64
, while Ttl
stores seconds as u32
to comply with the DNS specifications.
Duration
also represents time using sub-second precision, which is not kept when converting into a Ttl
.
Examples
use domain::base::Ttl;
use std::time::Duration;
assert_eq!(Ttl::from_duration_lossy(Duration::new(1, 0)), Ttl::from_secs(1));
assert_eq!(Ttl::from_duration_lossy(Duration::new(1, 6000)), Ttl::from_secs(1));
sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Returns true if this Tll
spans no time.
This usually indicates a given record should not be cached.
Examples
use domain::base::Ttl;
assert!(Ttl::ZERO.is_zero());
assert!(Ttl::from_secs(0).is_zero());
assert!(Ttl::from_mins(0).is_zero());
assert!(Ttl::from_hours(0).is_zero());
assert!(Ttl::from_days(0).is_zero());
sourcepub const fn checked_add(self, rhs: Ttl) -> Option<Ttl>
pub const fn checked_add(self, rhs: Ttl) -> Option<Ttl>
sourcepub const fn saturating_add(self, rhs: Ttl) -> Ttl
pub const fn saturating_add(self, rhs: Ttl) -> Ttl
sourcepub const fn checked_sub(self, rhs: Ttl) -> Option<Ttl>
pub const fn checked_sub(self, rhs: Ttl) -> Option<Ttl>
Checked Ttl
subtraction. Computes self - other
, returning None
if the result would be negative or if overflow occurred.
Examples
use domain::base::Ttl;
assert_eq!(Ttl::from_secs(1).checked_sub(Ttl::from_secs(0)), Some(Ttl::from_secs(1)));
assert_eq!(Ttl::from_secs(0).checked_sub(Ttl::from_secs(1)), None);
sourcepub const fn saturating_sub(self, rhs: Ttl) -> Ttl
pub const fn saturating_sub(self, rhs: Ttl) -> Ttl
Saturating Ttl
subtraction. Computes self - other
, returning Ttl::ZERO
if the result would be negative or if overflow occurred.
Examples
use domain::base::Ttl;
assert_eq!(Ttl::from_secs(1).saturating_sub(Ttl::from_secs(0)), Ttl::from_secs(1));
assert_eq!(Ttl::from_secs(0).saturating_sub(Ttl::from_secs(1)), Ttl::ZERO);
sourcepub const fn checked_mul(self, rhs: u32) -> Option<Ttl>
pub const fn checked_mul(self, rhs: u32) -> Option<Ttl>
sourcepub const fn saturating_mul(self, rhs: u32) -> Ttl
pub const fn saturating_mul(self, rhs: u32) -> Ttl
Saturating Duration
multiplication. Computes self * other
, returning
Duration::MAX
if overflow occurred.
Examples
use domain::base::Ttl;
assert_eq!(Ttl::from_secs(5).saturating_mul(2), Ttl::from_secs(10));
assert_eq!(Ttl::from_secs(u32::MAX - 1).saturating_mul(2), Ttl::MAX);
sourcepub const fn checked_div(self, rhs: u32) -> Option<Ttl>
pub const fn checked_div(self, rhs: u32) -> Option<Ttl>
Checked Duration
division. Computes self / other
, returning None
if other == 0
.
Examples
use domain::base::Ttl;
assert_eq!(Ttl::from_secs(10).checked_div(2), Some(Ttl::from_secs(5)));
assert_eq!(Ttl::from_mins(1).checked_div(2), Some(Ttl::from_secs(30)));
assert_eq!(Ttl::from_secs(2).checked_div(0), None);
pub fn compose<Target: OctetsBuilder + ?Sized>( &self, target: &mut Target ) -> Result<(), Target::AppendError>
pub fn parse<Octs: AsRef<[u8]> + ?Sized>( parser: &mut Parser<'_, Octs> ) -> Result<Self, ParseError>
Trait Implementations§
source§impl AddAssign<Ttl> for Ttl
impl AddAssign<Ttl> for Ttl
source§fn add_assign(&mut self, rhs: Ttl)
fn add_assign(&mut self, rhs: Ttl)
+=
operation. Read moresource§impl DivAssign<u32> for Ttl
impl DivAssign<u32> for Ttl
source§fn div_assign(&mut self, rhs: u32)
fn div_assign(&mut self, rhs: u32)
/=
operation. Read moresource§impl MulAssign<u32> for Ttl
impl MulAssign<u32> for Ttl
source§fn mul_assign(&mut self, rhs: u32)
fn mul_assign(&mut self, rhs: u32)
*=
operation. Read moresource§impl Ord for Ttl
impl Ord for Ttl
source§impl PartialOrd<Ttl> for Ttl
impl PartialOrd<Ttl> for Ttl
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign<Ttl> for Ttl
impl SubAssign<Ttl> for Ttl
source§fn sub_assign(&mut self, rhs: Ttl)
fn sub_assign(&mut self, rhs: Ttl)
-=
operation. Read more