Macro safer_ffi::c

source ·
c!() { /* proc-macro */ }
Expand description

Creates a compile-time checked char_p::Ref<'static> out of a string literal.

Example

use ::safer_ffi::prelude::*;

#[ffi_export]
fn concat (s1: char_p::Ref<'_>, s2: char_p::Ref<'_>)
  -> char_p::Box
{
    format!("{}{}", s1.to_str(), s2.to_str())
        .try_into()
        .unwrap() // No inner nulls in our format string
}

fn main ()
{
    assert_eq!(
        concat(c!("Hello, "), c!("World!")).as_ref(),
        c!("Hello, World!"),
    );
}

If the string literal contains an inner null byte, then the macro will detect it at compile time and thus cause a compile-time error (allowing to skip the then unnecessary runtime check!):

let _ = ::safer_ffi::c!("Hell\0, World!"); // <- Compile error