Skip to main content

Overflow

Enum Overflow 

Source
#[non_exhaustive]
pub enum Overflow { Constrain, Reject, }
Expand description

Whether to constrain or reject out-of-bounds values when constructing a Date.

The behavior conforms to the ECMAScript Temporal specification.

๐Ÿšง This code is considered unstable; it may change at any time, in breaking or non-breaking ways, including in SemVer minor releases. Do not use this type unless you are prepared for things to occasionally break.

Graduation tracking issue: issue #7161.

โœจ Enabled with the unstable Cargo feature.

Variants (Non-exhaustive)ยง

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
ยง

Constrain

Constrain out-of-bounds fields to the nearest in-bounds value.

Only the out-of-bounds field is constrained. If the other fields are not themselves out of bounds, they are not changed.

This is the default option, following the ECMAScript Temporal specification. See also the docs on MDN.

ยงExamples

use icu::calendar::cal::Hebrew;
use icu::calendar::options::DateFromFieldsOptions;
use icu::calendar::options::Overflow;
use icu::calendar::types::DateFields;
use icu::calendar::Date;
use icu::calendar::DateError;

let mut options = DateFromFieldsOptions::default();
options.overflow = Some(Overflow::Constrain);

// 5784, a leap year, contains M05L, but the day is too big.
let mut fields = DateFields::default();
fields.extended_year = Some(5784);
fields.month_code = Some(b"M05L");
fields.day = Some(50);

let date = Date::try_from_fields(fields, options, Hebrew).unwrap();

// Constrained to the 30th day of M05L of year 5784
assert_eq!(date.year().extended_year(), 5784);
assert_eq!(date.month().standard_code.0, "M05L");
assert_eq!(date.day_of_month().0, 30);

// 5785, a common year, does not contain M05L.
fields.extended_year = Some(5785);

let date = Date::try_from_fields(fields, options, Hebrew).unwrap();

// Constrained to the 29th day of M06 of year 5785
assert_eq!(date.year().extended_year(), 5785);
assert_eq!(date.month().standard_code.0, "M06");
assert_eq!(date.day_of_month().0, 29);
ยง

Reject

Return an error if any fields are out of bounds.

ยงExamples

use icu::calendar::cal::Hebrew;
use icu::calendar::error::DateFromFieldsError;
use icu::calendar::options::DateFromFieldsOptions;
use icu::calendar::options::Overflow;
use icu::calendar::types::DateFields;
use icu::calendar::Date;
use tinystr::tinystr;

let mut options = DateFromFieldsOptions::default();
options.overflow = Some(Overflow::Reject);

// 5784, a leap year, contains M05L, but the day is too big.
let mut fields = DateFields::default();
fields.extended_year = Some(5784);
fields.month_code = Some(b"M05L");
fields.day = Some(50);

let err = Date::try_from_fields(fields, options, Hebrew)
    .expect_err("Day is out of bounds");
assert!(matches!(err, DateFromFieldsError::Range { .. }));

// Set the day to one that exists
fields.day = Some(1);
Date::try_from_fields(fields, options, Hebrew)
    .expect("A valid Hebrew date");

// 5785, a common year, does not contain M05L.
fields.extended_year = Some(5785);

let err = Date::try_from_fields(fields, options, Hebrew)
    .expect_err("Month is out of bounds");
assert!(matches!(err, DateFromFieldsError::MonthCodeNotInYear));

Trait Implementationsยง

Sourceยง

impl Clone for Overflow

Sourceยง

fn clone(&self) -> Overflow

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) ยท Sourceยง

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

Performs copy-assignment from source. Read more
Sourceยง

impl Debug for Overflow

Sourceยง

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

Formats the value using the given formatter. Read more
Sourceยง

impl Default for Overflow

Sourceยง

fn default() -> Overflow

Returns the โ€œdefault valueโ€ for a type. Read more
Sourceยง

impl PartialEq for Overflow

Sourceยง

fn eq(&self, other: &Overflow) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) ยท Sourceยง

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Sourceยง

impl Copy for Overflow

Sourceยง

impl StructuralPartialEq for Overflow

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.
Sourceยง

impl<T> ErasedDestructor for T
where T: 'static,