Macro c

Source
macro_rules! c {
    ($s:expr) => { ... };
}
Expand description

CStr literal macro.

Note: Since Rust 1.77, this macro is soft-deprecated in favor of C string literals (c"...").

Path is not available in core, so this crate uses CStr instead in the API where std uses Path. This macro makes it safe and zero-cost to create a CStr from a literal.

use semihosting::{c, fs};

fs::write(c!("a.txt"), "abc")?;
// concat! in c! is also supported
fs::write(c!(concat!("b", ".txt")), "def")?;

This macro guarantees the correctness of the input by compile-time validation. Incorrect input will cause compile-time errors.

use semihosting::c;

let s = c!("ab\0c"); // CStr must not contain any interior nul bytes.
error[E0080]: evaluation of constant value failed
  --> semihosting/src/c_str.rs:48:9
   |
48 |         assert!(byte != 0, "input contained interior nul");
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'input contained interior nul', semihosting/src/c_str.rs:48:9
   |
note: inside `const_c_str_check`
  --> semihosting/src/c_str.rs:48:9
   |
48 |         assert!(byte != 0, "input contained interior nul");
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
note: inside `_`
  --> src/c_str.rs:18:9
   |
5  | let s = c!("ab\0c"); // CStr must not contain any interior nul bytes.
   |         ^^^^^^^^^^^