Skip to content

Commit

Permalink
Use explicit_generic_args_with_impl_trait
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Feb 22, 2023
1 parent 3b6d77f commit 0e82474
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 97 deletions.
56 changes: 27 additions & 29 deletions time/src/formatting/formattable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,22 +173,21 @@ 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.minutes_past_hour().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())?;

Ok(bytes)
}
Expand Down Expand Up @@ -217,40 +216,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 @@ -260,10 +259,9 @@ impl sealed::Sealed for Rfc3339 {
}

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 += write(output, b":")?;
bytes +=
format_number_pad_zero::<2, _, _>(output, offset.minutes_past_hour().unsigned_abs())?;
bytes += format_number_pad_zero::<2>(output, offset.minutes_past_hour().unsigned_abs())?;

Ok(bytes)
}
Expand All @@ -281,15 +279,15 @@ impl<const CONFIG: EncodedConfig> sealed::Sealed for Iso8601<CONFIG> {

if Self::FORMAT_DATE {
let date = date.ok_or(error::Format::InsufficientTypeInformation)?;
bytes += iso8601::format_date::<_, CONFIG>(output, date)?;
bytes += iso8601::format_date::<CONFIG>(output, date)?;
}
if Self::FORMAT_TIME {
let time = time.ok_or(error::Format::InsufficientTypeInformation)?;
bytes += iso8601::format_time::<_, CONFIG>(output, time)?;
bytes += iso8601::format_time::<CONFIG>(output, time)?;
}
if Self::FORMAT_OFFSET {
let offset = offset.ok_or(error::Format::InsufficientTypeInformation)?;
bytes += iso8601::format_offset::<_, CONFIG>(output, offset)?;
bytes += iso8601::format_offset::<CONFIG>(output, offset)?;
}

if bytes == 0 {
Expand Down
44 changes: 22 additions & 22 deletions time/src/formatting/iso8601.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::formatting::{format_float, format_number_pad_zero, write, write_if, w
use crate::{error, Date, Time, UtcOffset};

/// Format the date portion of ISO 8601.
pub(super) fn format_date<W: io::Write, const CONFIG: EncodedConfig>(
output: &mut W,
pub(super) fn format_date<const CONFIG: EncodedConfig>(
output: &mut impl io::Write,
date: Date,
) -> Result<usize, error::Format> {
let mut bytes = 0;
Expand All @@ -21,53 +21,53 @@ 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)?;
}
}

Ok(bytes)
}

/// Format the time portion of ISO 8601.
pub(super) fn format_time<W: io::Write, const CONFIG: EncodedConfig>(
output: &mut W,
pub(super) fn format_time<const CONFIG: EncodedConfig>(
output: &mut impl io::Write,
time: Time,
) -> Result<usize, error::Format> {
let mut bytes = 0;
Expand All @@ -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 @@ -111,8 +111,8 @@ pub(super) fn format_time<W: io::Write, const CONFIG: EncodedConfig>(
}

/// Format the UTC offset portion of ISO 8601.
pub(super) fn format_offset<W: io::Write, const CONFIG: EncodedConfig>(
output: &mut W,
pub(super) fn format_offset<const CONFIG: EncodedConfig>(
output: &mut impl io::Write,
offset: UtcOffset,
) -> Result<usize, error::Format> {
if Iso8601::<CONFIG>::FORMAT_TIME && offset.is_utc() {
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 0e82474

Please sign in to comment.