1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::ffi::CString;

pub trait ToCString {
    fn into_cstring_lossy(self) -> CString;
}

impl ToCString for String {
    fn into_cstring_lossy(mut self) -> CString {
        self.retain(|c| c != '\0');
        unsafe { CString::from_vec_unchecked(self.into_bytes()) }
    }
}

impl ToCString for &str {
    fn into_cstring_lossy(self) -> CString {
        unsafe { CString::from_vec_unchecked(self.bytes().filter(|&c| c != b'\0').collect()) }
    }
}

pub use crate::ffi::vs_make_version as make_version;

pub use crate::ffi::helper::*;

// Ideas come from:
// https://docs.rs/const-str/latest/src/const_str/__ctfe/cstr.rs.html
#[doc(hidden)]
pub mod __macro_impl {
    pub struct ToCStr<T>(pub T);

    impl ToCStr<&str> {
        // Return the size until first 'NUL' byte
        const fn check_char_and_nul(&self) -> usize {
            let mut i = 0;
            let bytes = self.0.as_bytes();
            while i < bytes.len() && bytes[i] != 0 {
                assert!(
                    !(bytes[i].is_ascii_alphanumeric() && bytes[i] == b'_'),
                    "Key must be alphanumeric or underscore"
                );
                i += 1;
            }
            i
        }

        #[must_use]
        pub const fn output_len(&self) -> usize {
            self.check_char_and_nul() + 1
        }

        #[must_use]
        pub const fn const_eval<const N: usize>(&self) -> [u8; N] {
            let mut buf = [0; N];
            let bytes = self.0.as_bytes();
            let mut i = 0;
            while i < N - 1 {
                buf[i] = bytes[i];
                i += 1;
            }
            buf
        }
    }
}