cstr_macro/
lib.rs

1#[macro_export]
2macro_rules! cstr {
3    ($s:expr) => (
4        concat!($s, "\0") as *const str as *const [::std::os::raw::c_char] as *const ::std::os::raw::c_char
5    )
6}
7
8#[cfg(test)]
9mod tests {
10    use ::std::ffi::CStr;
11    use ::std::os::raw::c_char;
12
13    #[test]
14    fn has_right_type() {
15        let val = cstr!("hello world");
16        assert_eq!(to_str(val), "hello world");
17    }
18
19    const hello: *const c_char = cstr!("hello");
20
21    #[test]
22    fn can_be_used_in_const_position() {
23        assert_eq!(to_str(hello), "hello");
24    }
25
26    fn to_str<'a>(input: *const c_char) -> &'a str {
27        unsafe { CStr::from_ptr(input) }.to_str().unwrap()
28    }
29}