use std::ffi::CString;
use crate::{array::CStringArray, error::CStringArrayError};
impl TryFrom<Vec<String>> for CStringArray {
type Error = CStringArrayError;
fn try_from(strings: Vec<String>) -> Result<Self, Self::Error> {
CStringArray::new(strings)
}
}
impl TryFrom<Vec<&str>> for CStringArray {
type Error = CStringArrayError;
fn try_from(strings: Vec<&str>) -> Result<Self, Self::Error> {
let owned: Vec<String> = strings.into_iter().map(String::from).collect();
CStringArray::new(owned)
}
}
impl<const N: usize> TryFrom<[String; N]> for CStringArray {
type Error = CStringArrayError;
fn try_from(strings: [String; N]) -> Result<Self, Self::Error> {
CStringArray::new(strings.to_vec())
}
}
impl<const N: usize> TryFrom<[&str; N]> for CStringArray {
type Error = CStringArrayError;
fn try_from(strings: [&str; N]) -> Result<Self, Self::Error> {
let owned: Vec<String> = strings.into_iter().map(String::from).collect();
CStringArray::new(owned)
}
}
impl TryFrom<Vec<CString>> for CStringArray {
type Error = CStringArrayError;
fn try_from(strings: Vec<CString>) -> Result<Self, Self::Error> {
CStringArray::from_cstrings(strings)
}
}
impl PartialEq for CStringArray {
fn eq(&self, other: &Self) -> bool {
self.as_slice() == other.as_slice()
}
}
impl Eq for CStringArray {}
impl std::hash::Hash for CStringArray {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_slice().hash(state);
}
}
impl Clone for CStringArray {
fn clone(&self) -> Self {
Self::from_cstrings(self.as_slice().to_vec()).expect("clone from non-empty array")
}
}
impl FromIterator<String> for CStringArray {
fn from_iter<I: IntoIterator<Item = String>>(iter: I) -> Self {
let strings: Vec<String> = iter.into_iter().collect();
Self::new(strings).expect("FromIterator from non-empty iterator")
}
}
impl FromIterator<CString> for CStringArray {
fn from_iter<I: IntoIterator<Item = CString>>(iter: I) -> Self {
let strings: Vec<CString> = iter.into_iter().collect();
Self::from_cstrings(strings).expect("FromIterator from non-empty iterator")
}
}
impl IntoIterator for CStringArray {
type Item = CString;
type IntoIter = std::vec::IntoIter<CString>;
fn into_iter(self) -> Self::IntoIter {
self.into_strings().into_iter()
}
}
impl<'a> IntoIterator for &'a CStringArray {
type Item = &'a CString;
type IntoIter = std::slice::Iter<'a, CString>;
fn into_iter(self) -> Self::IntoIter {
self.iter()
}
}
impl std::ops::Index<usize> for CStringArray {
type Output = CString;
fn index(&self, index: usize) -> &Self::Output {
&self.as_slice()[index]
}
}
impl AsRef<[CString]> for CStringArray {
fn as_ref(&self) -> &[CString] {
self.as_slice()
}
}