pub trait ToSpan: Sized {
Show 20 methods
// Required methods
fn years(self) -> Span;
fn months(self) -> Span;
fn weeks(self) -> Span;
fn days(self) -> Span;
fn hours(self) -> Span;
fn minutes(self) -> Span;
fn seconds(self) -> Span;
fn milliseconds(self) -> Span;
fn microseconds(self) -> Span;
fn nanoseconds(self) -> Span;
// Provided methods
fn year(self) -> Span { ... }
fn month(self) -> Span { ... }
fn week(self) -> Span { ... }
fn day(self) -> Span { ... }
fn hour(self) -> Span { ... }
fn minute(self) -> Span { ... }
fn second(self) -> Span { ... }
fn millisecond(self) -> Span { ... }
fn microsecond(self) -> Span { ... }
fn nanosecond(self) -> Span { ... }
}
Expand description
A trait for enabling concise literals for creating Span
values.
In short, this trait lets you write something like 5.seconds()
or
1.day()
to create a Span
. Once a Span
has been created, you can
use its mutator methods to add more fields. For example,
1.day().hours(10)
is equivalent to Span::new().days(1).hours(10)
.
This trait is implemented for the following integer types: i8
, i16
,
i32
and i64
.
Note that this trait is provided as a convenience and should generally
only be used for literals in your source code. You should not use this
trait on numbers provided by end users. Namely, if the number provided
is not within Jiff’s span limits, then these trait methods will panic.
Instead, use fallible mutator constructors like Span::try_days
or Span::try_seconds
.
§Example
use jiff::ToSpan;
assert_eq!(5.days().to_string(), "P5D");
assert_eq!(5.days().hours(10).to_string(), "P5DT10H");
// Negation works and it doesn't matter where the sign goes. It can be
// applied to the span itself or to the integer.
assert_eq!((-5.days()).to_string(), "-P5D");
assert_eq!((-5).days().to_string(), "-P5D");
§Example: alternative via span parsing
Another way of tersely building a Span
value is by parsing a ISO 8601
duration string:
use jiff::Span;
let span = "P5y2m15dT23h30m10s".parse::<Span>()?;
assert_eq!(
span.fieldwise(),
Span::new().years(5).months(2).days(15).hours(23).minutes(30).seconds(10),
);
Required Methods§
Sourcefn years(self) -> Span
fn years(self) -> Span
Create a new span from this integer in units of years.
§Panics
When Span::new().years(self)
would panic.
Sourcefn months(self) -> Span
fn months(self) -> Span
Create a new span from this integer in units of months.
§Panics
When Span::new().months(self)
would panic.
Sourcefn weeks(self) -> Span
fn weeks(self) -> Span
Create a new span from this integer in units of weeks.
§Panics
When Span::new().weeks(self)
would panic.
Sourcefn days(self) -> Span
fn days(self) -> Span
Create a new span from this integer in units of days.
§Panics
When Span::new().days(self)
would panic.
Sourcefn hours(self) -> Span
fn hours(self) -> Span
Create a new span from this integer in units of hours.
§Panics
When Span::new().hours(self)
would panic.
Sourcefn minutes(self) -> Span
fn minutes(self) -> Span
Create a new span from this integer in units of minutes.
§Panics
When Span::new().minutes(self)
would panic.
Sourcefn seconds(self) -> Span
fn seconds(self) -> Span
Create a new span from this integer in units of seconds.
§Panics
When Span::new().seconds(self)
would panic.
Sourcefn milliseconds(self) -> Span
fn milliseconds(self) -> Span
Create a new span from this integer in units of milliseconds.
§Panics
When Span::new().milliseconds(self)
would panic.
Sourcefn microseconds(self) -> Span
fn microseconds(self) -> Span
Create a new span from this integer in units of microseconds.
§Panics
When Span::new().microseconds(self)
would panic.
Sourcefn nanoseconds(self) -> Span
fn nanoseconds(self) -> Span
Create a new span from this integer in units of nanoseconds.
§Panics
When Span::new().nanoseconds(self)
would panic.
Provided Methods§
Sourcefn millisecond(self) -> Span
fn millisecond(self) -> Span
Equivalent to milliseconds()
, but reads better for singular units.
Sourcefn microsecond(self) -> Span
fn microsecond(self) -> Span
Equivalent to microseconds()
, but reads better for singular units.
Sourcefn nanosecond(self) -> Span
fn nanosecond(self) -> Span
Equivalent to nanoseconds()
, but reads better for singular units.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.