From 8f8ac3b86b5ed37478c3106dfe86cc8038c2c861 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sat, 23 Sep 2023 12:25:42 +0200 Subject: [PATCH 1/2] Make `std` imply `alloc` feature --- Cargo.toml | 2 +- src/date.rs | 8 ++--- src/datetime/mod.rs | 12 +++---- src/datetime/tests.rs | 16 ++++----- src/format/formatting.rs | 71 +++++++++++++++---------------------- src/format/mod.rs | 23 +++++------- src/format/parse.rs | 6 ++-- src/format/strftime.rs | 12 +++---- src/naive/date.rs | 8 ++--- src/naive/datetime/mod.rs | 8 ++--- src/naive/datetime/tests.rs | 4 +-- src/naive/isoweek.rs | 4 +-- src/naive/time/mod.rs | 8 ++--- 13 files changed, 81 insertions(+), 101 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 571b2e9f70..195cf4434d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,7 +21,7 @@ default = ["clock", "std", "oldtime", "wasmbind"] alloc = [] libc = [] winapi = ["windows-targets"] -std = [] +std = ["alloc"] clock = ["std", "winapi", "iana-time-zone", "android-tzdata"] oldtime = [] wasmbind = ["wasm-bindgen", "js-sys"] diff --git a/src/date.rs b/src/date.rs index 6cbe851fac..78d5dc9f01 100644 --- a/src/date.rs +++ b/src/date.rs @@ -4,7 +4,7 @@ //! ISO 8601 calendar date with time zone. #![allow(deprecated)] -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use core::borrow::Borrow; use core::cmp::Ordering; use core::ops::{Add, AddAssign, Sub, SubAssign}; @@ -16,7 +16,7 @@ use rkyv::{Archive, Deserialize, Serialize}; use crate::duration::Duration as OldDuration; #[cfg(feature = "unstable-locales")] use crate::format::Locale; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use crate::format::{DelayedFormat, Item, StrftimeItems}; use crate::naive::{IsoWeek, NaiveDate, NaiveTime}; use crate::offset::{TimeZone, Utc}; @@ -333,7 +333,7 @@ where Tz::Offset: fmt::Display, { /// Formats the date with the specified formatting items. - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat @@ -347,7 +347,7 @@ where /// Formats the date with the specified format string. /// See the [`crate::format::strftime`] module /// on the supported escape sequences. - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat> { diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index 12e2660297..cfd2815153 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -21,7 +21,7 @@ use crate::format::{ parse, parse_and_remainder, parse_rfc3339, Fixed, Item, ParseError, ParseResult, Parsed, StrftimeItems, TOO_LONG, }; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use crate::format::{write_rfc3339, DelayedFormat}; use crate::naive::{Days, IsoWeek, NaiveDate, NaiveDateTime, NaiveTime}; #[cfg(feature = "clock")] @@ -536,7 +536,7 @@ impl DateTime { /// /// Panics if the date can not be represented in this format: the year may not be negative and /// can not have more than 4 digits. - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[must_use] pub fn to_rfc2822(&self) -> String { let mut result = String::with_capacity(32); @@ -546,7 +546,7 @@ impl DateTime { } /// Returns an RFC 3339 and ISO 8601 date and time string such as `1996-12-19T16:39:57-08:00`. - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[must_use] pub fn to_rfc3339(&self) -> String { // For some reason a string with a capacity less than 32 is ca 20% slower when benchmarking. @@ -582,7 +582,7 @@ impl DateTime { /// assert_eq!(dt.to_rfc3339_opts(SecondsFormat::Secs, true), /// "2018-01-26T10:30:09+08:00"); /// ``` - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[must_use] pub fn to_rfc3339_opts(&self, secform: SecondsFormat, use_z: bool) -> String { let mut result = String::with_capacity(38); @@ -859,7 +859,7 @@ where Tz::Offset: fmt::Display, { /// Formats the combined date and time with the specified formatting items. - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat @@ -883,7 +883,7 @@ where /// let formatted = format!("{}", date_time.format("%d/%m/%Y %H:%M")); /// assert_eq!(formatted, "02/04/2017 12:50"); /// ``` - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat> { diff --git a/src/datetime/tests.rs b/src/datetime/tests.rs index 63dfe59b4e..3872e14b36 100644 --- a/src/datetime/tests.rs +++ b/src/datetime/tests.rs @@ -273,7 +273,7 @@ fn ymdhms_milli( // local helper function to easily create a DateTime #[allow(clippy::too_many_arguments)] -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] fn ymdhms_micro( fixedoffset: &FixedOffset, year: i32, @@ -293,7 +293,7 @@ fn ymdhms_micro( // local helper function to easily create a DateTime #[allow(clippy::too_many_arguments)] -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] fn ymdhms_nano( fixedoffset: &FixedOffset, year: i32, @@ -312,7 +312,7 @@ fn ymdhms_nano( } // local helper function to easily create a DateTime -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] fn ymdhms_utc(year: i32, month: u32, day: u32, hour: u32, min: u32, sec: u32) -> DateTime { Utc.with_ymd_and_hms(year, month, day, hour, min, sec).unwrap() } @@ -451,7 +451,7 @@ fn test_datetime_with_timezone() { } #[test] -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] fn test_datetime_rfc2822() { let edt = FixedOffset::east_opt(5 * 60 * 60).unwrap(); @@ -577,7 +577,7 @@ fn test_datetime_rfc2822() { } #[test] -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] fn test_datetime_rfc3339() { let edt5 = FixedOffset::east_opt(5 * 60 * 60).unwrap(); let edt0 = FixedOffset::east_opt(0).unwrap(); @@ -663,7 +663,7 @@ fn test_datetime_rfc3339() { } #[test] -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] fn test_rfc3339_opts() { use crate::SecondsFormat::*; let pst = FixedOffset::east_opt(8 * 60 * 60).unwrap(); @@ -694,7 +694,7 @@ fn test_rfc3339_opts() { #[test] #[should_panic] -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] fn test_rfc3339_opts_nonexhaustive() { use crate::SecondsFormat; let dt = Utc.with_ymd_and_hms(1999, 10, 9, 1, 2, 3).unwrap(); @@ -1470,7 +1470,7 @@ fn test_test_deprecated_from_offset() { } #[test] -#[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))] +#[cfg(all(feature = "unstable-locales", feature = "alloc"))] fn locale_decimal_point() { use crate::Locale::{ar_SY, nl_NL}; let dt = diff --git a/src/format/formatting.rs b/src/format/formatting.rs index 4e572d548a..805866bb4a 100644 --- a/src/format/formatting.rs +++ b/src/format/formatting.rs @@ -3,50 +3,35 @@ //! Date and time formatting routines. -#[cfg(feature = "alloc")] +#[cfg(all(not(feature = "std"), feature = "alloc"))] use alloc::string::{String, ToString}; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use core::borrow::Borrow; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use core::fmt::Display; use core::fmt::{self, Write}; -#[cfg(any( - feature = "alloc", - feature = "std", - feature = "serde", - feature = "rustc-serialize" -))] +#[cfg(any(feature = "alloc", feature = "serde", feature = "rustc-serialize"))] use crate::datetime::SecondsFormat; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use crate::offset::Offset; -#[cfg(any( - feature = "alloc", - feature = "std", - feature = "serde", - feature = "rustc-serialize" -))] +#[cfg(any(feature = "alloc", feature = "serde", feature = "rustc-serialize"))] use crate::{Datelike, FixedOffset, NaiveDateTime, Timelike}; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use crate::{NaiveDate, NaiveTime, Weekday}; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use super::locales; -#[cfg(any( - feature = "alloc", - feature = "std", - feature = "serde", - feature = "rustc-serialize" -))] +#[cfg(any(feature = "alloc", feature = "serde", feature = "rustc-serialize"))] use super::{Colons, OffsetFormat, OffsetPrecision, Pad}; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use super::{Fixed, InternalFixed, InternalInternal, Item, Locale, Numeric}; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] 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"))] +#[cfg(feature = "alloc")] #[derive(Debug)] pub struct DelayedFormat { /// The date view, if any. @@ -64,7 +49,7 @@ pub struct DelayedFormat { locale: Option, } -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] impl<'a, I: Iterator + Clone, B: Borrow>> DelayedFormat { /// Makes a new `DelayedFormat` value out of local date and time. #[must_use] @@ -131,7 +116,7 @@ impl<'a, I: Iterator + Clone, B: Borrow>> DelayedFormat { } } -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] impl<'a, I: Iterator + Clone, B: Borrow>> Display for DelayedFormat { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { #[cfg(feature = "unstable-locales")] @@ -156,7 +141,7 @@ impl<'a, I: Iterator + Clone, B: Borrow>> Display for Delayed /// Tries to format given arguments with given formatting items. /// Internally used by `DelayedFormat`. -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] #[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt instead")] pub fn format<'a, I, B>( w: &mut fmt::Formatter, @@ -181,7 +166,7 @@ where } /// Formats single formatting item -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] #[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt instead")] pub fn format_item( w: &mut fmt::Formatter, @@ -248,7 +233,7 @@ pub fn format_item_localized( .fmt(w) } -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] fn format_inner( w: &mut impl Write, date: Option<&NaiveDate>, @@ -261,7 +246,7 @@ fn format_inner( match *item { Item::Literal(s) | Item::Space(s) => w.write_str(s), - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] Item::OwnedLiteral(ref s) | Item::OwnedSpace(ref s) => w.write_str(s), Item::Numeric(ref spec, ref pad) => { @@ -484,7 +469,7 @@ fn format_inner( } } -#[cfg(any(feature = "alloc", feature = "std", feature = "serde", feature = "rustc-serialize"))] +#[cfg(any(feature = "alloc", feature = "serde", feature = "rustc-serialize"))] impl OffsetFormat { /// Writes an offset from UTC with the format defined by `self`. fn format(&self, w: &mut impl Write, off: FixedOffset) -> fmt::Result { @@ -566,7 +551,7 @@ impl OffsetFormat { /// Writes the date, time and offset to the string. same as `%Y-%m-%dT%H:%M:%S%.f%:z` #[inline] -#[cfg(any(feature = "alloc", feature = "std", feature = "serde", feature = "rustc-serialize"))] +#[cfg(any(feature = "alloc", feature = "serde", feature = "rustc-serialize"))] pub(crate) fn write_rfc3339( w: &mut impl Write, dt: NaiveDateTime, @@ -629,7 +614,7 @@ pub(crate) fn write_rfc3339( .format(w, off) } -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] /// write datetimes like `Tue, 1 Jul 2003 10:52:37 +0200`, same as `%a, %d %b %Y %H:%M:%S %z` pub(crate) fn write_rfc2822( w: &mut impl Write, @@ -639,7 +624,7 @@ pub(crate) fn write_rfc2822( write_rfc2822_inner(w, dt.date(), dt.time(), off, default_locale()) } -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] /// 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, @@ -699,15 +684,15 @@ pub(crate) fn write_hundreds(w: &mut impl Write, n: u8) -> fmt::Result { } #[cfg(test)] -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] mod tests { use super::{Colons, OffsetFormat, OffsetPrecision, Pad}; use crate::FixedOffset; - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] use crate::{NaiveDate, NaiveTime, TimeZone, Timelike, Utc}; #[test] - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] fn test_date_format() { let d = NaiveDate::from_ymd_opt(2012, 3, 4).unwrap(); assert_eq!(d.format("%Y,%C,%y,%G,%g").to_string(), "2012,20,12,2012,12"); @@ -752,7 +737,7 @@ mod tests { } #[test] - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] fn test_time_format() { let t = NaiveTime::from_hms_nano_opt(3, 5, 7, 98765432).unwrap(); assert_eq!(t.format("%H,%k,%I,%l,%P,%p").to_string(), "03, 3,03, 3,am,AM"); @@ -788,7 +773,7 @@ mod tests { } #[test] - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] fn test_datetime_format() { let dt = NaiveDate::from_ymd_opt(2010, 9, 8).unwrap().and_hms_milli_opt(7, 6, 54, 321).unwrap(); @@ -806,7 +791,7 @@ mod tests { } #[test] - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] fn test_datetime_format_alignment() { let datetime = Utc .with_ymd_and_hms(2007, 1, 2, 12, 34, 56) diff --git a/src/format/mod.rs b/src/format/mod.rs index d5256ab1e9..58e6a4d027 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -30,7 +30,7 @@ //! # Ok::<(), chrono::ParseError>(()) //! ``` -#[cfg(feature = "alloc")] +#[cfg(all(not(feature = "std"), feature = "alloc"))] use alloc::boxed::Box; use core::fmt; use core::str::FromStr; @@ -48,28 +48,23 @@ pub(crate) mod scan; pub mod strftime; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] pub(crate) mod locales; pub(crate) use formatting::write_hundreds; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] pub(crate) use formatting::write_rfc2822; -#[cfg(any( - feature = "alloc", - feature = "std", - feature = "serde", - feature = "rustc-serialize" -))] +#[cfg(any(feature = "alloc", feature = "serde", feature = "rustc-serialize"))] pub(crate) use formatting::write_rfc3339; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] #[allow(deprecated)] pub use formatting::{format, format_item, DelayedFormat}; #[cfg(feature = "unstable-locales")] #[allow(deprecated)] pub use formatting::{format_item_localized, format_localized}; -#[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))] +#[cfg(all(feature = "unstable-locales", feature = "alloc"))] pub use locales::Locale; -#[cfg(all(not(feature = "unstable-locales"), any(feature = "alloc", feature = "std")))] +#[cfg(all(not(feature = "unstable-locales"), feature = "alloc"))] pub(crate) use locales::Locale; pub(crate) use parse::parse_rfc3339; pub use parse::{parse, parse_and_remainder}; @@ -334,12 +329,12 @@ pub enum Item<'a> { /// A literally printed and parsed text. Literal(&'a str), /// Same as `Literal` but with the string owned by the item. - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] OwnedLiteral(Box), /// Whitespace. Prints literally but reads zero or more whitespace. Space(&'a str), /// Same as `Space` but with the string owned by the item. - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] OwnedSpace(Box), /// Numeric item. Can be optionally padded to the maximal length (if any) when formatting; /// the parser simply ignores any padded whitespace and zeroes. diff --git a/src/format/parse.rs b/src/format/parse.rs index 418fa424eb..04628ac861 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -311,7 +311,7 @@ where s = &s[prefix.len()..]; } - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] Item::OwnedLiteral(ref prefix) => { if s.len() < prefix.len() { return Err((s, TOO_SHORT)); @@ -326,7 +326,7 @@ where s = s.trim_start(); } - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] Item::OwnedSpace(_) => { s = s.trim_start(); } @@ -1700,7 +1700,7 @@ mod tests { let dt = Utc.with_ymd_and_hms(1994, 11, 6, 8, 49, 37).unwrap(); // Check that the format is what we expect - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] assert_eq!(dt.format(RFC850_FMT).to_string(), "Sunday, 06-Nov-94 08:49:37 GMT"); // Check that it parses correctly diff --git a/src/format/strftime.rs b/src/format/strftime.rs index a72e6353d0..557034d15a 100644 --- a/src/format/strftime.rs +++ b/src/format/strftime.rs @@ -500,7 +500,7 @@ mod tests { use crate::format::Locale; use crate::format::{fixed, internal_fixed, num, num0, nums}; use crate::format::{Fixed, InternalInternal, Numeric::*}; - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] use crate::{DateTime, FixedOffset, NaiveDate, TimeZone, Timelike, Utc}; #[test] @@ -662,7 +662,7 @@ mod tests { } #[test] - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] fn test_strftime_docs() { let dt = FixedOffset::east_opt(34200) .unwrap() @@ -773,7 +773,7 @@ mod tests { } #[test] - #[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] fn test_strftime_docs_localized() { let dt = FixedOffset::east_opt(34200) .unwrap() @@ -826,7 +826,7 @@ mod tests { /// /// See . #[test] - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] fn test_parse_only_timezone_offset_permissive_no_panic() { use crate::NaiveDate; use crate::{FixedOffset, TimeZone}; @@ -847,7 +847,7 @@ mod tests { } #[test] - #[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] fn test_strftime_localized_korean() { let dt = FixedOffset::east_opt(34200) .unwrap() @@ -876,7 +876,7 @@ mod tests { } #[test] - #[cfg(all(feature = "unstable-locales", any(feature = "alloc", feature = "std")))] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] fn test_strftime_localized_japanese() { let dt = FixedOffset::east_opt(34200) .unwrap() diff --git a/src/naive/date.rs b/src/naive/date.rs index 886dbb4f95..0990e2d5a7 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -3,7 +3,7 @@ //! ISO 8601 calendar date without timezone. -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use core::borrow::Borrow; use core::iter::FusedIterator; use core::ops::{Add, AddAssign, RangeInclusive, Sub, SubAssign}; @@ -17,7 +17,7 @@ use rkyv::{Archive, Deserialize, Serialize}; use pure_rust_locales::Locale; use crate::duration::Duration as OldDuration; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use crate::format::DelayedFormat; use crate::format::{ parse, parse_and_remainder, write_hundreds, Item, Numeric, Pad, ParseError, ParseResult, @@ -1268,7 +1268,7 @@ impl NaiveDate { /// # let d = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap(); /// assert_eq!(format!("{}", d.format_with_items(fmt)), "2015-09-05"); /// ``` - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat @@ -1311,7 +1311,7 @@ impl NaiveDate { /// assert_eq!(format!("{}", d.format("%Y-%m-%d")), "2015-09-05"); /// assert_eq!(format!("{}", d.format("%A, %-d %B, %C%y")), "Saturday, 5 September, 2015"); /// ``` - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat> { diff --git a/src/naive/datetime/mod.rs b/src/naive/datetime/mod.rs index 9aabfe583f..c5c3c0b231 100644 --- a/src/naive/datetime/mod.rs +++ b/src/naive/datetime/mod.rs @@ -3,7 +3,7 @@ //! ISO 8601 date and time without timezone. -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use core::borrow::Borrow; use core::fmt::Write; use core::ops::{Add, AddAssign, Sub, SubAssign}; @@ -14,7 +14,7 @@ use core::{fmt, str}; use rkyv::{Archive, Deserialize, Serialize}; use crate::duration::Duration as OldDuration; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use crate::format::DelayedFormat; use crate::format::{parse, parse_and_remainder, ParseError, ParseResult, Parsed, StrftimeItems}; use crate::format::{Fixed, Item, Numeric, Pad}; @@ -897,7 +897,7 @@ impl NaiveDateTime { /// # let dt = NaiveDate::from_ymd_opt(2015, 9, 5).unwrap().and_hms_opt(23, 56, 4).unwrap(); /// assert_eq!(format!("{}", dt.format_with_items(fmt)), "2015-09-05 23:56:04"); /// ``` - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat @@ -940,7 +940,7 @@ impl NaiveDateTime { /// assert_eq!(format!("{}", dt.format("%Y-%m-%d %H:%M:%S")), "2015-09-05 23:56:04"); /// assert_eq!(format!("{}", dt.format("around %l %p on %b %-d")), "around 11 PM on Sep 5"); /// ``` - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat> { diff --git a/src/naive/datetime/tests.rs b/src/naive/datetime/tests.rs index 39187de67e..9ee22dfde6 100644 --- a/src/naive/datetime/tests.rs +++ b/src/naive/datetime/tests.rs @@ -19,7 +19,7 @@ fn test_datetime_from_timestamp_millis() { for (timestamp_millis, _formatted) in valid_map.iter().copied() { let naive_datetime = NaiveDateTime::from_timestamp_millis(timestamp_millis); assert_eq!(timestamp_millis, naive_datetime.unwrap().timestamp_millis()); - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] assert_eq!(naive_datetime.unwrap().format("%F %T%.9f").to_string(), _formatted); } @@ -57,7 +57,7 @@ fn test_datetime_from_timestamp_micros() { for (timestamp_micros, _formatted) in valid_map.iter().copied() { let naive_datetime = NaiveDateTime::from_timestamp_micros(timestamp_micros); assert_eq!(timestamp_micros, naive_datetime.unwrap().timestamp_micros()); - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] assert_eq!(naive_datetime.unwrap().format("%F %T%.9f").to_string(), _formatted); } diff --git a/src/naive/isoweek.rs b/src/naive/isoweek.rs index deede94d65..606699659b 100644 --- a/src/naive/isoweek.rs +++ b/src/naive/isoweek.rs @@ -162,13 +162,13 @@ mod tests { assert_eq!(minweek.year(), internals::MIN_YEAR); assert_eq!(minweek.week(), 1); assert_eq!(minweek.week0(), 0); - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] assert_eq!(format!("{:?}", minweek), NaiveDate::MIN.format("%G-W%V").to_string()); assert_eq!(maxweek.year(), internals::MAX_YEAR + 1); assert_eq!(maxweek.week(), 1); assert_eq!(maxweek.week0(), 0); - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] assert_eq!(format!("{:?}", maxweek), NaiveDate::MAX.format("%G-W%V").to_string()); } diff --git a/src/naive/time/mod.rs b/src/naive/time/mod.rs index 3268890ec6..bf1cb2b1c5 100644 --- a/src/naive/time/mod.rs +++ b/src/naive/time/mod.rs @@ -3,7 +3,7 @@ //! ISO 8601 time without timezone. -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use core::borrow::Borrow; use core::ops::{Add, AddAssign, Sub, SubAssign}; use core::time::Duration; @@ -13,7 +13,7 @@ use core::{fmt, str}; use rkyv::{Archive, Deserialize, Serialize}; use crate::duration::Duration as OldDuration; -#[cfg(any(feature = "alloc", feature = "std"))] +#[cfg(feature = "alloc")] use crate::format::DelayedFormat; use crate::format::{ parse, parse_and_remainder, write_hundreds, Fixed, Item, Numeric, Pad, ParseError, ParseResult, @@ -781,7 +781,7 @@ impl NaiveTime { /// # let t = NaiveTime::from_hms_opt(23, 56, 4).unwrap(); /// assert_eq!(format!("{}", t.format_with_items(fmt)), "23:56:04"); /// ``` - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format_with_items<'a, I, B>(&self, items: I) -> DelayedFormat @@ -826,7 +826,7 @@ impl NaiveTime { /// assert_eq!(format!("{}", t.format("%H:%M:%S%.6f")), "23:56:04.012345"); /// assert_eq!(format!("{}", t.format("%-I:%M %p")), "11:56 PM"); /// ``` - #[cfg(any(feature = "alloc", feature = "std"))] + #[cfg(feature = "alloc")] #[inline] #[must_use] pub fn format<'a>(&self, fmt: &'a str) -> DelayedFormat> { From 94d9e65b4f68d6379886e969dec131daabc85a48 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Sat, 23 Sep 2023 12:39:48 +0200 Subject: [PATCH 2/2] Don't make `unstable-locales` depend on `alloc` feature --- Cargo.toml | 2 +- src/date.rs | 6 +++--- src/datetime/mod.rs | 6 +++--- src/format/formatting.rs | 8 +++++--- src/format/mod.rs | 10 +++++----- src/lib.rs | 8 ++++---- src/naive/date.rs | 6 +++--- 7 files changed, 24 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 195cf4434d..c5f2c8cbf3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ std = ["alloc"] clock = ["std", "winapi", "iana-time-zone", "android-tzdata"] oldtime = [] wasmbind = ["wasm-bindgen", "js-sys"] -unstable-locales = ["pure-rust-locales", "alloc"] +unstable-locales = ["pure-rust-locales"] __internal_bench = [] [dependencies] diff --git a/src/date.rs b/src/date.rs index 78d5dc9f01..e82a3f0eee 100644 --- a/src/date.rs +++ b/src/date.rs @@ -14,7 +14,7 @@ use core::{fmt, hash}; use rkyv::{Archive, Deserialize, Serialize}; use crate::duration::Duration as OldDuration; -#[cfg(feature = "unstable-locales")] +#[cfg(all(feature = "unstable-locales", feature = "alloc"))] use crate::format::Locale; #[cfg(feature = "alloc")] use crate::format::{DelayedFormat, Item, StrftimeItems}; @@ -355,7 +355,7 @@ where } /// Formats the date with the specified formatting items and locale. - #[cfg(feature = "unstable-locales")] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[inline] #[must_use] pub fn format_localized_with_items<'a, I, B>( @@ -379,7 +379,7 @@ where /// Formats the date with the specified format string and locale. /// See the [`crate::format::strftime`] module /// on the supported escape sequences. - #[cfg(feature = "unstable-locales")] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[inline] #[must_use] pub fn format_localized<'a>( diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index cfd2815153..d0e63d5732 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -15,7 +15,7 @@ use core::{fmt, hash, str}; use std::time::{SystemTime, UNIX_EPOCH}; use crate::duration::Duration as OldDuration; -#[cfg(feature = "unstable-locales")] +#[cfg(all(feature = "unstable-locales", feature = "alloc"))] use crate::format::Locale; use crate::format::{ parse, parse_and_remainder, parse_rfc3339, Fixed, Item, ParseError, ParseResult, Parsed, @@ -891,7 +891,7 @@ where } /// Formats the combined date and time with the specified formatting items and locale. - #[cfg(feature = "unstable-locales")] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[inline] #[must_use] pub fn format_localized_with_items<'a, I, B>( @@ -918,7 +918,7 @@ where /// /// See the [`crate::format::strftime`] module on the supported escape /// sequences. - #[cfg(feature = "unstable-locales")] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[inline] #[must_use] pub fn format_localized<'a>( diff --git a/src/format/formatting.rs b/src/format/formatting.rs index 805866bb4a..46e9663440 100644 --- a/src/format/formatting.rs +++ b/src/format/formatting.rs @@ -22,10 +22,12 @@ use crate::{NaiveDate, NaiveTime, Weekday}; #[cfg(feature = "alloc")] use super::locales; +#[cfg(all(feature = "unstable-locales", feature = "alloc"))] +use super::Locale; #[cfg(any(feature = "alloc", feature = "serde", feature = "rustc-serialize"))] use super::{Colons, OffsetFormat, OffsetPrecision, Pad}; #[cfg(feature = "alloc")] -use super::{Fixed, InternalFixed, InternalInternal, Item, Locale, Numeric}; +use super::{Fixed, InternalFixed, InternalInternal, Item, Numeric}; #[cfg(feature = "alloc")] use locales::*; @@ -188,7 +190,7 @@ pub fn format_item( /// Tries to format given arguments with given formatting items. /// Internally used by `DelayedFormat`. -#[cfg(feature = "unstable-locales")] +#[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt instead")] pub fn format_localized<'a, I, B>( w: &mut fmt::Formatter, @@ -213,7 +215,7 @@ where } /// Formats single formatting item -#[cfg(feature = "unstable-locales")] +#[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[deprecated(since = "0.4.32", note = "Use DelayedFormat::fmt instead")] pub fn format_item_localized( w: &mut fmt::Formatter, diff --git a/src/format/mod.rs b/src/format/mod.rs index 58e6a4d027..46723eff9c 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -48,7 +48,9 @@ pub(crate) mod scan; pub mod strftime; -#[cfg(feature = "alloc")] +#[allow(unused)] +// TODO: remove '#[allow(unused)]' once we use this module for parsing or something else that does +// not require `alloc`. pub(crate) mod locales; pub(crate) use formatting::write_hundreds; @@ -59,13 +61,11 @@ pub(crate) use formatting::write_rfc3339; #[cfg(feature = "alloc")] #[allow(deprecated)] pub use formatting::{format, format_item, DelayedFormat}; -#[cfg(feature = "unstable-locales")] +#[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[allow(deprecated)] pub use formatting::{format_item_localized, format_localized}; -#[cfg(all(feature = "unstable-locales", feature = "alloc"))] +#[cfg(feature = "unstable-locales")] pub use locales::Locale; -#[cfg(all(not(feature = "unstable-locales"), feature = "alloc"))] -pub(crate) use locales::Locale; pub(crate) use parse::parse_rfc3339; pub use parse::{parse, parse_and_remainder}; pub use parsed::Parsed; diff --git a/src/lib.rs b/src/lib.rs index c91bc06777..7841afb42c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -227,7 +227,7 @@ //! # #[allow(unused_imports)] //! use chrono::prelude::*; //! -//! # #[cfg(feature = "unstable-locales")] +//! # #[cfg(all(feature = "unstable-locales", feature = "alloc"))] //! # fn test() { //! let dt = Utc.with_ymd_and_hms(2014, 11, 28, 12, 0, 9).unwrap(); //! assert_eq!(dt.format("%Y-%m-%d %H:%M:%S").to_string(), "2014-11-28 12:00:09"); @@ -244,9 +244,9 @@ //! let dt_nano = NaiveDate::from_ymd_opt(2014, 11, 28).unwrap().and_hms_nano_opt(12, 0, 9, 1).unwrap().and_local_timezone(Utc).unwrap(); //! assert_eq!(format!("{:?}", dt_nano), "2014-11-28T12:00:09.000000001Z"); //! # } -//! # #[cfg(not(feature = "unstable-locales"))] +//! # #[cfg(not(all(feature = "unstable-locales", feature = "alloc")))] //! # fn test() {} -//! # if cfg!(feature = "unstable-locales") { +//! # if cfg!(all(feature = "unstable-locales", feature = "alloc")) { //! # test(); //! # } //! ``` @@ -489,7 +489,7 @@ pub mod prelude { #[cfg(feature = "clock")] #[doc(no_inline)] pub use crate::Local; - #[cfg(feature = "unstable-locales")] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[doc(no_inline)] pub use crate::Locale; #[doc(no_inline)] diff --git a/src/naive/date.rs b/src/naive/date.rs index 0990e2d5a7..907b1285d1 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -13,7 +13,7 @@ use core::{fmt, str}; use rkyv::{Archive, Deserialize, Serialize}; /// L10n locales. -#[cfg(feature = "unstable-locales")] +#[cfg(all(feature = "unstable-locales", feature = "alloc"))] use pure_rust_locales::Locale; use crate::duration::Duration as OldDuration; @@ -1319,7 +1319,7 @@ impl NaiveDate { } /// Formats the date with the specified formatting items and locale. - #[cfg(feature = "unstable-locales")] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[inline] #[must_use] pub fn format_localized_with_items<'a, I, B>( @@ -1338,7 +1338,7 @@ impl NaiveDate { /// /// See the [`crate::format::strftime`] module on the supported escape /// sequences. - #[cfg(feature = "unstable-locales")] + #[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[inline] #[must_use] pub fn format_localized<'a>(