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
//! `NcString`
// WIP

// use crate::c_api::libc::{free, strdup};
use core::ffi::c_char;
use std::ffi::CString;

/// A wrapped [`CString`] accepted by widgets.
///
// MAYBE: also for exporting to `*mut c_char`?
#[derive(Debug)]
pub struct NcString {
    cstring: CString,
    // ptr: *mut c_char,
    // deallocate: bool,
}
impl NcString {
    ///
    pub fn new(string: &str) -> Self {
        // let cstring = CString::new(string).expect("CString::new");
        // let ptr = unsafe { strdup(cstring.as_ptr()) };
        // Self { ptr, deallocate: true }
        Self { cstring: CString::new(string).expect("CString::new") }
    }

    pub fn as_ptr(&self) -> *const c_char {
        self.cstring.as_ptr()
    }

    // MAYBE: fn as_raw(self) ? (transfers ownership)

    // /// Choose whether to dellocate the string on drop or not.
    // pub fn deallocate(&mut self, deallocate: bool) {
    //     self.deallocate = deallocate;
    // }

    //
    // pub fn as_mut_ptr(&mut self) -> *mut c_char {
    //     self.cstring.as_mut_ptr()
    // }
}

// impl Drop for NcString {
//     fn drop(&mut self) {
//         if self.deallocate {
//             unsafe { free(self.ptr as *mut c_void) };
//         }
//     }
// }