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.
This commit is contained in:
Brandon W Maister 2019-11-22 15:27:10 -05:00
parent 918cff1f72
commit 64a28d6812
2 changed files with 10 additions and 25 deletions

View File

@ -26,30 +26,6 @@ use alloc::boxed::Box;
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
use alloc::string::{String, ToString}; 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))] #[cfg(any(feature = "alloc", feature = "std", test))]
use {Datelike, Timelike}; use {Datelike, Timelike};
use {Weekday, ParseWeekdayError}; use {Weekday, ParseWeekdayError};
@ -279,10 +255,12 @@ pub enum Item<'a> {
/// A literally printed and parsed text. /// A literally printed and parsed text.
Literal(&'a str), Literal(&'a str),
/// Same to `Literal` but with the string owned by the item. /// Same to `Literal` but with the string owned by the item.
#[cfg(any(feature = "alloc", feature = "std", test))]
OwnedLiteral(Box<str>), OwnedLiteral(Box<str>),
/// Whitespace. Prints literally but reads zero or more whitespace. /// Whitespace. Prints literally but reads zero or more whitespace.
Space(&'a str), Space(&'a str),
/// Same to `Space` but with the string owned by the item. /// Same to `Space` but with the string owned by the item.
#[cfg(any(feature = "alloc", feature = "std", test))]
OwnedSpace(Box<str>), OwnedSpace(Box<str>),
/// Numeric item. Can be optionally padded to the maximal length (if any) when formatting; /// Numeric item. Can be optionally padded to the maximal length (if any) when formatting;
/// the parser simply ignores any padded whitespace and zeroes. /// the parser simply ignores any padded whitespace and zeroes.
@ -404,6 +382,7 @@ pub fn format<'a, I>(
for item in items { for item in items {
match item { match item {
Item::Literal(s) | Item::Space(s) => result.push_str(s), 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::OwnedLiteral(ref s) | Item::OwnedSpace(ref s) => result.push_str(s),
Item::Numeric(spec, pad) => { Item::Numeric(spec, pad) => {

View File

@ -218,13 +218,19 @@ pub fn parse<'a, I>(parsed: &mut Parsed, mut s: &str, items: I) -> ParseResult<(
s = &s[prefix.len()..]; s = &s[prefix.len()..];
} }
#[cfg(any(feature = "alloc", feature = "std", test))]
Item::OwnedLiteral(ref prefix) => { Item::OwnedLiteral(ref prefix) => {
if s.len() < prefix.len() { return Err(TOO_SHORT); } if s.len() < prefix.len() { return Err(TOO_SHORT); }
if !s.starts_with(&prefix[..]) { return Err(INVALID); } if !s.starts_with(&prefix[..]) { return Err(INVALID); }
s = &s[prefix.len()..]; 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(); s = s.trim_left();
} }