Skip to content

Commit

Permalink
Use const generic defaults
Browse files Browse the repository at this point in the history
This also permits arbitrary ordering of const generics and type generics
  • Loading branch information
jhpratt committed Aug 10, 2022
1 parent e920d0a commit c0ae430
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 144 deletions.
2 changes: 1 addition & 1 deletion src/format_description/well_known/iso8601.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const DEFAULT_CONFIG: EncodedConfig = Config::DEFAULT.encode();
/// # Ok::<_, time::Error>(())
/// ```
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct Iso8601<const CONFIG: EncodedConfig>;
pub struct Iso8601<const CONFIG: EncodedConfig = DEFAULT_CONFIG>;

impl<const CONFIG: EncodedConfig> core::fmt::Debug for Iso8601<CONFIG> {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
Expand Down
50 changes: 25 additions & 25 deletions src/formatting/formattable.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! A trait that can be used to format an item from its components.
//! A trait that can d, be use to format an item from its components.

use core::ops::Deref;
use std::io;
Expand Down Expand Up @@ -135,22 +135,22 @@ impl sealed::Sealed for Rfc2822 {
&WEEKDAY_NAMES[date.weekday().number_days_from_monday() as usize][..3],
)?;
bytes += write(output, b", ")?;
bytes += format_number_pad_zero::<_, _, 2>(output, day)?;
bytes += format_number_pad_zero::<2, _, _>(output, day)?;
bytes += write(output, b" ")?;
bytes += write(output, &MONTH_NAMES[month as usize - 1][..3])?;
bytes += write(output, b" ")?;
bytes += format_number_pad_zero::<_, _, 4>(output, year as u32)?;
bytes += format_number_pad_zero::<4, _, _>(output, year as u32)?;
bytes += write(output, b" ")?;
bytes += format_number_pad_zero::<_, _, 2>(output, time.hour())?;
bytes += format_number_pad_zero::<2, _, _>(output, time.hour())?;
bytes += write(output, b":")?;
bytes += format_number_pad_zero::<_, _, 2>(output, time.minute())?;
bytes += format_number_pad_zero::<2, _, _>(output, time.minute())?;
bytes += write(output, b":")?;
bytes += format_number_pad_zero::<_, _, 2>(output, time.second())?;
bytes += format_number_pad_zero::<2, _, _>(output, time.second())?;
bytes += write(output, b" ")?;
bytes += write(output, if offset.is_negative() { b"-" } else { b"+" })?;
bytes += format_number_pad_zero::<_, _, 2>(output, offset.whole_hours().unsigned_abs())?;
bytes += format_number_pad_zero::<2, _, _>(output, offset.whole_hours().unsigned_abs())?;
bytes +=
format_number_pad_zero::<_, _, 2>(output, offset.minutes_past_hour().unsigned_abs())?;
format_number_pad_zero::<2, _, _>(output, offset.minutes_past_hour().unsigned_abs())?;

Ok(bytes)
}
Expand Down Expand Up @@ -179,40 +179,40 @@ impl sealed::Sealed for Rfc3339 {
return Err(error::Format::InvalidComponent("offset_second"));
}

bytes += format_number_pad_zero::<_, _, 4>(output, year as u32)?;
bytes += format_number_pad_zero::<4, _, _>(output, year as u32)?;
bytes += write(output, &[b'-'])?;
bytes += format_number_pad_zero::<_, _, 2>(output, date.month() as u8)?;
bytes += format_number_pad_zero::<2, _, _>(output, date.month() as u8)?;
bytes += write(output, &[b'-'])?;
bytes += format_number_pad_zero::<_, _, 2>(output, date.day())?;
bytes += format_number_pad_zero::<2, _, _>(output, date.day())?;
bytes += write(output, &[b'T'])?;
bytes += format_number_pad_zero::<_, _, 2>(output, time.hour())?;
bytes += format_number_pad_zero::<2, _, _>(output, time.hour())?;
bytes += write(output, &[b':'])?;
bytes += format_number_pad_zero::<_, _, 2>(output, time.minute())?;
bytes += format_number_pad_zero::<2, _, _>(output, time.minute())?;
bytes += write(output, &[b':'])?;
bytes += format_number_pad_zero::<_, _, 2>(output, time.second())?;
bytes += format_number_pad_zero::<2, _, _>(output, time.second())?;

