Skip to content

Commit

Permalink
#![no_std], embedded support (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
PTaylor-us authored Apr 18, 2020
1 parent ea187f3 commit a933f2a
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 15 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,49 @@ jobs:
command: check
args: --features serde,deprecated,panicking-api,rand

check-embedded:
name: Type checking (embedded)
runs-on: ubuntu-latest
strategy:
matrix:
rust: [1.34.0, 1.36.0, stable]

steps:
- name: Checkout sources
uses: actions/checkout@v2

- name: Cache target
uses: actions/cache@v1
env:
cache-name: target
with:
path: ./target
key: ubuntu-latest-typecheck-target-${{ matrix.rust }}-thumbv7em-none-eabihf

- name: Install toolchain
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
target: thumbv7em-none-eabihf
override: true

# ensure `#![no_std]` support
- name: Run `cargo check --no-default-features`
uses: actions-rs/cargo@v1
with:
command: check
args: --no-default-features --target thumbv7em-none-eabihf
if: matrix.rust != '1.34.0' # alloc is unstable in 1.34

# `#![no_std]` with serde, rand
- name: Run `cargo check --no-default-features --features serde,rand`
uses: actions-rs/cargo@v1
with:
command: check
args: --no-default-features --features serde,rand --target thumbv7em-none-eabihf
if: matrix.rust != '1.34.0' # alloc is unstable in 1.34

test:
name: Test suite
runs-on: ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ cfg-if = "0.1.10"
rand = { version = "0.7", optional = true, default-features = false }
rustversion = "1"
serde = { version = "1", optional = true, default-features = false, features = ["derive"] }
standback = { version = "0.2", default-features = false }
standback = { version = "0.2.3", default-features = false }
time-macros = { version = "0.1", path = "time-macros" }

[workspace]
Expand Down
7 changes: 0 additions & 7 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@ fn features(features: &[(&str, &str)]) {
}
}

fn no_std() {
if env::var("CARGO_FEATURE_STD").is_err() {
println!("cargo:rustc-cfg=no_std");
}
}

#[rustversion::since(1.40.0)]
fn non_exhaustive() {
println!("cargo:rustc-cfg=supports_non_exhaustive");
Expand All @@ -38,6 +32,5 @@ fn main() {
("SERDE", "serde"),
("__DOC", "docs"),
]);
no_std();
non_exhaustive();
}
2 changes: 1 addition & 1 deletion src/format/format.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The `Format` struct and its implementations.
#[cfg(no_std)]
#[cfg(not(std))]
use crate::internal_prelude::*;

/// Various well-known formats, along with the possibility for a custom format
Expand Down
2 changes: 1 addition & 1 deletion src/format/parse_items.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Parse formats used in the `format` and `parse` methods.
use crate::format::{FormatItem, Padding, Specifier};
#[cfg(no_std)]
#[cfg(not(std))]
use crate::internal_prelude::*;

/// Parse the formatting string. Panics if not valid.
Expand Down
10 changes: 5 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@
//! | `0` | Pad with zeros | `%0d` => `05` |
#![cfg_attr(docs, feature(doc_cfg))]
#![cfg_attr(no_std, no_std)]
#![cfg_attr(not(std), no_std)]
#![deny(
unsafe_code, // Used when interacting with system APIs
anonymous_parameters,
Expand Down Expand Up @@ -203,7 +203,7 @@ compile_error!("The `__doc` feature requires a nightly compiler, and is for inte
#[rustversion::before(1.34.0)]
compile_error!("The time crate has a minimum supported rust version of 1.34.0.");

#[cfg(no_std)]
#[cfg(not(std))]
#[rustversion::before(1.36.0)]
compile_error!(
"Using the time crate without the standard library enabled requires a global allocator. This \
Expand All @@ -218,7 +218,7 @@ macro_rules! format_conditional {
};

($first_conditional:ident, $($conditional:ident),*) => {{
#[cfg(no_std)]
#[cfg(not(std))]
let mut s = alloc::string::String::new();
#[cfg(std)]
let mut s = String::new();
Expand Down Expand Up @@ -457,7 +457,7 @@ pub mod prelude {
mod internal_prelude {
#![allow(unused_imports)]

#[cfg(no_std)]
#[cfg(not(std))]
extern crate alloc;

#[cfg(std)]
Expand All @@ -469,7 +469,7 @@ mod internal_prelude {
PrimitiveDateTime, Time, UtcOffset,
Weekday::{self, Friday, Monday, Saturday, Sunday, Thursday, Tuesday, Wednesday},
};
#[cfg(no_std)]
#[cfg(not(std))]
pub(crate) use alloc::{
borrow::ToOwned,
boxed::Box,
Expand Down
2 changes: 2 additions & 0 deletions time-macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![no_std]

use proc_macro_hack::proc_macro_hack;

#[proc_macro_hack]
Expand Down

0 comments on commit a933f2a

Please sign in to comment.