Skip to content

Commit

Permalink
Merge branch 'main' into duration-formatter-remove-allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
younies authored Aug 20, 2024
2 parents 42f2aa1 + 00b596b commit c351054
Show file tree
Hide file tree
Showing 1,395 changed files with 40,001 additions and 17,900 deletions.
9 changes: 4 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 7 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ icu_benchmark_macros = { path = "tools/benchmark/macros" }

# The version here can either be a `version = ".."` spec or `git = "https://github.com/rust-diplomat/diplomat", rev = ".."`
# Diplomat must be published preceding a new ICU4X release but may use git versions in between
diplomat = { git = "https://github.com/rust-diplomat/diplomat", rev = "8744ac97162341f347b63131969ad736e1047f6d" }
diplomat-runtime = { git = "https://github.com/rust-diplomat/diplomat", rev = "8744ac97162341f347b63131969ad736e1047f6d" }
diplomat_core = { git = "https://github.com/rust-diplomat/diplomat", rev = "8744ac97162341f347b63131969ad736e1047f6d" }
diplomat-tool = { git = "https://github.com/rust-diplomat/diplomat", rev = "8744ac97162341f347b63131969ad736e1047f6d" }
diplomat = { git = "https://github.com/rust-diplomat/diplomat", rev = "645c1872e6581074bc559fb72514eb8e96040fb9" }
diplomat-runtime = { git = "https://github.com/rust-diplomat/diplomat", rev = "645c1872e6581074bc559fb72514eb8e96040fb9" }
diplomat_core = { git = "https://github.com/rust-diplomat/diplomat", rev = "645c1872e6581074bc559fb72514eb8e96040fb9" }
diplomat-tool = { git = "https://github.com/rust-diplomat/diplomat", rev = "645c1872e6581074bc559fb72514eb8e96040fb9" }

# EXTERNAL DEPENDENCIES
#
Expand Down Expand Up @@ -245,7 +245,9 @@ memchr = { version = "2.6.0", default-features = false }
num-bigint = { version = "0.4.3", default-features = false }
num-rational = { version = "0.4.0", default-features = false }
num-traits = { version = "0.2.0", default-features = false }
postcard = { version = "1.0.1", default-features = false }
# Postcard 1.0.8 pulls in embedded_io by accident, and it doesn't
# look like that's being fixed: https://github.com/jamesmunns/postcard/issues/158
postcard = { version = "1.0.1, <1.0.8", default-features = false }
regex-automata = { version = "0.4.7", default-features = false }
ryu = { version = "1.0.5", default-features = false }
serde = { version = "1.0.110", default-features = false }
Expand Down
2 changes: 1 addition & 1 deletion components/datetime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ required-features = ["bench"]

[[test]]
name = "datetime"
required-features = ["compiled_data"]
required-features = ["experimental", "compiled_data"]

[[test]]
name = "resolved_components"
Expand Down
18 changes: 16 additions & 2 deletions components/datetime/src/format/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl<'l> fmt::Display for FormattedDateTime<'l> {
}
}

// Apply length to input number and write to result using fixed_decimal_format.
/// Apply length to input number and write to result using fixed_decimal_format.
fn try_write_number<W>(
result: &mut W,
fixed_decimal_format: Option<&FixedDecimalFormatter>,
Expand Down Expand Up @@ -386,7 +386,21 @@ where
})
}

