pub struct DurationString(/* private fields */);Expand description
A duration value that deserializes from the formats accepted by the Agent’s configuration loader.
§Deserialization
Accepted inputs:
-
Strings with Go time-unit suffixes:
"30s","1h30m","250ms","2h45m30s","1.5h". Valid suffixes:ns,us,µs,ms,s,m,h. -
Strings containing only a bare integer:
"5"is 5 nanoseconds. This matches viperscast.ToDurationE’s fallback for unit-less string values. -
Integer numbers:
5is 5 nanoseconds. -
Floating-point numbers:
5.0is 5 nanoseconds (truncated toward zero).
Negative durations (for example "-1h") are rejected because std::time::Duration cannot represent them.
§Bare numbers are nanoseconds, not seconds (!!)
A configuration value like expected_tags_duration: 30 means 30 nanoseconds, not 30 seconds. Use "30s" for
30 seconds. This matches the Agent’s time.Duration coercion.
§Serialization
Serializes as "{seconds}s{nanoseconds}ns". For example, 30 seconds becomes "30s0ns" and 30.5 seconds becomes
"30s500000000ns". Whole seconds are maximized and the nanosecond component is always less than 1_000_000_000,
so the form is unambiguous and round-trips through this parser and would be accepted by the Agent as well.
Implementations§
Methods from Deref<Target = Duration>§
pub const SECOND: Duration
pub const MILLISECOND: Duration
pub const MICROSECOND: Duration
pub const NANOSECOND: Duration
pub const ZERO: Duration
pub const MAX: Duration
1.53.0 · Sourcepub fn is_zero(&self) -> bool
pub fn is_zero(&self) -> bool
Returns true if this Duration spans no time.
§Examples
use std::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());1.3.0 · Sourcepub fn as_secs(&self) -> u64
pub 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 std::time::Duration;
let duration = Duration::new(5, 730_023_852);
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
1.27.0 · Sourcepub fn subsec_millis(&self) -> u32
pub 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 std::time::Duration;
let duration = Duration::from_millis(5_432);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_millis(), 432);1.27.0 · Sourcepub fn subsec_micros(&self) -> u32
pub 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 std::time::Duration;
let duration = Duration::from_micros(1_234_567);
assert_eq!(duration.as_secs(), 1);
assert_eq!(duration.subsec_micros(), 234_567);1.3.0 · Sourcepub fn subsec_nanos(&self) -> u32
pub 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 std::time::Duration;
let duration = Duration::from_millis(5_010);
assert_eq!(duration.as_secs(), 5);
assert_eq!(duration.subsec_nanos(), 10_000_000);1.33.0 · Sourcepub fn as_millis(&self) -> u128
pub fn as_millis(&self) -> u128
Returns the total number of whole milliseconds contained by this Duration.
§Examples
use std::time::Duration;
let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_millis(), 5_730);1.33.0 · Sourcepub fn as_micros(&self) -> u128
pub fn as_micros(&self) -> u128
Returns the total number of whole microseconds contained by this Duration.
§Examples
use std::time::Duration;
let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_micros(), 5_730_023);1.33.0 · Sourcepub fn as_nanos(&self) -> u128
pub fn as_nanos(&self) -> u128
Returns the total number of nanoseconds contained by this Duration.
§Examples
use std::time::Duration;
let duration = Duration::new(5, 730_023_852);
assert_eq!(duration.as_nanos(), 5_730_023_852);1.38.0 · 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 includes the fractional (nanosecond) part of the duration.
§Examples
use std::time::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f64(), 2.7);1.38.0 · Sourcepub fn as_secs_f32(&self) -> f32
pub fn as_secs_f32(&self) -> f32
Returns the number of seconds contained by this Duration as f32.
The returned value includes the fractional (nanosecond) part of the duration.
§Examples
use std::time::Duration;
let dur = Duration::new(2, 700_000_000);
assert_eq!(dur.as_secs_f32(), 2.7);Sourcepub fn as_millis_f64(&self) -> f64
🔬This is a nightly-only experimental API. (duration_millis_float)
pub fn as_millis_f64(&self) -> f64
duration_millis_float)Returns the number of milliseconds contained by this Duration as f64.
The returned value includes the fractional (nanosecond) part of the duration.
§Examples
#![feature(duration_millis_float)]
use std::time::Duration;
let dur = Duration::new(2, 345_678_000);
assert_eq!(dur.as_millis_f64(), 2_345.678);Sourcepub fn as_millis_f32(&self) -> f32
🔬This is a nightly-only experimental API. (duration_millis_float)
pub fn as_millis_f32(&self) -> f32
duration_millis_float)Returns the number of milliseconds contained by this Duration as f32.
The returned value includes the fractional (nanosecond) part of the duration.
§Examples
#![feature(duration_millis_float)]
use std::time::Duration;
let dur = Duration::new(2, 345_678_000);
assert_eq!(dur.as_millis_f32(), 2_345.678);Trait Implementations§
Source§impl Clone for DurationString
impl Clone for DurationString
Source§fn clone(&self) -> DurationString
fn clone(&self) -> DurationString
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for DurationString
impl Debug for DurationString
Source§impl Default for DurationString
impl Default for DurationString
Source§fn default() -> DurationString
fn default() -> DurationString
Source§impl Deref for DurationString
impl Deref for DurationString
Source§impl<'de> Deserialize<'de> for DurationString
impl<'de> Deserialize<'de> for DurationString
Source§fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>
Source§impl Display for DurationString
impl Display for DurationString
Source§impl From<Duration> for DurationString
impl From<Duration> for DurationString
Source§impl From<DurationString> for Duration
impl From<DurationString> for Duration
Source§fn from(d: DurationString) -> Self
fn from(d: DurationString) -> Self
Source§impl FromStr for DurationString
impl FromStr for DurationString
Source§impl Hash for DurationString
impl Hash for DurationString
Source§impl Ord for DurationString
impl Ord for DurationString
Source§fn cmp(&self, other: &DurationString) -> Ordering
fn cmp(&self, other: &DurationString) -> Ordering
1.21.0 · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for DurationString
impl PartialEq for DurationString
Source§impl PartialOrd for DurationString
impl PartialOrd for DurationString
Source§impl Serialize for DurationString
impl Serialize for DurationString
impl Copy for DurationString
impl Eq for DurationString
impl StructuralPartialEq for DurationString
Auto Trait Implementations§
impl Freeze for DurationString
impl RefUnwindSafe for DurationString
impl Send for DurationString
impl Sync for DurationString
impl Unpin for DurationString
impl UnsafeUnpin for DurationString
impl UnwindSafe for DurationString
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
Source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();Source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
Source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
Source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
Source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();Source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
Source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
Source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
Source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();Source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi Quirk value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();Source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
Source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);