pub struct CStringArray { /* private fields */ }
Expand description
Safe wrapper for passing string arrays to C FFI as char**
.
This structure provides a safe abstraction over the common C pattern of
null-terminated arrays of strings (char**
), often used for command-line
arguments (argv
).
§Memory Safety
- Guarantees proper memory layout compatible with C’s
char**
- Automatically manages lifetime of all C strings
- Ensures null-termination of the pointer array
- Prevents dangling pointers through RAII
- Zero-copy when constructed from
Vec<CString>
§Example
use std::ffi::c_char;
use cstring_array::CStringArray;
let args = vec![
"program".to_string(),
"--verbose".to_string(),
"file.txt".to_string(),
];
let array = CStringArray::new(args).unwrap();
// Safe to pass to C FFI
let ptr: *const *const c_char = array.as_ptr();
assert_eq!(array.len(), 3);
Implementations§
Source§impl CStringArray
impl CStringArray
Sourcepub fn new(strings: Vec<String>) -> Result<Self, CStringArrayError>
pub fn new(strings: Vec<String>) -> Result<Self, CStringArrayError>
Creates a new CStringArray
from a vector of strings.
§Arguments
strings
- Vector of strings to convert into C-compatible format
§Errors
Returns CStringArrayError::NulError
if any string contains an interior
null byte. Returns CStringArrayError::EmptyArray
if the input
vector is empty.
§Example
use cstring_array::CStringArray;
let args = vec!["foo".to_string(), "bar".to_string()];
let array = CStringArray::new(args).unwrap();
assert_eq!(array.len(), 2);
Sourcepub fn from_cstrings(strings: Vec<CString>) -> Result<Self, CStringArrayError>
pub fn from_cstrings(strings: Vec<CString>) -> Result<Self, CStringArrayError>
Creates a new CStringArray
from a vector of CString
s (zero-copy).
This is the most efficient constructor as it takes ownership of
already-allocated CString
instances without re-allocation.
§Arguments
strings
- Vector ofCString
instances
§Errors
Returns CStringArrayError::EmptyArray
if the input vector is empty.
§Example
use std::ffi::CString;
use cstring_array::CStringArray;
let cstrings = vec![
CString::new("hello").unwrap(),
CString::new("world").unwrap(),
];
let array = CStringArray::from_cstrings(cstrings).unwrap();
assert_eq!(array.len(), 2);
Sourcepub fn as_ptr(&self) -> *const *const c_char
pub fn as_ptr(&self) -> *const *const c_char
Returns a pointer suitable for passing to C functions expecting
char**
.
The returned pointer is valid for the lifetime of this CStringArray
.
The pointer array is null-terminated as required by C conventions.
§Safety
The caller must ensure that:
- The pointer is not used after this
CStringArray
is dropped - The pointer is not used to modify the strings (use
as_mut_ptr
for that)
§Example
use std::ffi::c_char;
use cstring_array::CStringArray;
let array = CStringArray::new(vec!["test".to_string()]).unwrap();
let ptr: *const *const c_char = array.as_ptr();
// Safe to pass to C FFI functions like execve, etc.
Sourcepub fn as_mut_ptr(&mut self) -> *mut *const c_char
pub fn as_mut_ptr(&mut self) -> *mut *const c_char
Returns a mutable pointer suitable for C functions expecting char**
.
§Safety
The caller must ensure that:
- The pointer is not used after this
CStringArray
is dropped - C code does not replace pointers in the array (undefined behavior)
- C code only modifies string contents, not pointer values
§Example
use std::ffi::c_char;
use cstring_array::CStringArray;
let mut array = CStringArray::new(vec!["test".to_string()]).unwrap();
let ptr: *mut *const c_char = array.as_mut_ptr();
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of strings in the array.
This count does not include the null terminator.
§Example
use cstring_array::CStringArray;
let array = CStringArray::new(vec!["a".to_string(), "b".to_string()]).unwrap();
assert_eq!(array.len(), 2);
Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true
if the array contains no strings.
Note: Due to the constructor constraints, this will always return
false
for successfully constructed instances, but is provided for
completeness.
§Example
use cstring_array::CStringArray;
let array = CStringArray::new(vec!["x".to_string()]).unwrap();
assert!(!array.is_empty());
Sourcepub fn get(&self, index: usize) -> Option<&CString>
pub fn get(&self, index: usize) -> Option<&CString>
Returns a reference to the underlying CString
at the specified index.
§Arguments
index
- The index of the string to retrieve
§Returns
Returns Some(&CString)
if the index is valid, None
otherwise.
§Example
use cstring_array::CStringArray;
let array = CStringArray::new(vec!["first".to_string(), "second".to_string()]).unwrap();
assert_eq!(array.get(0).unwrap().to_str().unwrap(), "first");
assert_eq!(array.get(1).unwrap().to_str().unwrap(), "second");
assert!(array.get(2).is_none());
Sourcepub fn iter(&self) -> Iter<'_, CString>
pub fn iter(&self) -> Iter<'_, CString>
Returns an iterator over the CString
references.
§Example
use cstring_array::CStringArray;
let array = CStringArray::new(vec!["a".to_string(), "b".to_string()]).unwrap();
let strings: Vec<_> = array.iter().collect();
assert_eq!(strings.len(), 2);
Sourcepub fn as_slice(&self) -> &[CString]
pub fn as_slice(&self) -> &[CString]
Returns a slice of the underlying CString
array.
§Example
use cstring_array::CStringArray;
let array = CStringArray::new(vec!["a".to_string()]).unwrap();
let slice = array.as_slice();
assert_eq!(slice.len(), 1);
Sourcepub fn into_strings(self) -> Vec<CString>
pub fn into_strings(self) -> Vec<CString>
Consumes the array and returns the underlying vector of CString
s.
§Example
use cstring_array::CStringArray;
let array = CStringArray::new(vec!["test".to_string()]).unwrap();
let strings = array.into_strings();
assert_eq!(strings.len(), 1);
Trait Implementations§
Source§impl AsRef<[CString]> for CStringArray
impl AsRef<[CString]> for CStringArray
Source§impl Clone for CStringArray
impl Clone for CStringArray
Source§impl Debug for CStringArray
impl Debug for CStringArray
Source§impl Drop for CStringArray
impl Drop for CStringArray
Source§impl FromIterator<CString> for CStringArray
impl FromIterator<CString> for CStringArray
Source§impl FromIterator<String> for CStringArray
impl FromIterator<String> for CStringArray
Source§impl Hash for CStringArray
impl Hash for CStringArray
Source§impl Index<usize> for CStringArray
impl Index<usize> for CStringArray
Source§impl<'a> IntoIterator for &'a CStringArray
impl<'a> IntoIterator for &'a CStringArray
Source§impl IntoIterator for CStringArray
impl IntoIterator for CStringArray
Source§impl PartialEq for CStringArray
impl PartialEq for CStringArray
Source§impl<const N: usize> TryFrom<[&str; N]> for CStringArray
impl<const N: usize> TryFrom<[&str; N]> for CStringArray
Source§fn try_from(strings: [&str; N]) -> Result<Self, Self::Error>
fn try_from(strings: [&str; N]) -> Result<Self, Self::Error>
Converts an array of string slices into a CStringArray
.
§Errors
Returns an error if any string contains an interior null byte or if the array is empty.
§Example
use std::convert::TryFrom;
use cstring_array::CStringArray;
let strings = ["hello", "world"];
let array = CStringArray::try_from(strings).unwrap();
assert_eq!(array.len(), 2);
Source§type Error = CStringArrayError
type Error = CStringArrayError
Source§impl<const N: usize> TryFrom<[String; N]> for CStringArray
impl<const N: usize> TryFrom<[String; N]> for CStringArray
Source§fn try_from(strings: [String; N]) -> Result<Self, Self::Error>
fn try_from(strings: [String; N]) -> Result<Self, Self::Error>
Converts an array of String
s into a CStringArray
.
§Errors
Returns an error if any string contains an interior null byte or if the array is empty.
§Example
use std::convert::TryFrom;
use cstring_array::CStringArray;
let strings = ["hello".to_string(), "world".to_string()];
let array = CStringArray::try_from(strings).unwrap();
assert_eq!(array.len(), 2);
Source§type Error = CStringArrayError
type Error = CStringArrayError
Source§impl TryFrom<Vec<&str>> for CStringArray
impl TryFrom<Vec<&str>> for CStringArray
Source§fn try_from(strings: Vec<&str>) -> Result<Self, Self::Error>
fn try_from(strings: Vec<&str>) -> Result<Self, Self::Error>
Converts a Vec<&str>
into a CStringArray
.
§Errors
Returns an error if any string contains an interior null byte or if the vector is empty.
§Example
use std::convert::TryFrom;
use cstring_array::CStringArray;
let strings = vec!["hello", "world"];
let array = CStringArray::try_from(strings).unwrap();
assert_eq!(array.len(), 2);
Source§type Error = CStringArrayError
type Error = CStringArrayError
Source§impl TryFrom<Vec<CString>> for CStringArray
impl TryFrom<Vec<CString>> for CStringArray
Source§fn try_from(strings: Vec<CString>) -> Result<Self, Self::Error>
fn try_from(strings: Vec<CString>) -> Result<Self, Self::Error>
Converts a Vec<CString>
into a CStringArray
(zero-copy).
§Errors
Returns an error if the vector is empty.
§Example
use std::{convert::TryFrom, ffi::CString};
use cstring_array::CStringArray;
let cstrings = vec![
CString::new("hello").unwrap(),
CString::new("world").unwrap(),
];
let array = CStringArray::try_from(cstrings).unwrap();
assert_eq!(array.len(), 2);
Source§type Error = CStringArrayError
type Error = CStringArrayError
Source§impl TryFrom<Vec<String>> for CStringArray
impl TryFrom<Vec<String>> for CStringArray
Source§fn try_from(strings: Vec<String>) -> Result<Self, Self::Error>
fn try_from(strings: Vec<String>) -> Result<Self, Self::Error>
Converts a Vec<String>
into a CStringArray
.
§Errors
Returns an error if any string contains an interior null byte or if the vector is empty.
§Example
use std::convert::TryFrom;
use cstring_array::CStringArray;
let strings = vec!["hello".to_string(), "world".to_string()];
let array = CStringArray::try_from(strings).unwrap();
assert_eq!(array.len(), 2);