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

Support no_std (Rust 1.64 and later) (Take 2) #14

Merged
merged 3 commits into from
Sep 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ jobs:
command: check
args: --tests

build_no_std:
name: Build on no_std
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
target: thumbv7em-none-eabi
override: true
- uses: actions-rs/cargo@v1
with:
command: build
args: >-
--verbose
--target thumbv7em-none-eabi
--manifest-path tests/test_no_std/Cargo.toml

test:
name: Test Suite
runs-on: ubuntu-latest
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ license = "MIT"
keywords = ["macro", "cstr"]
readme = "README.md"
edition = "2018"
rust-version = "1.64"

[lib]
proc-macro = true
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ A macro for getting `&'static CStr` from literal or identifier.
This macro checks whether the given literal is valid for `CStr`
at compile time, and returns a static reference of `CStr`.

This macro can be used to to initialize constants on Rust 1.59 and above.
This macro can be used to to initialize constants on Rust 1.64 and above.

## Example

Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//! This macro checks whether the given literal is valid for `CStr`
//! at compile time, and returns a static reference of `CStr`.
//!
//! This macro can be used to to initialize constants on Rust 1.59 and above.
//! This macro can be used to to initialize constants on Rust 1.64 and above.
//!
//! ## Example
//!
Expand Down Expand Up @@ -37,7 +37,7 @@ struct Error(Span, &'static str);
#[proc_macro]
pub fn cstr(input: RawTokenStream) -> RawTokenStream {
let tokens = match build_byte_str(input.into()) {
Ok(s) => quote!(unsafe { ::std::ffi::CStr::from_bytes_with_nul_unchecked(#s) }),
Ok(s) => quote!(unsafe { ::core::ffi::CStr::from_bytes_with_nul_unchecked(#s) }),
Err(Error(span, msg)) => quote_spanned!(span => compile_error!(#msg)),
};
tokens.into()
Expand Down
2 changes: 2 additions & 0 deletions tests/test_no_std/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target/
Cargo.lock
10 changes: 10 additions & 0 deletions tests/test_no_std/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "test_no_std"
version = "0.0.0"
edition = "2021"
license = "MIT"

publish = false

[dependencies]
cstr.path = "../../"
15 changes: 15 additions & 0 deletions tests/test_no_std/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//! Verifies that cstr! can be used on no_std systems.
//!
//! To ensure that `std` is not sneaked in through a dependency (even though this crate has none at
//! runtime), this should best be built on a target that has no `std` because it has no operating
//! system, eg. thumbv7em-none-eabi.
//!
//! Note that building the [`cstr`] crate alone is insufficient, as it does not run throuogh any
//! `cstr!()` code generation and thus not trip over std-isms in the generated code.
#![no_std]

use core::ffi::CStr;

pub fn can_use_cstr_macro() -> &'static CStr {
cstr::cstr!("Hello World!")
}