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
use std;
use std::sync::Mutex;
use std::ffi::{CStr, CString};
use std::collections::HashMap;
use std::os::raw::c_char;

#[derive(Default, Debug)]
pub struct CustomProperties {
    pub fields: Mutex<HashMap<String, CString>>
}

#[no_mangle]
pub unsafe fn ice_glue_custom_properties_set(cp: *const CustomProperties, k: *const c_char, v: *const c_char) {
    let cp = &*cp;

    cp.fields.lock().unwrap().insert(
        CStr::from_ptr(k).to_str().unwrap().to_string(),
        CString::new(CStr::from_ptr(v).to_str().unwrap()).unwrap()
    );
}

#[no_mangle]
pub unsafe fn ice_glue_custom_properties_get(cp: *const CustomProperties, k: *const c_char) -> *const c_char {
    let cp = &*cp;

    match cp.fields.lock().unwrap().get(CStr::from_ptr(k).to_str().unwrap()) {
        Some(v) => v.as_ptr(),
        None => std::ptr::null()
    }
}