From 736c5676711f4aa154832cd56895ab15ae063c7f Mon Sep 17 00:00:00 2001 From: "Shane F. Carr" Date: Fri, 20 Dec 2024 13:35:34 -0800 Subject: [PATCH] Refactor NamesContainer --- components/datetime/src/pattern/names.rs | 115 +++++++++++------- components/datetime/src/scaffold/mod.rs | 2 +- .../datetime/src/scaffold/names_storage.rs | 49 ++++---- 3 files changed, 99 insertions(+), 67 deletions(-) diff --git a/components/datetime/src/pattern/names.rs b/components/datetime/src/pattern/names.rs index d6a97b83ad8..8159f0c2d2f 100644 --- a/components/datetime/src/pattern/names.rs +++ b/components/datetime/src/pattern/names.rs @@ -228,41 +228,30 @@ pub struct TypedDateTimeNames< } pub(crate) struct RawDateTimeNames { - year_names: - >::Container, - month_names: >::Container<( - fields::Month, - FieldLength, - )>, - weekday_names: - >::Container<( - fields::Weekday, - FieldLength, - )>, + year_names: >::Container, + month_names: >::Container, + weekday_names: >::Container, dayperiod_names: - >::Container< - FieldLength, - >, + >::Container, zone_essentials: - >::Container<()>, - locations_root: - >::Container<()>, - locations: - >::Container<()>, - mz_generic_long: >::Container<()>, - mz_generic_short: >::Container<()>, - mz_specific_long: >::Container<()>, - mz_specific_short: >::Container<()>, - mz_periods: - >::Container<()>, + >::Container, + locations_root: >::Container, + locations: >::Container, + mz_generic_long: + >::Container, + mz_generic_short: + >::Container, + mz_specific_long: + >::Container, + mz_specific_short: + >::Container, + mz_periods: >::Container, // TODO(#4340): Make the FixedDecimalFormatter optional fixed_decimal_formatter: Option, _marker: PhantomData, @@ -1315,18 +1304,54 @@ impl TypedDateTimeNames { impl RawDateTimeNames { pub(crate) fn new_without_number_formatting() -> Self { Self { - year_names: >::Container::<_>::new_empty(), - month_names: >::Container::<_>::new_empty(), - weekday_names: >::Container::<_>::new_empty(), - dayperiod_names: >::Container::<_>::new_empty(), - zone_essentials: >::Container::<_>::new_empty(), - locations_root: >::Container::<_>::new_empty(), - locations: >::Container::<_>::new_empty(), - mz_generic_long: >::Container::<_>::new_empty(), - mz_generic_short: >::Container::<_>::new_empty(), - mz_specific_long: >::Container::<_>::new_empty(), - mz_specific_short: >::Container::<_>::new_empty(), - mz_periods: >::Container::<_>::new_empty(), + year_names: >::Container::new_empty(), + month_names: >::Container::new_empty(), + weekday_names: >::Container::new_empty(), + dayperiod_names: >::Container::new_empty(), + zone_essentials: >::Container::new_empty(), + locations_root: >::Container::new_empty(), + locations: >::Container::new_empty(), + mz_generic_long: >::Container::new_empty(), + mz_generic_short: >::Container::new_empty(), + mz_specific_long: >::Container::new_empty(), + mz_specific_short: >::Container::new_empty(), + mz_periods: >::Container::new_empty(), fixed_decimal_formatter: None, _marker: PhantomData, } diff --git a/components/datetime/src/scaffold/mod.rs b/components/datetime/src/scaffold/mod.rs index 96e93eec41c..7497242d2a0 100644 --- a/components/datetime/src/scaffold/mod.rs +++ b/components/datetime/src/scaffold/mod.rs @@ -39,10 +39,10 @@ pub use get_field::GetField; pub use names_storage::DataPayloadWithVariables; pub use names_storage::DataPayloadWithVariablesBorrowed; -pub use names_storage::DateTimeNamesHolderTrait; pub use names_storage::DateTimeNamesMarker; pub use names_storage::MaybePayload; pub use names_storage::MaybePayloadError; +pub use names_storage::NamesContainer; pub(crate) use names_storage::OptionalNames; /// Trait marking other traits that are considered unstable and should not generally be diff --git a/components/datetime/src/scaffold/names_storage.rs b/components/datetime/src/scaffold/names_storage.rs index fd6bcc8e345..46dea7201f1 100644 --- a/components/datetime/src/scaffold/names_storage.rs +++ b/components/datetime/src/scaffold/names_storage.rs @@ -2,7 +2,7 @@ // called LICENSE at the top level of the ICU4X source tree // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). -use crate::fields::Field; +use crate::fields::{self, Field, FieldLength}; use crate::pattern::PatternLoadError; use crate::provider::neo::*; use crate::provider::time_zones::tz; @@ -19,36 +19,43 @@ use super::UnstableSealed; /// not weekday, day period, or time zone names. #[allow(missing_docs)] pub trait DateTimeNamesMarker: UnstableSealed { - type YearNames: DateTimeNamesHolderTrait; - type MonthNames: DateTimeNamesHolderTrait; - type WeekdayNames: DateTimeNamesHolderTrait; - type DayPeriodNames: DateTimeNamesHolderTrait; - type ZoneEssentials: DateTimeNamesHolderTrait; - type ZoneLocations: DateTimeNamesHolderTrait; - type ZoneGenericLong: DateTimeNamesHolderTrait; - type ZoneGenericShort: DateTimeNamesHolderTrait; - type ZoneSpecificLong: DateTimeNamesHolderTrait; - type ZoneSpecificShort: DateTimeNamesHolderTrait; - type MetazoneLookup: DateTimeNamesHolderTrait; + type YearNames: NamesContainer; + type MonthNames: NamesContainer; + type WeekdayNames: NamesContainer; + type DayPeriodNames: NamesContainer; + type ZoneEssentials: NamesContainer; + type ZoneLocations: NamesContainer; + type ZoneGenericLong: NamesContainer; + type ZoneGenericShort: NamesContainer; + type ZoneSpecificLong: NamesContainer; + type ZoneSpecificShort: NamesContainer; + type MetazoneLookup: NamesContainer; } -/// Helper trait for [`DateTimeNamesMarker`]. +/// Trait that associates a container for a payload parameterized by the given variables. #[allow(missing_docs)] -pub trait DateTimeNamesHolderTrait: UnstableSealed { - type Container: MaybePayload - + fmt::Debug; +pub trait NamesContainer: UnstableSealed +where + Variables: PartialEq + Copy + fmt::Debug, +{ + type Container: MaybePayload + fmt::Debug; } -impl DateTimeNamesHolderTrait for () { - type Container = (); +impl NamesContainer for () +where + Variables: PartialEq + Copy + fmt::Debug, +{ + type Container = (); } macro_rules! impl_holder_trait { ($marker:path) => { impl UnstableSealed for $marker {} - impl DateTimeNamesHolderTrait<$marker> for $marker { - type Container = - DataPayloadWithVariables<$marker, Variables>; + impl NamesContainer<$marker, Variables> for $marker + where + Variables: PartialEq + Copy + fmt::Debug, + { + type Container = DataPayloadWithVariables<$marker, Variables>; } }; }