pub struct Duration { /* private fields */ }Expand description
A Duration type to represent a span of time, typically used for system
timeouts.
Each Duration is composed of a whole number of seconds and a fractional part
represented in nanoseconds. If the underlying system does not support
nanosecond-level precision, APIs binding a system timeout will typically round up
the number of nanoseconds.
§Examples
use time::Duration;
let five_seconds = Duration::new(5, 0);
let five_seconds_and_five_nanos = five_seconds + Duration::new(0, 5);
assert_eq!(five_seconds_and_five_nanos.as_secs(), 5);
assert_eq!(five_seconds_and_five_nanos.subsec_nanos(), 5);
let ten_millis = Duration::from_millis(10);Implementations§
Source§impl Duration
impl Duration
Sourcepub fn into_std(self) -> Duration
pub fn into_std(self) -> Duration
Converts Duration into a std::time::Duration.
Sourcepub fn from_std(duration: Duration) -> Self
pub fn from_std(duration: Duration) -> Self
Creates a Duration from a std::time::Duration.
Sourcepub fn into_tokio(self) -> Duration
pub fn into_tokio(self) -> Duration
Converts Duration into a tokio::time::Duration.
§Example
use rune_modules::time::Duration;
let duration = Duration::from_secs(5);
let tokio_duration = duration.into_tokio();Sourcepub fn from_tokio(duration: Duration) -> Self
pub fn from_tokio(duration: Duration) -> Self
Creates a Duration from a tokio::time::Duration.
§Example
use rune_modules::time::Duration;
let tokio_duration = tokio::time::Duration::from_secs(5);
let duration = Duration::from_tokio(tokio_duration);Sourcepub fn new(secs: u64, nanos: u32) -> VmResult<Self>
pub fn new(secs: u64, nanos: u32) -> VmResult<Self>
Creates a new Duration from the specified number of whole seconds and
additional nanoseconds.
If the number of nanoseconds is greater than 1 billion (the number of nanoseconds in a second), then it will carry over into the seconds provided.
§Vm Panics
This constructor will panic if the carry from the nanoseconds overflows the seconds counter.
§Examples
use time::Duration;
let five_seconds = Duration::new(5, 0);Sourcepub const fn from_secs(secs: u64) -> Self
pub const fn from_secs(secs: u64) -> Self
Creates a new Duration from the specified number of whole seconds.
§Examples
use time::Duration;
let duration = Duration::from_secs(5);Sourcepub const fn from_millis(millis: u64) -> Self
pub const fn from_millis(millis: u64) -> Self
Creates a new Duration from the specified number of milliseconds.
§Examples
use time::Duration;
let duration = Duration::from_millis(2569);Sourcepub const fn from_micros(micros: u64) -> Self
pub const fn from_micros(micros: u64) -> Self
Creates a new Duration from the specified number of microseconds.
§Examples
use time::Duration;
let duration = Duration::from_micros(1_000_002);Sourcepub const fn from_nanos(nanos: u64) -> Self
pub const fn from_nanos(nanos: u64) -> Self
Creates a new Duration from the specified number of nanoseconds.
Note: Using this on the return value of as_nanos() might cause unexpected behavior:
as_nanos() returns a u128, and can return values that do not fit in u64, e.g. 585 years.
Instead, consider using the pattern Duration::new(d.as_secs(), d.subsec_nanos())
if you cannot copy/clone the Duration directly.
§Examples
use time::Duration;
let duration = Duration::from_nanos(1_000_000_123);Sourcepub const fn is_zero(&self) -> bool
pub const fn is_zero(&self) -> bool
Returns true if this Duration spans no time.
§Examples
use time::Duration;
assert!(Duration::ZERO.is_zero());
assert!(Duration::new(0, 0).is_zero());
assert!(Duration::from_nanos(0).is_zero());
assert!(Duration::from_secs(0).is_zero());
assert!(!Duration::new(1, 1).is_zero());
assert!(!Duration::from_nanos(1).is_zero());
assert!(!Duration::from_secs(1).is_zero());Sourcepub const fn as_secs(&self) -> u64
pub const fn as_secs(&self) -> u64
Returns the number of whole seconds contained by this Duration.
The returned value does not include the fractional (nanosecond) part of
the duration, which can be obtained using subsec_nanos.
§Examples
use time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_secs(), 5);To determine the total number of seconds represented by the Duration
including the fractional part, use as_secs_f64 or as_secs_f32
Sourcepub const fn subsec_millis(&self) -> u32
pub const fn subsec_millis(&self) -> u32
Returns the fractional part of this Duration, in whole milliseconds.
This method does not return the length of the duration when represented by milliseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one thousand).
§Examples
use time::Duration;
let duration = Duration::from_millis(5432);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_millis(), 432);Sourcepub const fn subsec_micros(&self) -> u32
pub const fn subsec_micros(&self) -> u32
Returns the fractional part of this Duration, in whole microseconds.
This method does not return the length of the duration when represented by microseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one million).
§Examples
use time::Duration;
let duration = Duration::from_micros(1_234_567);
assert_eq!(duration.as_secs(), 1);
assert_eq!(duration.subsec_micros(), 234_567);Sourcepub const fn subsec_nanos(&self) -> u32
pub const fn subsec_nanos(&self) -> u32
Returns the fractional part of this Duration, in nanoseconds.
This method does not return the length of the duration when represented by nanoseconds. The returned number always represents a fractional portion of a second (i.e., it is less than one billion).
§Examples
use time::Duration;
let duration = Duration::from_millis(5010);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_nanos(), 10_000_000);Sourcepub const fn as_millis(&self) -> u128
pub const fn as_millis(&self) -> u128
Returns the total number of whole milliseconds contained by this
Duration.
§Examples
use time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_millis(), 5730);Sourcepub const fn as_micros(&self) -> u128
pub const fn as_micros(&self) -> u128
Returns the total number of whole microseconds contained by this
Duration.
§Examples
use time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_micros(), 5730023);Sourcepub const fn as_nanos(&self) -> u128
pub const fn as_nanos(&self) -> u128
Returns the total number of nanoseconds contained by this Duration.
§Examples
use time::Duration;
let duration = Duration::new(5, 730023852);
assert_eq!(duration.as_nanos(), 5730023852);Sourcepub fn as_secs_f64(&self) -> f64
pub fn as_secs_f64(&self) -> f64
Returns the number of seconds contained by this Duration as f64.
The returned value does include the fractional (nanosecond) part of the duration.
§Examples
use time::Duration;
let duration = Duration::from_secs(60).as_secs_f64();Sourcepub fn from_secs_f64(secs: f64) -> VmResult<Self>
pub fn from_secs_f64(secs: f64) -> VmResult<Self>
Creates a new Duration from the specified number of seconds represented
as f64.
§Examples
use time::Duration;
let duration = Duration::from_secs_f64(0.0);