pub struct Ttl(/* private fields */);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::Durationcan represent time down to the nanosecond level. This amount of precision is simply not needed and might cause confusion when sendingDurations 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::Durationdoes not expose easy to use methods for this purpose, whileTtldoes.
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 for Ttl
impl AddAssign 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 for Ttl
impl PartialOrd for Ttl
Source§impl SubAssign for Ttl
impl SubAssign for Ttl
Source§fn sub_assign(&mut self, rhs: Ttl)
fn sub_assign(&mut self, rhs: Ttl)
-= operation. Read moreSource§impl ZonefileFmt for Ttl
impl ZonefileFmt for Ttl
Source§fn fmt(&self, p: &mut impl Formatter) -> Result
fn fmt(&self, p: &mut impl Formatter) -> Result
fmt::Formatter Read more