#[allow(clippy::if_not_else)]
if time.nanosecond() != 0 {
let nanos = time.nanosecond();
bytes += write(output, &[b'.'])?;
bytes += if nanos % 10 != 0 {
format_number_pad_zero::<_, _, 9>(output, nanos)
format_number_pad_zero::<9, _, _>(output, nanos)
} else if (nanos / 10) % 10 != 0 {
format_number_pad_zero::<_, _, 8>(output, nanos / 10)
format_number_pad_zero::<8, _, _>(output, nanos / 10)
} else if (nanos / 100) % 10 != 0 {
format_number_pad_zero::<_, _, 7>(output, nanos / 100)
format_number_pad_zero::<7, _, _>(output, nanos / 100)
} else if (nanos / 1_000) % 10 != 0 {
format_number_pad_zero::<_, _, 6>(output, nanos / 1_000)
format_number_pad_zero::<6, _, _>(output, nanos / 1_000)
} else if (nanos / 10_000) % 10 != 0 {
format_number_pad_zero::<_, _, 5>(output, nanos / 10_000)
format_number_pad_zero::<5, _, _>(output, nanos / 10_000)
} else if (nanos / 100_000) % 10 != 0 {
format_number_pad_zero::<_, _, 4>(output, nanos / 100_000)
format_number_pad_zero::<4, _, _>(output, nanos / 100_000)
} else if (nanos / 1_000_000) % 10 != 0 {
format_number_pad_zero::<_, _, 3>(output, nanos / 1_000_000)
format_number_pad_zero::<3, _, _>(output, nanos / 1_000_000)
} else if (nanos / 10_000_000) % 10 != 0 {
format_number_pad_zero::<_, _, 2>(output, nanos / 10_000_000)
format_number_pad_zero::<2, _, _>(output, nanos / 10_000_000)
} else {
format_number_pad_zero::<_, _, 1>(output, nanos / 100_000_000)
format_number_pad_zero::<1, _, _>(output, nanos / 100_000_000)
}?;
}

Expand All @@ -229,10 +229,10 @@ impl sealed::Sealed for Rfc3339 {
&[b'+']
},
)?;
bytes += format_number_pad_zero::<_, _, 2>(output, offset.whole_hours().unsigned_abs())?;
bytes += format_number_pad_zero::<2, _, _>(output, offset.whole_hours().unsigned_abs())?;
bytes += write(output, &[b':'])?;
bytes +=
format_number_pad_zero::<_, _, 2>(output, offset.minutes_past_hour().unsigned_abs())?;
format_number_pad_zero::<2, _, _>(output, offset.minutes_past_hour().unsigned_abs())?;

Ok(bytes)
}
Expand Down
32 changes: 16 additions & 16 deletions src/formatting/iso8601.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,44 +21,44 @@ pub(super) fn format_date<W: io::Write, const CONFIG: EncodedConfig>(
let (year, month, day) = date.to_calendar_date();
if Iso8601::<CONFIG>::YEAR_IS_SIX_DIGITS {
bytes += write_if_else(output, year < 0, b"-", b"+")?;
bytes += format_number_pad_zero::<_, _, 6>(output, year.unsigned_abs())?;
bytes += format_number_pad_zero::<6, _, _>(output, year.unsigned_abs())?;
} else if !(0..=9999).contains(&year) {
return Err(error::Format::InvalidComponent("year"));
} else {
bytes += format_number_pad_zero::<_, _, 4>(output, year as u32)?;
bytes += format_number_pad_zero::<4, _, _>(output, year as u32)?;
}
bytes += write_if(output, Iso8601::<CONFIG>::USE_SEPARATORS, b"-")?;
bytes += format_number_pad_zero::<_, _, 2>(output, month as u8)?;
bytes += format_number_pad_zero::<2, _, _>(output, month as u8)?;
bytes += write_if(output, Iso8601::<CONFIG>::USE_SEPARATORS, b"-")?;
bytes += format_number_pad_zero::<_, _, 2>(output, day)?;
bytes += format_number_pad_zero::<2, _, _>(output, day)?;
}
DateKind::Week => {
let (year, week, day) = date.to_iso_week_date();
if Iso8601::<CONFIG>::YEAR_IS_SIX_DIGITS {
bytes += write_if_else(output, year < 0, b"-", b"+")?;
bytes += format_number_pad_zero::<_, _, 6>(output, year.unsigned_abs())?;
bytes += format_number_pad_zero::<6, _, _>(output, year.unsigned_abs())?;
} else if !(0..=9999).contains(&year) {
return Err(error::Format::InvalidComponent("year"));
} else {
bytes += format_number_pad_zero::<_, _, 4>(output, year as u32)?;
bytes += format_number_pad_zero::<4, _, _>(output, year as u32)?;
}
bytes += write_if_else(output, Iso8601::<CONFIG>::USE_SEPARATORS, b"-W", b"W")?;
bytes += format_number_pad_zero::<_, _, 2>(output, week)?;
bytes += format_number_pad_zero::<2, _, _>(output, week)?;
bytes += write_if(output, Iso8601::<CONFIG>::USE_SEPARATORS, b"-")?;
bytes += format_number_pad_zero::<_, _, 1>(output, day.number_from_monday())?;
bytes += format_number_pad_zero::<1, _, _>(output, day.number_from_monday())?;
}
DateKind::Ordinal => {
let (year, day) = date.to_ordinal_date();
if Iso8601::<CONFIG>::YEAR_IS_SIX_DIGITS {
bytes += write_if_else(output, year < 0, b"-", b"+")?;
bytes += format_number_pad_zero::<_, _, 6>(output, year.unsigned_abs())?;
bytes += format_number_pad_zero::<6, _, _>(output, year.unsigned_abs())?;
} else if !(0..=9999).contains(&year) {
return Err(error::Format::InvalidComponent("year"));
} else {
bytes += format_number_pad_zero::<_, _, 4>(output, year as u32)?;
bytes += format_number_pad_zero::<4, _, _>(output, year as u32)?;
}
bytes += write_if(output, Iso8601::<CONFIG>::USE_SEPARATORS, b"-")?;
bytes += format_number_pad_zero::<_, _, 3>(output, day)?;
bytes += format_number_pad_zero::<3, _, _>(output, day)?;
}
}

