1use crate::ffi::{CStr, CString, c_char};
2use crate::ops::Index;
3use crate::{fmt, mem, ptr};
45/// Helper type to manage ownership of the strings within a C-style array.
6///
7/// This type manages an array of C-string pointers terminated by a null
8/// pointer. The pointer to the array (as returned by `as_ptr`) can be used as
9/// a value of `argv` or `environ`.
10pub struct CStringArray {
11 ptrs: Vec<*const c_char>,
12}
1314impl CStringArray {
15/// Creates a new `CStringArray` with enough capacity to hold `capacity`
16 /// strings.
17pub fn with_capacity(capacity: usize) -> Self {
18let mut result = CStringArray { ptrs: Vec::with_capacity(capacity + 1) };
19 result.ptrs.push(ptr::null());
20 result
21 }
2223/// Replace the string at position `index`.
24pub fn write(&mut self, index: usize, item: CString) {
25let argc = self.ptrs.len() - 1;
26let ptr = &mut self.ptrs[..argc][index];
27let old = mem::replace(ptr, item.into_raw());
28// SAFETY:
29 // `CStringArray` owns all of its strings, and they were all transformed
30 // into pointers using `CString::into_raw`. Also, this is not the null
31 // pointer since the indexing above would have failed.
32drop(unsafe { CString::from_raw(old.cast_mut()) });
33 }
3435/// Push an additional string to the array.
36pub fn push(&mut self, item: CString) {
37let argc = self.ptrs.len() - 1;
38// Replace the null pointer at the end of the array...
39self.ptrs[argc] = item.into_raw();
40// ... and recreate it to restore the data structure invariant.
41self.ptrs.push(ptr::null());
42 }
4344/// Returns a pointer to the C-string array managed by this type.
45pub fn as_ptr(&self) -> *const *const c_char {
46self.ptrs.as_ptr()
47 }
4849/// Returns an iterator over all `CStr`s contained in this array.
50pub fn iter(&self) -> CStringIter<'_> {
51 CStringIter { iter: self.ptrs[..self.ptrs.len() - 1].iter() }
52 }
53}
5455impl Index<usize> for CStringArray {
56type Output = CStr;
57fn index(&self, index: usize) -> &CStr {
58let ptr = self.ptrs[..self.ptrs.len() - 1][index];
59// SAFETY:
60 // `CStringArray` owns all of its strings. Also, this is not the null
61 // pointer since the indexing above would have failed.
62unsafe { CStr::from_ptr(ptr) }
63 }
64}
6566impl fmt::Debug for CStringArray {
67fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68 f.debug_list().entries(self.iter()).finish()
69 }
70}
7172// SAFETY: `CStringArray` is basically just a `Vec<CString>`
73unsafe impl Send for CStringArray {}
74// SAFETY: `CStringArray` is basically just a `Vec<CString>`
75unsafe impl Sync for CStringArray {}
7677impl Drop for CStringArray {
78fn drop(&mut self) {
79// SAFETY:
80 // `CStringArray` owns all of its strings, and they were all transformed
81 // into pointers using `CString::into_raw`.
82self.ptrs[..self.ptrs.len() - 1]
83 .iter()
84 .for_each(|&p| drop(unsafe { CString::from_raw(p.cast_mut()) }))
85 }
86}
8788/// An iterator over all `CStr`s contained in a `CStringArray`.
89#[derive(Clone)]
90pub struct CStringIter<'a> {
91 iter: crate::slice::Iter<'a, *const c_char>,
92}
9394impl<'a> Iterator for CStringIter<'a> {
95type Item = &'a CStr;
96fn next(&mut self) -> Option<&'a CStr> {
97// SAFETY:
98 // `CStringArray` owns all of its strings. Also, this is not the null
99 // pointer since the last element is excluded when creating `iter`.
100self.iter.next().map(|&p| unsafe { CStr::from_ptr(p) })
101 }
102103fn size_hint(&self) -> (usize, Option<usize>) {
104self.iter.size_hint()
105 }
106}
107108impl<'a> ExactSizeIterator for CStringIter<'a> {
109fn len(&self) -> usize {
110self.iter.len()
111 }
112fn is_empty(&self) -> bool {
113self.iter.is_empty()
114 }
115}