Skip to content

Commit

Permalink
Remove core_only, cfg-out the format::Item::Owned* variants
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
quodlibetor committed Nov 22, 2019
1 parent 918cff1 commit 64a28d6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 25 deletions.
27 changes: 3 additions & 24 deletions src/format/mod.rs
Expand Up @@ -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<T: ?Sized>(core::marker::PhantomData<T>);

impl Box<str> {
/// Core only
pub fn len(&self) -> usize { 0 }
}

impl Clone for Box<str> { fn clone(&self) -> Self { Box(core::marker::PhantomData) } }

impl core::ops::Index<core::ops::RangeFull> for Box<str> {
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};
Expand Down Expand Up @@ -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<str>),
/// 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<str>),
/// Numeric item. Can be optionally padded to the maximal length (if any) when formatting;
/// the parser simply ignores any padded whitespace and zeroes.
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 7 additions & 1 deletion src/format/parse.rs
Expand Up @@ -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();
}

Expand Down

0 comments on commit 64a28d6

Please sign in to comment.