Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Support for no_std #341

Merged
merged 15 commits into from
Nov 22, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
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.
  • Loading branch information
quodlibetor committed Nov 22, 2019
commit 64a28d6812ca221261be7ae6e341340b91b2d76c
27 changes: 3 additions & 24 deletions src/format/mod.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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