Expand Down Expand Up @@ -90,17 +90,17 @@ pub(super) fn format_time<W: io::Write, const CONFIG: EncodedConfig>(
format_float(output, hours, 2, decimal_digits)?;
}
TimePrecision::Minute { decimal_digits } => {
bytes += format_number_pad_zero::<_, _, 2>(output, hours)?;
bytes += format_number_pad_zero::<2, _, _>(output, hours)?;
bytes += write_if(output, Iso8601::<CONFIG>::USE_SEPARATORS, b":")?;
let minutes = (minutes as f64)
+ (seconds as f64) / 60.
+ (nanoseconds as f64) / 60. / 1_000_000_000.;
bytes += format_float(output, minutes, 2, decimal_digits)?;
}
TimePrecision::Second { decimal_digits } => {
bytes += format_number_pad_zero::<_, _, 2>(output, hours)?;
bytes += format_number_pad_zero::<2, _, _>(output, hours)?;
bytes += write_if(output, Iso8601::<CONFIG>::USE_SEPARATORS, b":")?;
bytes += format_number_pad_zero::<_, _, 2>(output, minutes)?;
bytes += format_number_pad_zero::<2, _, _>(output, minutes)?;
bytes += write_if(output, Iso8601::<CONFIG>::USE_SEPARATORS, b":")?;
let seconds = (seconds as f64) + (nanoseconds as f64) / 1_000_000_000.;
bytes += format_float(output, seconds, 2, decimal_digits)?;
Expand All @@ -126,13 +126,13 @@ pub(super) fn format_offset<W: io::Write, const CONFIG: EncodedConfig>(
return Err(error::Format::InvalidComponent("offset_second"));
}
bytes += write_if_else(output, offset.is_negative(), b"-", b"+")?;
bytes += format_number_pad_zero::<_, _, 2>(output, hours.unsigned_abs())?;
bytes += format_number_pad_zero::<2, _, _>(output, hours.unsigned_abs())?;

if Iso8601::<CONFIG>::OFFSET_PRECISION == OffsetPrecision::Hour && minutes != 0 {
return Err(error::Format::InvalidComponent("offset_minute"));
} else if Iso8601::<CONFIG>::OFFSET_PRECISION == OffsetPrecision::Minute {
bytes += write_if(output, Iso8601::<CONFIG>::USE_SEPARATORS, b":")?;
bytes += format_number_pad_zero::<_, _, 2>(output, minutes.unsigned_abs())?;
bytes += format_number_pad_zero::<2, _, _>(output, minutes.unsigned_abs())?;
}

Ok(bytes)
Expand Down
Loading

0 comments on commit c0ae430

Please sign in to comment.