From 92e5477f2cdd98777156f68a9f1958aca0ee7b60 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sat, 1 Jul 2023 15:48:54 +0200 Subject: [PATCH] Don't require `alloc` feature for `Formatter` --- src/format/formatting.rs | 26 ++++++++++++-------------- src/format/mod.rs | 3 ++- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/format/formatting.rs b/src/format/formatting.rs index 34a29c6dd7..4bcd64c2d4 100644 --- a/src/format/formatting.rs +++ b/src/format/formatting.rs @@ -8,19 +8,14 @@ extern crate alloc; #[cfg(feature = "alloc")] use alloc::string::{String, ToString}; -#[cfg(any(feature = "alloc", feature = "std"))] use core::borrow::Borrow; use core::fmt::{self, Display, Write}; -#[cfg(any(feature = "alloc", feature = "std"))] use crate::naive::{NaiveDate, NaiveDateTime, NaiveTime}; -#[cfg(any(feature = "alloc", feature = "std"))] use crate::offset::{FixedOffset, Offset}; -#[cfg(any(feature = "alloc", feature = "std"))] use crate::{Datelike, Timelike, Weekday}; use super::locales; -#[cfg(any(feature = "alloc", feature = "std"))] use super::{ Colons, Fixed, InternalFixed, InternalInternal, Item, Locale, Numeric, OffsetFormat, OffsetPrecision, Pad, @@ -29,7 +24,6 @@ use locales::*; /// A *temporary* object which can be used as an argument to `format!` or others. /// This is normally constructed via `format` methods of each date and time type. -#[cfg(any(feature = "alloc", feature = "std"))] #[derive(Debug)] pub struct Formatter { /// The date view, if any. @@ -45,7 +39,6 @@ pub struct Formatter { locale: Locale, } -#[cfg(any(feature = "alloc", feature = "std"))] impl<'a, I, B, Off> Formatter where I: Iterator + Clone, @@ -82,6 +75,7 @@ where for item in self.items.clone() { match *item.borrow() { Item::Literal(s) | Item::Space(s) => w.write_str(s), + #[cfg(any(feature = "alloc", feature = "std"))] Item::OwnedLiteral(ref s) | Item::OwnedSpace(ref s) => w.write_str(s), Item::Numeric(ref spec, ref pad) => self.format_numeric(w, spec, pad), Item::Fixed(ref spec) => self.format_fixed(w, spec), @@ -245,7 +239,6 @@ where } } -#[cfg(any(feature = "alloc", feature = "std"))] impl<'a, I, B, Off> Display for Formatter where I: Iterator + Clone, @@ -253,9 +246,17 @@ where Off: Offset + Display, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - let mut result = String::new(); - self.format(&mut result)?; - f.pad(&result) + #[cfg(any(feature = "alloc", feature = "std", test))] + if f.width().is_some() { + // Justify/pad/truncate the formatted result by rendering it to a temporary `String` + // first. + // We skip this step if there are no 'external' formatting specifiers. + // This is the only formatting functionality that is missing without `alloc`. + let mut result = String::new(); + self.format(&mut result)?; + return f.pad(&result); + } + self.format(f) } } @@ -425,7 +426,6 @@ pub fn format_item_localized( .fmt(w) } -#[cfg(any(feature = "alloc", feature = "std"))] impl OffsetFormat { /// Writes an offset from UTC with the format defined by `self`. fn format(&self, w: &mut impl Write, off: FixedOffset) -> fmt::Result { @@ -506,7 +506,6 @@ impl OffsetFormat { } /// Writes the date, time and offset to the string. same as `%Y-%m-%dT%H:%M:%S%.f%:z` -#[cfg(any(feature = "alloc", feature = "std"))] pub(crate) fn write_rfc3339( w: &mut impl Write, dt: NaiveDateTime, @@ -534,7 +533,6 @@ pub(crate) fn write_rfc2822( write_rfc2822_inner(w, dt.date(), dt.time(), off, default_locale()) } -#[cfg(any(feature = "alloc", feature = "std"))] /// write datetimes like `Tue, 1 Jul 2003 10:52:37 +0200`, same as `%a, %d %b %Y %H:%M:%S %z` fn write_rfc2822_inner( w: &mut impl Write, diff --git a/src/format/mod.rs b/src/format/mod.rs index eada4e512b..ec46f24bbd 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -54,9 +54,10 @@ pub mod strftime; pub(crate) mod locales; pub(crate) use formatting::write_hundreds; +pub use formatting::Formatter; #[allow(deprecated)] #[cfg(any(feature = "alloc", feature = "std"))] -pub use formatting::{format, format_item, DelayedFormat, Formatter}; +pub use formatting::{format, format_item, DelayedFormat}; #[allow(deprecated)] #[cfg(feature = "unstable-locales")] pub use formatting::{format_item_localized, format_localized};