test_tube/
conversions.rs

1use crate::bindings::GoString;
2use std::ffi::CString;
3
4/// conversion from &CString to GoString
5impl From<&CString> for GoString {
6    fn from(c_str: &CString) -> Self {
7        let ptr_c_str = c_str.as_ptr();
8
9        GoString {
10            p: ptr_c_str,
11            n: c_str.as_bytes().len() as isize,
12        }
13    }
14}
15
16/// This is needed to be implemented as macro since
17/// conversion from &CString to GoString requires
18/// CString to not get dropped before referecing its pointer
19#[macro_export]
20macro_rules! redefine_as_go_string {
21    ($($ident:ident),*) => {
22        $(
23            let $ident = &std::ffi::CString::new($ident).unwrap();
24            let $ident: $crate::bindings::GoString = $ident.into();
25        )*
26    };
27}