std/sys/process/unix/common/
cstring_array.rs1use crate::ffi::{CStr, CString, c_char};
2use crate::ops::Index;
3use crate::{fmt, mem, ptr};
4
5pub struct CStringArray {
11    ptrs: Vec<*const c_char>,
12}
13
14impl CStringArray {
15    pub fn with_capacity(capacity: usize) -> Self {
18        let mut result = CStringArray { ptrs: Vec::with_capacity(capacity + 1) };
19        result.ptrs.push(ptr::null());
20        result
21    }
22
23    pub fn write(&mut self, index: usize, item: CString) {
25        let argc = self.ptrs.len() - 1;
26        let ptr = &mut self.ptrs[..argc][index];
27        let old = mem::replace(ptr, item.into_raw());
28        drop(unsafe { CString::from_raw(old.cast_mut()) });
33    }
34
35    pub fn push(&mut self, item: CString) {
37        let argc = self.ptrs.len() - 1;
38        self.ptrs[argc] = item.into_raw();
40        self.ptrs.push(ptr::null());
42    }
43
44    pub fn as_ptr(&self) -> *const *const c_char {
46        self.ptrs.as_ptr()
47    }
48
49    pub fn iter(&self) -> CStringIter<'_> {
51        CStringIter { iter: self.ptrs[..self.ptrs.len() - 1].iter() }
52    }
53}
54
55impl Index<usize> for CStringArray {
56    type Output = CStr;
57    fn index(&self, index: usize) -> &CStr {
58        let ptr = self.ptrs[..self.ptrs.len() - 1][index];
59        unsafe { CStr::from_ptr(ptr) }
63    }
64}
65
66impl fmt::Debug for CStringArray {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        f.debug_list().entries(self.iter()).finish()
69    }
70}
71
72unsafe impl Send for CStringArray {}
74unsafe impl Sync for CStringArray {}
76
77impl Drop for CStringArray {
78    fn drop(&mut self) {
79        self.ptrs[..self.ptrs.len() - 1]
83            .iter()
84            .for_each(|&p| drop(unsafe { CString::from_raw(p.cast_mut()) }))
85    }
86}
87
88#[derive(Clone)]
90pub struct CStringIter<'a> {
91    iter: crate::slice::Iter<'a, *const c_char>,
92}
93
94impl<'a> Iterator for CStringIter<'a> {
95    type Item = &'a CStr;
96    fn next(&mut self) -> Option<&'a CStr> {
97        self.iter.next().map(|&p| unsafe { CStr::from_ptr(p) })
101    }
102
103    fn size_hint(&self) -> (usize, Option<usize>) {
104        self.iter.size_hint()
105    }
106}
107
108impl<'a> ExactSizeIterator for CStringIter<'a> {
109    fn len(&self) -> usize {
110        self.iter.len()
111    }
112    fn is_empty(&self) -> bool {
113        self.iter.is_empty()
114    }
115}