diff --git a/src/format/formatting.rs b/src/format/formatting.rs index dfeb9b04af..d375a09024 100644 --- a/src/format/formatting.rs +++ b/src/format/formatting.rs @@ -190,7 +190,9 @@ impl<'a, J, Tz: TimeZone> FormattingSpec, J> { } } -impl<'a, Tz: TimeZone> FormattingSpec, &'a [Item<'a>]> { +// TODO: once our MSRV >= 1.61 change this impl to take a generic `Tz` trait bound. +// Trait bounds on const fn parameters were not supported before. +impl<'a> FormattingSpec, &'a [Item<'a>]> { /// Creates a new `FormattingSpec` given a slice of [`Item`]'s. /// /// The difference with the more generic [`FormattingSpec::from_items`] is that `from_slice` is @@ -273,6 +275,37 @@ impl<'a, Tz: TimeZone> FormattingSpec, &'a [Item<'a>]> { } } +impl<'a> FormattingSpec, &'a [Item<'a>]> { + /// Creates a new `FormattingSpec` given a slice of [`Item`]'s. + pub const fn from_slice(items: &'a [Item<'a>]) -> Result { + let locale = locales::default_locale(); + let mut i = 0; + while i < items.len() { + if let Err(e) = items[i].check_fields(true, true, true, locale) { + return Err(e); + } + i += 1; + } + Ok(FormattingSpec { items, date_time_type: PhantomData, locale }) + } + + /// Creates a new `FormattingSpec` given a slice of [`Item`]'s and a `locale`. + #[cfg(feature = "unstable-locales")] + pub const fn from_slice_localized( + items: &'a [Item<'a>], + locale: Locale, + ) -> Result { + let mut i = 0; + while i < items.len() { + if let Err(e) = items[i].check_fields(true, true, true, locale) { + return Err(e); + } + i += 1; + } + Ok(FormattingSpec { items, date_time_type: PhantomData, locale }) + } +} + macro_rules! formatting_spec_impls { ($type:ty, $date:literal, $time:literal, $off:literal) => { impl<'a, J> FormattingSpec<$type, J> {