Ok(match (field.symbol, field.length) {
let mut field_length = field.length;
if formatting_options.force_2_digit_month_day_week_hour
&& field_length == FieldLength::One
&& matches!(
field.symbol,
FieldSymbol::Month(_)
| FieldSymbol::Day(_)
| FieldSymbol::Week(_)
| FieldSymbol::Hour(_)
)
{
field_length = FieldLength::TwoDigit;
}

Ok(match (field.symbol, field_length) {
(FieldSymbol::Era, l) => match datetime.year() {
None => {
write_value_missing(w, field)?;
Expand Down
1 change: 1 addition & 0 deletions components/datetime/src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod zoned_datetime;
#[derive(Debug, Copy, Clone, Default)]
pub(crate) struct FormattingOptions {
pub(crate) hour_cycle: Option<HourCycle>,
pub(crate) force_2_digit_month_day_week_hour: bool,
#[cfg(feature = "experimental")]
pub(crate) fractional_second_digits: Option<FractionalSecondDigits>,
#[cfg(not(feature = "experimental"))]
Expand Down
11 changes: 11 additions & 0 deletions components/datetime/src/neo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ pub struct NeoOptions<R: DateTimeMarkers> {
///
/// See [`NeoSkeletonLength`].
pub length: R::LengthOption,
/// Whether fields should be aligned for a column-like layout,
/// if required for the chosen field set.
///
/// See [`Alignment`](crate::neo_skeleton::Alignment).
pub alignment: R::AlignmentOption,
/// When to display the era field in the formatted string,
/// if required for the chosen field set.
///
Expand All @@ -173,13 +178,15 @@ impl<R> From<NeoSkeletonLength> for NeoOptions<R>
where
R: DateTimeMarkers,
R::LengthOption: From<NeoSkeletonLength>,
R::AlignmentOption: Default,
R::EraDisplayOption: Default,
R::FractionalSecondDigitsOption: Default,
{
#[inline]
fn from(value: NeoSkeletonLength) -> Self {
NeoOptions {
length: value.into(),
alignment: Default::default(),
era_display: Default::default(),
fractional_second_digits: Default::default(),
}
Expand All @@ -192,13 +199,15 @@ impl<R> Default for NeoOptions<R>
where
R: DateTimeMarkers,
R::LengthOption: Default,
R::AlignmentOption: Default,
R::EraDisplayOption: Default,
R::FractionalSecondDigitsOption: Default,
{
#[inline]
fn default() -> Self {
NeoOptions {
length: Default::default(),
alignment: Default::default(),
era_display: Default::default(),
fractional_second_digits: Default::default(),
}
Expand Down Expand Up @@ -549,6 +558,7 @@ where
locale,
options.length.into(),
components,
options.alignment.into(),
options.era_display.into(),
options.fractional_second_digits.into(),
hour_cycle,
Expand Down Expand Up @@ -1256,6 +1266,7 @@ where
locale,
options.length.into(),
components,
options.alignment.into(),
options.era_display.into(),
options.fractional_second_digits.into(),
hour_cycle,
Expand Down
79 changes: 79 additions & 0 deletions components/datetime/src/neo_marker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,54 @@
//!
//! # Examples
//!
//! ## Alignment
//!
//! By default, datetimes are formatted for a variable-width context. You can
//! give a hint that the strings will be displayed in a column-like context,
//! which will coerce numerics to be padded with zeros.
//!
//! ```
//! use icu::calendar::Date;
//! use icu::calendar::Gregorian;
//! use icu::datetime::neo::NeoOptions;
//! use icu::datetime::neo::TypedNeoFormatter;
//! use icu::datetime::neo_marker::NeoYearMonthDayMarker;
//! use icu::datetime::neo_skeleton::Alignment;
//! use icu::datetime::neo_skeleton::NeoSkeletonLength;
//! use icu::locale::locale;
//! use writeable::assert_try_writeable_eq;
//!
//! let plain_formatter =
//! TypedNeoFormatter::<Gregorian, NeoYearMonthDayMarker>::try_new(
//! &locale!("en-US").into(),
//! NeoSkeletonLength::Short.into(),
//! )
//! .unwrap();
//!
//! let column_formatter =
//! TypedNeoFormatter::<Gregorian, NeoYearMonthDayMarker>::try_new(
//! &locale!("en-US").into(),
//! {
//! let mut options = NeoOptions::from(NeoSkeletonLength::Short);
//! options.alignment = Some(Alignment::Column);
//! options
//! }
//! )
//! .unwrap();
//!
//! // By default, en-US does not pad the month and day with zeros.
//! assert_try_writeable_eq!(
//! plain_formatter.format(&Date::try_new_gregorian_date(2025, 1, 1).unwrap()),
//! "1/1/25"
//! );
//!
//! // The column alignment option hints that they should be padded.
//! assert_try_writeable_eq!(
//! column_formatter.format(&Date::try_new_gregorian_date(2025, 1, 1).unwrap()),
//! "01/01/25"
//! );
//! ```
//!
//! ## Era Display
//!
//! The era field can be toggled on and off using the [`EraDisplay`] option.
Expand Down Expand Up @@ -776,6 +824,13 @@ impl From<NeverField> for Option<NeoSkeletonLength> {
}
}

impl From<NeverField> for Option<Alignment> {
#[inline]
fn from(_: NeverField) -> Self {
None
}
}

impl From<NeverField> for Option<EraDisplay> {
#[inline]
fn from(_: NeverField) -> Self {
Expand Down Expand Up @@ -918,6 +973,8 @@ pub trait DateTimeMarkers: private::Sealed + DateTimeNamesMarker {
type Z;
/// Type of the length option in the constructor.
type LengthOption: Into<Option<NeoSkeletonLength>>;
/// Type of the alignment option in the constructor.
type AlignmentOption: Into<Option<Alignment>>;
/// Type of the era display option in the constructor.
type EraDisplayOption: Into<Option<EraDisplay>>;
/// Type of the fractional seconds display option in the constructor.
Expand Down Expand Up @@ -1059,6 +1116,7 @@ where
type T = NeoNeverMarker;
type Z = NeoNeverMarker;
type LengthOption = NeoSkeletonLength; // always needed for date
type AlignmentOption = D::AlignmentOption;
type EraDisplayOption = D::EraDisplayOption;
type FractionalSecondDigitsOption = NeverField;
type GluePatternV1Marker = NeverMarker<GluePatternV1<'static>>;
Expand Down Expand Up @@ -1095,6 +1153,7 @@ where
type T = T;
type Z = NeoNeverMarker;
type LengthOption = NeoSkeletonLength; // always needed for time
type AlignmentOption = Option<Alignment>; // always needed for time
type EraDisplayOption = NeverField; // no year in a time-only format
type FractionalSecondDigitsOption = T::FractionalSecondDigitsOption;
type GluePatternV1Marker = NeverMarker<GluePatternV1<'static>>;
Expand Down Expand Up @@ -1131,6 +1190,7 @@ where
type T = NeoNeverMarker;
type Z = Z;
type LengthOption = Z::LengthOption; // no date or time: inherit from zone
type AlignmentOption = Z::AlignmentOption; // no date or time: inherit from zone
type EraDisplayOption = NeverField; // no year in a zone-only format
type FractionalSecondDigitsOption = NeverField;
type GluePatternV1Marker = GluePatternV1Marker;
Expand Down Expand Up @@ -1170,6 +1230,7 @@ where
type T = T;
type Z = NeoNeverMarker;
type LengthOption = NeoSkeletonLength; // always needed for date/time
type AlignmentOption = Option<Alignment>; // always needed for date/time
type EraDisplayOption = D::EraDisplayOption;
type FractionalSecondDigitsOption = T::FractionalSecondDigitsOption;
type GluePatternV1Marker = GluePatternV1Marker;
Expand Down Expand Up @@ -1213,6 +1274,7 @@ where
type T = T;
type Z = Z;
type LengthOption = NeoSkeletonLength; // always needed for date/time
type AlignmentOption = Option<Alignment>; // always needed for date/time
type EraDisplayOption = D::EraDisplayOption;
type FractionalSecondDigitsOption = T::FractionalSecondDigitsOption;
type GluePatternV1Marker = GluePatternV1Marker;
Expand Down Expand Up @@ -1272,6 +1334,15 @@ macro_rules! datetime_marker_helper {
(@option/eradisplay, yes) => {
Option<EraDisplay>
};
(@option/alignment, yes) => {
Option<Alignment>
};
(@option/alignment, no, no) => {
NeverField
};
(@option/alignment, $any0:ident, $any1:ident) => {
Option<Alignment>
};
(@option/fractionalsecondigits, yes) => {
Option<FractionalSecondDigits>
};
Expand Down Expand Up @@ -1512,6 +1583,7 @@ macro_rules! impl_date_marker {
type T = NeoNeverMarker;
type Z = NeoNeverMarker;
type LengthOption = datetime_marker_helper!(@option/length, yes);
type AlignmentOption = datetime_marker_helper!(@option/alignment, $months_yesno, $dates_yesno);
type EraDisplayOption = datetime_marker_helper!(@option/eradisplay, $year_yesno);
type FractionalSecondDigitsOption = datetime_marker_helper!(@option/fractionalsecondigits, no);
type GluePatternV1Marker = datetime_marker_helper!(@glue, no);
Expand Down Expand Up @@ -1659,6 +1731,7 @@ macro_rules! impl_time_marker {
type T = Self;
type Z = NeoNeverMarker;
type LengthOption = datetime_marker_helper!(@option/length, yes);
type AlignmentOption = datetime_marker_helper!(@option/alignment, yes);
type EraDisplayOption = datetime_marker_helper!(@option/eradisplay, no);
type FractionalSecondDigitsOption = datetime_marker_helper!(@option/fractionalsecondigits, $nanosecond_yesno);
type GluePatternV1Marker = datetime_marker_helper!(@glue, no);
Expand Down Expand Up @@ -1785,6 +1858,7 @@ macro_rules! impl_zone_marker {
type T = NeoNeverMarker;
type Z = Self;
type LengthOption = datetime_marker_helper!(@option/length, $sample_length);
type AlignmentOption = datetime_marker_helper!(@option/alignment, no);
type EraDisplayOption = datetime_marker_helper!(@option/eradisplay, no);
type FractionalSecondDigitsOption = datetime_marker_helper!(@option/fractionalsecondigits, no);
type GluePatternV1Marker = datetime_marker_helper!(@glue, no);
Expand Down Expand Up @@ -2321,6 +2395,7 @@ impl DateTimeMarkers for NeoDateComponents {
type T = NeoNeverMarker;
type Z = NeoNeverMarker;
type LengthOption = datetime_marker_helper!(@option/length, yes);
type AlignmentOption = datetime_marker_helper!(@option/alignment, yes);
type EraDisplayOption = datetime_marker_helper!(@option/eradisplay, yes);
type FractionalSecondDigitsOption = datetime_marker_helper!(@option/fractionalsecondigits, no);
type GluePatternV1Marker = datetime_marker_helper!(@glue, no);
Expand Down Expand Up @@ -2357,6 +2432,7 @@ impl DateTimeMarkers for NeoTimeComponents {
type T = Self;
type Z = NeoNeverMarker;
type LengthOption = datetime_marker_helper!(@option/length, yes);
type AlignmentOption = datetime_marker_helper!(@option/alignment, yes);
type EraDisplayOption = datetime_marker_helper!(@option/eradisplay, no);
type FractionalSecondDigitsOption = datetime_marker_helper!(@option/fractionalsecondigits, yes);
type GluePatternV1Marker = datetime_marker_helper!(@glue, no);
Expand Down Expand Up @@ -2394,6 +2470,7 @@ impl DateTimeMarkers for NeoTimeZoneSkeleton {
type T = NeoNeverMarker;
type Z = Self;
type LengthOption = datetime_marker_helper!(@option/length, yes);
type AlignmentOption = datetime_marker_helper!(@option/alignment, no);
type EraDisplayOption = datetime_marker_helper!(@option/eradisplay, no);
type FractionalSecondDigitsOption = datetime_marker_helper!(@option/fractionalsecondigits, no);
type GluePatternV1Marker = datetime_marker_helper!(@glue, no);
Expand Down Expand Up @@ -2421,6 +2498,7 @@ impl DateTimeMarkers for NeoDateTimeComponents {
type T = NeoTimeComponents;
type Z = NeoNeverMarker;
type LengthOption = datetime_marker_helper!(@option/length, yes);
type AlignmentOption = datetime_marker_helper!(@option/alignment, yes);
type EraDisplayOption = datetime_marker_helper!(@option/eradisplay, yes);
type FractionalSecondDigitsOption = datetime_marker_helper!(@option/fractionalsecondigits, yes);
type GluePatternV1Marker = datetime_marker_helper!(@glue, yes);
Expand Down Expand Up @@ -2448,6 +2526,7 @@ impl DateTimeMarkers for NeoComponents {
type T = NeoTimeComponents;
type Z = NeoTimeZoneSkeleton;
type LengthOption = datetime_marker_helper!(@option/length, yes);
type AlignmentOption = datetime_marker_helper!(@option/alignment, yes);
type EraDisplayOption = datetime_marker_helper!(@option/eradisplay, yes);
type FractionalSecondDigitsOption = datetime_marker_helper!(@option/fractionalsecondigits, yes);
type GluePatternV1Marker = datetime_marker_helper!(@glue, yes);
Expand Down
Loading

0 comments on commit c351054

Please sign in to comment.