1
//! Small wrapper functions over FFI things.
2

            
3
use std::ffi::{c_char, CStr, CString};
4

            
5
use crate::ffi;
6
use libc::free;
7

            
8
/// Gets a given value from Minion's TableOut (where it stores run statistics).
9
893
pub fn get_from_table(key: String) -> Option<String> {
10
893
    unsafe {
11
893
        #[allow(clippy::expect_used)]
12
893
        let c_string = CString::new(key).expect("");
13
893
        let key_ptr = c_string.into_raw();
14
893
        let val_ptr: *mut c_char = ffi::TableOut_get(key_ptr);
15
893

            
16
893
        drop(CString::from_raw(key_ptr));
17
893

            
18
893
        if val_ptr.is_null() {
19
            free(val_ptr as _);
20
            None
21
        } else {
22
            #[allow(clippy::unwrap_used)]
23
            // CStr borrows the string in the ptr.
24
            // We convert it to &str then clone into a String.
25
893
            let res = CStr::from_ptr(val_ptr).to_str().unwrap().to_owned();
26
893
            free(val_ptr as _);
27
893
            Some(res)
28
        }
29
    }
30
893
}