Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit bac7fde

Browse files
authored
Support no_std (Rust 1.64 and later) (#14)
1 parent 8357a00 commit bac7fde

File tree

7 files changed

+50
-3
lines changed

7 files changed

+50
-3
lines changed

.github/workflows/ci.yaml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,25 @@ jobs:
1818
command: check
1919
args: --tests
2020

21+
build_no_std:
22+
name: Build on no_std
23+
runs-on: ubuntu-latest
24+
steps:
25+
- uses: actions/checkout@v2
26+
- uses: actions-rs/toolchain@v1
27+
with:
28+
profile: minimal
29+
toolchain: stable
30+
target: thumbv7em-none-eabi
31+
override: true
32+
- uses: actions-rs/cargo@v1
33+
with:
34+
command: build
35+
args: >-
36+
--verbose
37+
--target thumbv7em-none-eabi
38+
--manifest-path tests/test_no_std/Cargo.toml
39+
2140
test:
2241
name: Test Suite
2342
runs-on: ubuntu-latest

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ license = "MIT"
88
keywords = ["macro", "cstr"]
99
readme = "README.md"
1010
edition = "2018"
11+
rust-version = "1.64"
1112

1213
[lib]
1314
proc-macro = true

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ A macro for getting `&'static CStr` from literal or identifier.
1111
This macro checks whether the given literal is valid for `CStr`
1212
at compile time, and returns a static reference of `CStr`.
1313

14-
This macro can be used to to initialize constants on Rust 1.59 and above.
14+
This macro can be used to to initialize constants on Rust 1.64 and above.
1515

1616
## Example
1717

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! This macro checks whether the given literal is valid for `CStr`
44
//! at compile time, and returns a static reference of `CStr`.
55
//!
6-
//! This macro can be used to to initialize constants on Rust 1.59 and above.
6+
//! This macro can be used to to initialize constants on Rust 1.64 and above.
77
//!
88
//! ## Example
99
//!
@@ -37,7 +37,7 @@ struct Error(Span, &'static str);
3737
#[proc_macro]
3838
pub fn cstr(input: RawTokenStream) -> RawTokenStream {
3939
let tokens = match build_byte_str(input.into()) {
40-
Ok(s) => quote!(unsafe { ::std::ffi::CStr::from_bytes_with_nul_unchecked(#s) }),
40+
Ok(s) => quote!(unsafe { ::core::ffi::CStr::from_bytes_with_nul_unchecked(#s) }),
4141
Err(Error(span, msg)) => quote_spanned!(span => compile_error!(#msg)),
4242
};
4343
tokens.into()

tests/test_no_std/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/target/
2+
Cargo.lock

tests/test_no_std/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "test_no_std"
3+
version = "0.0.0"
4+
edition = "2021"
5+
license = "MIT"
6+
7+
publish = false
8+
9+
[dependencies]
10+
cstr.path = "../../"

tests/test_no_std/src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Verifies that cstr! can be used on no_std systems.
2+
//!
3+
//! To ensure that `std` is not sneaked in through a dependency (even though this crate has none at
4+
//! runtime), this should best be built on a target that has no `std` because it has no operating
5+
//! system, eg. thumbv7em-none-eabi.
6+
//!
7+
//! Note that building the [`cstr`] crate alone is insufficient, as it does not run throuogh any
8+
//! `cstr!()` code generation and thus not trip over std-isms in the generated code.
9+
#![no_std]
10+
11+
use core::ffi::CStr;
12+
13+
pub fn can_use_cstr_macro() -> &'static CStr {
14+
cstr::cstr!("Hello World!")
15+
}

0 commit comments

Comments
 (0)