#[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 PartialEq for Overflow
impl PartialEq for Overflow
impl Copy for Overflow
impl StructuralPartialEq for Overflow
Auto Trait Implementationsยง
impl Freeze for Overflow
impl RefUnwindSafe for Overflow
impl Send for Overflow
impl Sync for Overflow
impl Unpin for Overflow
impl UnsafeUnpin for Overflow
impl UnwindSafe for Overflow
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
Mutably borrows from an owned value. Read more