Increment

Struct Increment 

Source
pub(crate) struct Increment {
    unit: Unit,
    value: i32,
}
Expand description

A representation of a rounding increment in a particular unit.

This implements the core rounding interface of Jiff, shared across all types. The constructors on Increment know which units and increments are valid for a particular rounding configuration. For example, only Increment::for_span supports any unit value. All other constructors have some kind of limitation (e.g., you can’t round a date to the nearest month).

For most cases, rounding is done via Increment::round. This takes a quantity in a number of nanoseconds and rounds it to the nearest increment (after converting the increment to nanoseconds as well, based on the unit provided). This API makes it difficult for callers to get rounding wrong. They just need to use the right constructor and provide a quantity in units of nanoseconds.

For lower level needs, one can use RoundMode::round_by_duration directly. This doesn’t know about any units other than nanoseconds. It’s useful in contexts when dealing with variable length units and there are no rules for what is a valid increment.

Fields§

§unit: Unit§value: i32

Implementations§

Source§

impl Increment

Source

pub(crate) fn for_span(unit: Unit, value: i64) -> Result<Increment, Error>

Return a rounding increment suitable for use when rounding a Span.

Source

pub(crate) fn for_signed_duration( unit: Unit, value: i64, ) -> Result<Increment, Error>

Return a rounding increment suitable for use when rounding a SignedDuration.

Source

pub(crate) fn for_offset(unit: Unit, value: i64) -> Result<Increment, Error>

Return a rounding increment suitable for use when rounding a tz::Offset.

Source

pub(crate) fn for_datetime(unit: Unit, value: i64) -> Result<Increment, Error>

Return a rounding increment suitable for use when rounding a civil::DateTime or a Zoned.

Source

pub(crate) fn for_time(unit: Unit, value: i64) -> Result<Increment, Error>

Return a rounding increment suitable for use when rounding a civil::Time.

Source

pub(crate) fn for_timestamp(unit: Unit, value: i64) -> Result<Increment, Error>

Return a rounding increment suitable for use when rounding a civil::Timestamp.

Source

pub(crate) fn round( &self, mode: RoundMode, quantity: SignedDuration, ) -> Result<SignedDuration, Error>

Rounds the given quantity to the nearest increment value (in terms of the units given to this increment’s constructor). Rounding down or up is determined by the mode given.

Source

pub(crate) fn unit(&self) -> Unit

Returns this increment’s unit.

The increment value is always in terms of this unit.

Source

pub(crate) fn value(&self) -> i32

Returns this increment’s value.

e.g., “round to the nearest 15th minute” is expressed with an increment value of 15 and a unit of Unit::Minute.

The value returned is guaranteed to be in the range 1..=1_000_000_000.

Source§

impl Increment

Lower level constructors that require the caller to know the precise limits or maximums. These should generally stay unexported.

Source

fn for_limits( unit: Unit, value: i64, limits: &[MustDivide], ) -> Result<Increment, Error>

Validate the increment value for the given unit and limits.

Specifically, this ensures that rounding to the given unit is supported and that the specific value is less than to the corresponding limit.

Source

fn for_maximums( unit: Unit, value: i64, maximums: &[MustDivide], ) -> Result<Increment, Error>

Validate the increment value for the given unit and maximums.

Specifically, this ensures that rounding to the given unit is supported and that the specific value is less than or equal to the corresponding maximum.

Trait Implementations§

Source§

impl Clone for Increment

Source§

fn clone(&self) -> Increment

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Increment

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Copy for Increment

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.