Skip to content

Commit

Permalink
Don't require alloc feature for Formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jul 1, 2023
1 parent 2f4bbf3 commit 92e5477
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
26 changes: 12 additions & 14 deletions src/format/formatting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<I, Off> {
/// The date view, if any.
Expand All @@ -45,7 +39,6 @@ pub struct Formatter<I, Off> {
locale: Locale,
}

#[cfg(any(feature = "alloc", feature = "std"))]
impl<'a, I, B, Off> Formatter<I, Off>
where
I: Iterator<Item = B> + Clone,
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -245,17 +239,24 @@ where
}
}

#[cfg(any(feature = "alloc", feature = "std"))]
impl<'a, I, B, Off> Display for Formatter<I, Off>
where
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
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)
}
}

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/format/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 92e5477

Please sign in to comment.