libnotcurses_sys/string.rs
1//! `NcString`
2// WIP
3
4// use crate::c_api::libc::{free, strdup};
5
6use core::ffi::c_char;
7
8#[cfg(not(feature = "std"))]
9use alloc::ffi::CString;
10
11#[cfg(feature = "std")]
12use std::ffi::CString;
13
14/// A wrapped [`CString`] accepted by widgets.
15///
16// MAYBE: also for exporting to `*mut c_char`?
17#[derive(Debug)]
18pub struct NcString {
19 cstring: CString,
20 // ptr: *mut c_char,
21 // deallocate: bool,
22}
23impl NcString {
24 ///
25 pub fn new(string: &str) -> Self {
26 // let cstring = CString::new(string).expect("CString::new");
27 // let ptr = unsafe { strdup(cstring.as_ptr()) };
28 // Self { ptr, deallocate: true }
29 Self { cstring: CString::new(string).expect("CString::new") }
30 }
31
32 pub fn as_ptr(&self) -> *const c_char {
33 self.cstring.as_ptr()
34 }
35
36 // MAYBE: fn as_raw(self) ? (transfers ownership)
37
38 // /// Choose whether to dellocate the string on drop or not.
39 // pub fn deallocate(&mut self, deallocate: bool) {
40 // self.deallocate = deallocate;
41 // }
42
43 //
44 // pub fn as_mut_ptr(&mut self) -> *mut c_char {
45 // self.cstring.as_mut_ptr()
46 // }
47}
48
49// impl Drop for NcString {
50// fn drop(&mut self) {
51// if self.deallocate {
52// unsafe { free(self.ptr as *mut c_void) };
53// }
54// }
55// }