From 64a28d6812ca221261be7ae6e341340b91b2d76c Mon Sep 17 00:00:00 2001 From: Brandon W Maister Date: Fri, 22 Nov 2019 15:27:10 -0500 Subject: [PATCH] Remove core_only, cfg-out the `format::Item::Owned*` variants This means that a few more features of formatting items don't compile in non-alloc environments, but they wouldn't have worked correctly anyway. --- src/format/mod.rs | 27 +++------------------------ src/format/parse.rs | 8 +++++++- 2 files changed, 10 insertions(+), 25 deletions(-) diff --git a/src/format/mod.rs b/src/format/mod.rs index e003c30..b8e6458 100644 --- a/src/format/mod.rs +++ b/src/format/mod.rs @@ -26,30 +26,6 @@ use alloc::boxed::Box; #[cfg(feature = "alloc")] use alloc::string::{String, ToString}; -#[cfg(not(any(feature = "alloc", feature = "std", test)))] -mod core_only { - /// Core only - #[derive(Debug, PartialEq, Eq, PartialOrd, Ord)] - pub struct Box(core::marker::PhantomData); - - impl Box { - /// Core only - pub fn len(&self) -> usize { 0 } - } - - impl Clone for Box { fn clone(&self) -> Self { Box(core::marker::PhantomData) } } - - impl core::ops::Index for Box { - type Output = str; - fn index(&self, _: core::ops::RangeFull) -> &Self::Output { - "" - } - } -} - -#[cfg(not(any(feature = "alloc", feature = "std", test)))] -use self::core_only::Box; - #[cfg(any(feature = "alloc", feature = "std", test))] use {Datelike, Timelike}; use {Weekday, ParseWeekdayError}; @@ -279,10 +255,12 @@ pub enum Item<'a> { /// A literally printed and parsed text. Literal(&'a str), /// Same to `Literal` but with the string owned by the item. + #[cfg(any(feature = "alloc", feature = "std", test))] OwnedLiteral(Box), /// Whitespace. Prints literally but reads zero or more whitespace. Space(&'a str), /// Same to `Space` but with the string owned by the item. + #[cfg(any(feature = "alloc", feature = "std", test))] 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. @@ -404,6 +382,7 @@ pub fn format<'a, I>( for item in items { match item { Item::Literal(s) | Item::Space(s) => result.push_str(s), + #[cfg(any(feature = "alloc", feature = "std", test))] Item::OwnedLiteral(ref s) | Item::OwnedSpace(ref s) => result.push_str(s), Item::Numeric(spec, pad) => { diff --git a/src/format/parse.rs b/src/format/parse.rs index c10a802..e8e0d25 100644 --- a/src/format/parse.rs +++ b/src/format/parse.rs @@ -218,13 +218,19 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<( s = &s[prefix.len()..]; } + #[cfg(any(feature = "alloc", feature = "std", test))] Item::OwnedLiteral(ref prefix) => { if s.len() < prefix.len() { return Err(TOO_SHORT); } if !s.starts_with(&prefix[..]) { return Err(INVALID); } s = &s[prefix.len()..]; } - Item::Space(_) | Item::OwnedSpace(_) => { + Item::Space(_) => { + s = s.trim_left(); + } + + #[cfg(any(feature = "alloc", feature = "std", test))] + Item::OwnedSpace(_) => { s = s.trim_left(); }