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
use std::{ffi::CString, ptr::null};
#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum ObsString {
Static(&'static str),
Dynamic(CString),
}
impl ObsString {
pub const unsafe fn from_nul_terminted_str(string: &'static str) -> Self {
Self::Static(string)
}
pub fn as_str(&self) -> &str {
match self {
Self::Static(s) => *s,
Self::Dynamic(s) => s.as_c_str().to_str().unwrap(),
}
}
pub fn as_ptr(&self) -> *const std::os::raw::c_char {
match self {
Self::Static(s) => (*s).as_ptr() as *const std::os::raw::c_char,
Self::Dynamic(s) => s.as_ptr(),
}
}
pub fn ptr_or_null(opt: &Option<Self>) -> *const std::os::raw::c_char {
opt.as_ref().map(|s| s.as_ptr()).unwrap_or(null())
}
}
impl<T: Into<Vec<u8>>> From<T> for ObsString {
fn from(s: T) -> Self {
Self::Dynamic(CString::new(s).expect("Failed to convert to CString"))
}
}
#[macro_export]
macro_rules! obs_string {
($e:expr) => {
unsafe { $crate::string::ObsString::from_nul_terminted_str(concat!($e, "\0")) }
};
}