1pub use alloc_crate::ffi::CString;
8use alloc_crate::vec::Vec;
9
10use crate::alloc_crate;
11
12#[derive(Debug, Clone, PartialEq, Eq)]
14pub enum TryNewError {
15 NulError(usize, Vec<u8>),
17 AllocError,
19}
20
21pub struct FallibleCString(CString);
23
24impl FallibleCString {
25 pub unsafe fn from_vec_unchecked(v: Vec<u8>) -> Result<Self, TryNewError> {
32 let cs = unsafe { CString::from_vec_unchecked(v) };
34 Ok(FallibleCString(cs))
35 }
36
37 pub fn new(v: Vec<u8>) -> Result<Self, TryNewError> {
39 match CString::new(v) {
40 Ok(cs) => Ok(FallibleCString(cs)),
41 Err(e) => Err(TryNewError::NulError(e.nul_position(), e.into_vec())),
42 }
43 }
44}
45
46impl core::ops::Deref for FallibleCString {
47 type Target = CString;
48
49 fn deref(&self) -> &Self::Target {
50 &self.0
51 }
52}
53
54impl From<crate::alloc::AllocError> for TryNewError {
55 fn from(_err: crate::alloc::AllocError) -> Self {
56 TryNewError::AllocError
57 }
58}