Skip to content

Commit f7491b4

Browse files
authored
Theoretically become no_std compatible (#2)
This commit adds no_std compliance and some trybuild tests to verify the no_std compliance and a ci configuration for github actions to verify said compliance.
1 parent 1380203 commit f7491b4

File tree

8 files changed

+169
-19
lines changed

8 files changed

+169
-19
lines changed

.github/workflows/ci.yml

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
on: [push]
2+
3+
name: Continuous integration
4+
5+
jobs:
6+
check:
7+
name: Check
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
rust:
12+
- stable
13+
- 1.31.0
14+
steps:
15+
- uses: actions/checkout@v1
16+
- uses: actions-rs/toolchain@v1
17+
with:
18+
toolchain: ${{ matrix.rust }}
19+
override: true
20+
- uses: actions-rs/cargo@v1
21+
with:
22+
command: check
23+
24+
test:
25+
name: Test Suite
26+
runs-on: ubuntu-latest
27+
strategy:
28+
matrix:
29+
rust:
30+
- stable
31+
- nightly
32+
- 1.31.0
33+
steps:
34+
- uses: actions/checkout@v1
35+
- uses: actions-rs/toolchain@v1
36+
with:
37+
toolchain: ${{ matrix.rust }}
38+
override: true
39+
- uses: actions-rs/cargo@v1
40+
with:
41+
command: test
42+
43+
fmt:
44+
name: Rustfmt
45+
runs-on: ubuntu-latest
46+
strategy:
47+
matrix:
48+
rust:
49+
- stable
50+
- 1.31.0
51+
steps:
52+
- uses: actions/checkout@v1
53+
- uses: actions-rs/toolchain@v1
54+
with:
55+
toolchain: ${{ matrix.rust }}
56+
override: true
57+
- run: rustup component add rustfmt
58+
- uses: actions-rs/cargo@v1
59+
with:
60+
command: fmt
61+
args: --all -- --check
62+
63+
clippy:
64+
name: Clippy
65+
runs-on: ubuntu-latest
66+
strategy:
67+
matrix:
68+
rust:
69+
- stable
70+
- 1.31.0
71+
steps:
72+
- uses: actions/checkout@v1
73+
- uses: actions-rs/toolchain@v1
74+
with:
75+
toolchain: ${{ matrix.rust }}
76+
override: true
77+
- run: rustup component add clippy
78+
- uses: actions-rs/cargo@v1
79+
with:
80+
command: clippy
81+
args: -- -D warnings

.github/workflows/rust.yml

Lines changed: 0 additions & 15 deletions
This file was deleted.

Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,9 @@ proc-macro = true
2222
syn = "1.0"
2323
quote = "1.0"
2424
proc-macro2 = "1.0"
25+
26+
[dev-dependencies]
27+
trybuild = "1.0"
28+
static_assertions = "0.3.4"
29+
libc = { version = "0.2", default-features = false }
30+
rustversion = "1.0.0"

src/expand.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ fn impl_struct(input: &DeriveInput, data: &DataStruct) -> Result<TokenStream> {
2828
Fields::Unit => quote!(_),
2929
};
3030
quote! {
31-
impl #impl_generics std::fmt::Display for #ty #ty_generics #where_clause {
32-
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
31+
impl #impl_generics core::fmt::Display for #ty #ty_generics #where_clause {
32+
fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
3333
#[allow(unused_variables)]
3434
let #pat = self;
3535
#display
@@ -76,8 +76,8 @@ fn impl_enum(input: &DeriveInput, data: &DataEnum) -> Result<TokenStream> {
7676
})
7777
.collect::<Result<Vec<_>>>()?;
7878
Some(quote! {
79-
impl #impl_generics std::fmt::Display for #ty #ty_generics #where_clause {
80-
fn fmt(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
79+
impl #impl_generics core::fmt::Display for #ty #ty_generics #where_clause {
80+
fn fmt(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
8181
#[allow(unused_variables)]
8282
match self {
8383
#(#arms,)*

tests/compile_tests.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#[rustversion::attr(not(nightly), ignore)]
2+
#[test]
3+
fn no_std() {
4+
let t = trybuild::TestCases::new();
5+
t.compile_fail("tests/no_std/without.rs");
6+
t.pass("tests/no_std/with.rs");
7+
}

tests/no_std/with.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![feature(lang_items, start)]
2+
#![no_std]
3+
4+
#[start]
5+
fn start(_argc: isize, _argv: *const *const u8) -> isize {
6+
0
7+
}
8+
9+
#[lang = "eh_personality"]
10+
#[no_mangle]
11+
pub extern "C" fn rust_eh_personality() {}
12+
13+
#[panic_handler]
14+
fn panic(_info: &core::panic::PanicInfo) -> ! {
15+
unsafe {
16+
libc::abort();
17+
}
18+
}
19+
20+
use displaydoc::Display;
21+
22+
/// this type is pretty swell
23+
#[derive(Display)]
24+
struct FakeType;
25+
26+
static_assertions::assert_impl_all!(label; FakeType, core::fmt::Display);

tests/no_std/without.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#![feature(lang_items, start)]
2+
#![no_std]
3+
4+
#[start]
5+
fn start(_argc: isize, _argv: *const *const u8) -> isize {
6+
0
7+
}
8+
9+
#[lang = "eh_personality"]
10+
#[no_mangle]
11+
pub extern "C" fn rust_eh_personality() {}
12+
13+
#[panic_handler]
14+
fn panic(_info: &core::panic::PanicInfo) -> ! {
15+
unsafe {
16+
libc::abort();
17+
}
18+
}
19+
20+
use displaydoc::Display;
21+
22+
/// this type is pretty swell
23+
struct FakeType;
24+
25+
static_assertions::assert_impl_all!(label; FakeType, core::fmt::Display);

tests/no_std/without.stderr

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: unused import: `displaydoc::Display`
2+
--> $DIR/without.rs:20:5
3+
|
4+
20 | use displaydoc::Display;
5+
| ^^^^^^^^^^^^^^^^^^^
6+
|
7+
= note: `#[warn(unused_imports)]` on by default
8+
9+
error[E0277]: `FakeType` doesn't implement `core::fmt::Display`
10+
--> $DIR/without.rs:25:44
11+
|
12+
25 | static_assertions::assert_impl_all!(label; FakeType, core::fmt::Display);
13+
| -------------------------------------------^^^^^^^^----------------------
14+
| | |
15+
| | `FakeType` cannot be formatted with the default formatter
16+
| required by this bound in `label::assert_impl_all`
17+
|
18+
= help: the trait `core::fmt::Display` is not implemented for `FakeType`
19+
= note: in format strings you may be able to use `{:?}` (or {:#?} for pretty-print) instead
20+
= note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

0 commit comments

Comments
 (0)