Skip to content

Commit a278f32

Browse files
authored
Rename method to set_date_fields_unchecked (#6488)
This cleans up the format_unchecked invariants a little.
1 parent f952b03 commit a278f32

File tree

5 files changed

+28
-14
lines changed

5 files changed

+28
-14
lines changed

components/datetime/src/format/input.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,13 @@ pub struct DateTimeInputUnchecked {
6161

6262
impl DateTimeInputUnchecked {
6363
/// Sets all fields from a [`Date`] input.
64-
pub fn set_date_fields<C: Calendar, A: AsCalendar<Calendar = C>>(&mut self, input: Date<A>) {
64+
///
65+
/// This method does not check the calendar of the date! The caller is
66+
/// responsible for making sure the calendar matches the formatter.
67+
pub fn set_date_fields_unchecked<C: Calendar, A: AsCalendar<Calendar = C>>(
68+
&mut self,
69+
input: Date<A>,
70+
) {
6571
self.year = Some(input.year());
6672
self.month = Some(input.month());
6773
self.day_of_month = Some(input.day_of_month());

components/datetime/src/neo.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -404,11 +404,15 @@ impl<C: CldrCalendar, FSet: DateTimeNamesMarker> FixedCalendarDateTimeFormatter<
404404
/// .cast_into_fset::<CompositeFieldSet>();
405405
///
406406
/// // Create a date and convert it to the correct calendar:
407-
/// let date = Date::try_new_iso(2025, 3, 7).unwrap().to_calendar(Buddhist);
408-
///
409-
/// // Extract the fields and use it with format_unchecked:
410407
/// let mut input = DateTimeInputUnchecked::default();
411-
/// input.set_date_fields(date);
408+
/// let date = Date::try_new_iso(2025, 3, 7)
409+
/// .unwrap()
410+
/// .to_calendar(Buddhist);
411+
///
412+
/// // Safe because the calendar matches the formatter:
413+
/// input.set_date_fields_unchecked(date);
414+
///
415+
/// // Safe because YMD needs only date fields, which are in the input:
412416
/// let result = formatter.format_unchecked(input);
413417
///
414418
/// assert_try_writeable_eq!(result, "7 มีนาคม 2568");
@@ -810,11 +814,15 @@ impl<FSet: DateTimeNamesMarker> DateTimeFormatter<FSet> {
810814
/// .cast_into_fset::<CompositeFieldSet>();
811815
///
812816
/// // Create a date and convert it to the correct calendar:
813-
/// let date = Date::try_new_iso(2025, 3, 7).unwrap().to_calendar(formatter.calendar());
814-
///
815-
/// // Extract the fields and use it with format_unchecked:
816817
/// let mut input = DateTimeInputUnchecked::default();
817-
/// input.set_date_fields(date);
818+
/// let date = Date::try_new_iso(2025, 3, 7)
819+
/// .unwrap()
820+
/// .to_calendar(formatter.calendar());
821+
///
822+
/// // Safe because the calendar matches the formatter:
823+
/// input.set_date_fields_unchecked(date);
824+
///
825+
/// // Safe because YMD needs only date fields, which are in the input:
818826
/// let result = formatter.format_unchecked(input);
819827
///
820828
/// assert_try_writeable_eq!(result, "7 มีนาคม 2568");

ffi/capi/src/zoned_date_formatter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ pub mod ffi {
518518
) -> Result<(), DateTimeWriteError> {
519519
let mut input = icu_datetime::DateTimeInputUnchecked::default();
520520
let date = date.0.to_calendar(self.0.calendar());
521-
input.set_date_fields(date);
521+
input.set_date_fields_unchecked(date); // calendar conversion on previous line
522522
input.set_time_zone_id(zone.id);
523523
if let Some(offset) = zone.offset {
524524
input.set_time_zone_utc_offset(offset);
@@ -1010,7 +1010,7 @@ pub mod ffi {
10101010
) -> Result<(), DateTimeWriteError> {
10111011
let mut input = icu_datetime::DateTimeInputUnchecked::default();
10121012
let date = date.0.to_calendar(Gregorian);
1013-
input.set_date_fields(date);
1013+
input.set_date_fields_unchecked(date); // calendar conversion on previous line
10141014
input.set_time_zone_id(zone.id);
10151015
if let Some(offset) = zone.offset {
10161016
input.set_time_zone_utc_offset(offset);

ffi/capi/src/zoned_date_time_formatter.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,7 @@ pub mod ffi {
519519
) -> Result<(), DateTimeWriteError> {
520520
let mut input = icu_datetime::DateTimeInputUnchecked::default();
521521
let date = date.0.to_calendar(self.0.calendar());
522-
input.set_date_fields(date);
522+
input.set_date_fields_unchecked(date); // calendar conversion on previous line
523523
input.set_time_fields(time.0);
524524
input.set_time_zone_id(zone.id);
525525
if let Some(offset) = zone.offset {
@@ -1013,7 +1013,7 @@ pub mod ffi {
10131013
) -> Result<(), DateTimeWriteError> {
10141014
let mut input = icu_datetime::DateTimeInputUnchecked::default();
10151015
let date = date.0.to_calendar(Gregorian);
1016-
input.set_date_fields(date);
1016+
input.set_date_fields_unchecked(date); // calendar conversion on previous line
10171017
input.set_time_fields(time.0);
10181018
input.set_time_zone_id(zone.id);
10191019
if let Some(offset) = zone.offset {

tools/make/codegen/templates/zoned_formatter.rs.jinja

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ pub mod ffi {
194194
{%- else %}
195195
let date = date.0.to_calendar(self.0.calendar());
196196
{%- endif %}
197-
input.set_date_fields(date);
197+
input.set_date_fields_unchecked(date); // calendar conversion on previous line
198198
{%- endif %}
199199
{%- if flavor.has_time() %}
200200
input.set_time_fields(time.0);

0 commit comments

Comments
 (0)