Skip to content

Commit

Permalink
Refactor NamesContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
sffc committed Dec 20, 2024
1 parent 451c30c commit 736c567
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 67 deletions.
115 changes: 70 additions & 45 deletions components/datetime/src/pattern/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,41 +228,30 @@ pub struct TypedDateTimeNames<
}

pub(crate) struct RawDateTimeNames<FSet: DateTimeNamesMarker> {
year_names:
<FSet::YearNames as DateTimeNamesHolderTrait<YearNamesV1Marker>>::Container<FieldLength>,
month_names: <FSet::MonthNames as DateTimeNamesHolderTrait<MonthNamesV1Marker>>::Container<(
fields::Month,
FieldLength,
)>,
weekday_names:
<FSet::WeekdayNames as DateTimeNamesHolderTrait<WeekdayNamesV1Marker>>::Container<(
fields::Weekday,
FieldLength,
)>,
year_names: <FSet::YearNames as NamesContainer<YearNamesV1Marker, FieldLength>>::Container,
month_names: <FSet::MonthNames as NamesContainer<
MonthNamesV1Marker,
(fields::Month, FieldLength),
>>::Container,
weekday_names: <FSet::WeekdayNames as NamesContainer<
WeekdayNamesV1Marker,
(fields::Weekday, FieldLength),
>>::Container,
dayperiod_names:
<FSet::DayPeriodNames as DateTimeNamesHolderTrait<DayPeriodNamesV1Marker>>::Container<
FieldLength,
>,
<FSet::DayPeriodNames as NamesContainer<DayPeriodNamesV1Marker, FieldLength>>::Container,
zone_essentials:
<FSet::ZoneEssentials as DateTimeNamesHolderTrait<tz::EssentialsV1Marker>>::Container<()>,
locations_root:
<FSet::ZoneLocations as DateTimeNamesHolderTrait<tz::LocationsV1Marker>>::Container<()>,
locations:
<FSet::ZoneLocations as DateTimeNamesHolderTrait<tz::LocationsV1Marker>>::Container<()>,
mz_generic_long: <FSet::ZoneGenericLong as DateTimeNamesHolderTrait<
tz::MzGenericLongV1Marker,
>>::Container<()>,
mz_generic_short: <FSet::ZoneGenericShort as DateTimeNamesHolderTrait<
tz::MzGenericShortV1Marker,
>>::Container<()>,
mz_specific_long: <FSet::ZoneSpecificLong as DateTimeNamesHolderTrait<
tz::MzSpecificLongV1Marker,
>>::Container<()>,
mz_specific_short: <FSet::ZoneSpecificShort as DateTimeNamesHolderTrait<
tz::MzSpecificShortV1Marker,
>>::Container<()>,
mz_periods:
<FSet::MetazoneLookup as DateTimeNamesHolderTrait<tz::MzPeriodV1Marker>>::Container<()>,
<FSet::ZoneEssentials as NamesContainer<tz::EssentialsV1Marker, ()>>::Container,
locations_root: <FSet::ZoneLocations as NamesContainer<tz::LocationsV1Marker, ()>>::Container,
locations: <FSet::ZoneLocations as NamesContainer<tz::LocationsV1Marker, ()>>::Container,
mz_generic_long:
<FSet::ZoneGenericLong as NamesContainer<tz::MzGenericLongV1Marker, ()>>::Container,
mz_generic_short:
<FSet::ZoneGenericShort as NamesContainer<tz::MzGenericShortV1Marker, ()>>::Container,
mz_specific_long:
<FSet::ZoneSpecificLong as NamesContainer<tz::MzSpecificLongV1Marker, ()>>::Container,
mz_specific_short:
<FSet::ZoneSpecificShort as NamesContainer<tz::MzSpecificShortV1Marker, ()>>::Container,
mz_periods: <FSet::MetazoneLookup as NamesContainer<tz::MzPeriodV1Marker, ()>>::Container,
// TODO(#4340): Make the FixedDecimalFormatter optional
fixed_decimal_formatter: Option<FixedDecimalFormatter>,
_marker: PhantomData<FSet>,
Expand Down Expand Up @@ -1315,18 +1304,54 @@ impl<C: CldrCalendar, FSet: DateTimeNamesMarker> TypedDateTimeNames<C, FSet> {
impl<FSet: DateTimeNamesMarker> RawDateTimeNames<FSet> {
pub(crate) fn new_without_number_formatting() -> Self {
Self {
year_names: <FSet::YearNames as DateTimeNamesHolderTrait<YearNamesV1Marker>>::Container::<_>::new_empty(),
month_names: <FSet::MonthNames as DateTimeNamesHolderTrait<MonthNamesV1Marker>>::Container::<_>::new_empty(),
weekday_names: <FSet::WeekdayNames as DateTimeNamesHolderTrait<WeekdayNamesV1Marker>>::Container::<_>::new_empty(),
dayperiod_names: <FSet::DayPeriodNames as DateTimeNamesHolderTrait<DayPeriodNamesV1Marker>>::Container::<_>::new_empty(),
zone_essentials: <FSet::ZoneEssentials as DateTimeNamesHolderTrait<tz::EssentialsV1Marker>>::Container::<_>::new_empty(),
locations_root: <FSet::ZoneLocations as DateTimeNamesHolderTrait<tz::LocationsV1Marker>>::Container::<_>::new_empty(),
locations: <FSet::ZoneLocations as DateTimeNamesHolderTrait<tz::LocationsV1Marker>>::Container::<_>::new_empty(),
mz_generic_long: <FSet::ZoneGenericLong as DateTimeNamesHolderTrait<tz::MzGenericLongV1Marker>>::Container::<_>::new_empty(),
mz_generic_short: <FSet::ZoneGenericShort as DateTimeNamesHolderTrait<tz::MzGenericShortV1Marker>>::Container::<_>::new_empty(),
mz_specific_long: <FSet::ZoneSpecificLong as DateTimeNamesHolderTrait<tz::MzSpecificLongV1Marker>>::Container::<_>::new_empty(),
mz_specific_short: <FSet::ZoneSpecificShort as DateTimeNamesHolderTrait<tz::MzSpecificShortV1Marker>>::Container::<_>::new_empty(),
mz_periods: <FSet::MetazoneLookup as DateTimeNamesHolderTrait<tz::MzPeriodV1Marker>>::Container::<_>::new_empty(),
year_names: <FSet::YearNames as NamesContainer<
YearNamesV1Marker,
FieldLength,
>>::Container::new_empty(),
month_names: <FSet::MonthNames as NamesContainer<
MonthNamesV1Marker,
(fields::Month, FieldLength),
>>::Container::new_empty(),
weekday_names: <FSet::WeekdayNames as NamesContainer<
WeekdayNamesV1Marker,
(fields::Weekday, FieldLength),
>>::Container::new_empty(),
dayperiod_names: <FSet::DayPeriodNames as NamesContainer<
DayPeriodNamesV1Marker,
FieldLength,
>>::Container::new_empty(),
zone_essentials: <FSet::ZoneEssentials as NamesContainer<
tz::EssentialsV1Marker,
(),
>>::Container::new_empty(),
locations_root: <FSet::ZoneLocations as NamesContainer<
tz::LocationsV1Marker,
(),
>>::Container::new_empty(),
locations: <FSet::ZoneLocations as NamesContainer<
tz::LocationsV1Marker,
(),
>>::Container::new_empty(),
mz_generic_long: <FSet::ZoneGenericLong as NamesContainer<
tz::MzGenericLongV1Marker,
(),
>>::Container::new_empty(),
mz_generic_short: <FSet::ZoneGenericShort as NamesContainer<
tz::MzGenericShortV1Marker,
(),
>>::Container::new_empty(),
mz_specific_long: <FSet::ZoneSpecificLong as NamesContainer<
tz::MzSpecificLongV1Marker,
(),
>>::Container::new_empty(),
mz_specific_short: <FSet::ZoneSpecificShort as NamesContainer<
tz::MzSpecificShortV1Marker,
(),
>>::Container::new_empty(),
mz_periods: <FSet::MetazoneLookup as NamesContainer<
tz::MzPeriodV1Marker,
(),
>>::Container::new_empty(),
fixed_decimal_formatter: None,
_marker: PhantomData,
}
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/src/scaffold/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
49 changes: 28 additions & 21 deletions components/datetime/src/scaffold/names_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<YearNamesV1Marker>;
type MonthNames: DateTimeNamesHolderTrait<MonthNamesV1Marker>;
type WeekdayNames: DateTimeNamesHolderTrait<WeekdayNamesV1Marker>;
type DayPeriodNames: DateTimeNamesHolderTrait<DayPeriodNamesV1Marker>;
type ZoneEssentials: DateTimeNamesHolderTrait<tz::EssentialsV1Marker>;
type ZoneLocations: DateTimeNamesHolderTrait<tz::LocationsV1Marker>;
type ZoneGenericLong: DateTimeNamesHolderTrait<tz::MzGenericLongV1Marker>;
type ZoneGenericShort: DateTimeNamesHolderTrait<tz::MzGenericShortV1Marker>;
type ZoneSpecificLong: DateTimeNamesHolderTrait<tz::MzSpecificLongV1Marker>;
type ZoneSpecificShort: DateTimeNamesHolderTrait<tz::MzSpecificShortV1Marker>;
type MetazoneLookup: DateTimeNamesHolderTrait<tz::MzPeriodV1Marker>;
type YearNames: NamesContainer<YearNamesV1Marker, FieldLength>;
type MonthNames: NamesContainer<MonthNamesV1Marker, (fields::Month, FieldLength)>;
type WeekdayNames: NamesContainer<WeekdayNamesV1Marker, (fields::Weekday, FieldLength)>;
type DayPeriodNames: NamesContainer<DayPeriodNamesV1Marker, FieldLength>;
type ZoneEssentials: NamesContainer<tz::EssentialsV1Marker, ()>;
type ZoneLocations: NamesContainer<tz::LocationsV1Marker, ()>;
type ZoneGenericLong: NamesContainer<tz::MzGenericLongV1Marker, ()>;
type ZoneGenericShort: NamesContainer<tz::MzGenericShortV1Marker, ()>;
type ZoneSpecificLong: NamesContainer<tz::MzSpecificLongV1Marker, ()>;
type ZoneSpecificShort: NamesContainer<tz::MzSpecificShortV1Marker, ()>;
type MetazoneLookup: NamesContainer<tz::MzPeriodV1Marker, ()>;
}

/// Helper trait for [`DateTimeNamesMarker`].
/// Trait that associates a container for a payload parameterized by the given variables.
#[allow(missing_docs)]
pub trait DateTimeNamesHolderTrait<M: DynamicDataMarker>: UnstableSealed {
type Container<Variables: PartialEq + Copy + fmt::Debug>: MaybePayload<M, Variables>
+ fmt::Debug;
pub trait NamesContainer<M: DynamicDataMarker, Variables>: UnstableSealed
where
Variables: PartialEq + Copy + fmt::Debug,
{
type Container: MaybePayload<M, Variables> + fmt::Debug;
}

impl<M: DynamicDataMarker> DateTimeNamesHolderTrait<M> for () {
type Container<Variables: PartialEq + Copy + fmt::Debug> = ();
impl<M: DynamicDataMarker, Variables> NamesContainer<M, Variables> 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<Variables: PartialEq + Copy + fmt::Debug> =
DataPayloadWithVariables<$marker, Variables>;
impl<Variables> NamesContainer<$marker, Variables> for $marker
where
Variables: PartialEq + Copy + fmt::Debug,
{
type Container = DataPayloadWithVariables<$marker, Variables>;
}
};
}
Expand Down

0 comments on commit 736c567

Please sign in to comment.