use alloc::fmt;
use core::ffi::CStr;
use crate::traits::StringToFromBytes;
#[derive(Debug)]
pub struct InteriorNulError {
pub position: usize,
}
impl fmt::Display for InteriorNulError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Interior NUL byte found at position {}", self.position)
}
}
impl core::error::Error for InteriorNulError {}
impl StringToFromBytes for CStr {
#[cfg(feature = "safe")]
#[inline]
fn bytes_as_self(bytes: &[u8]) -> &Self {
CStr::from_bytes_with_nul(bytes).expect("Missing NUL byte")
}
#[cfg(not(feature = "safe"))]
#[inline]
fn bytes_as_self(bytes: &[u8]) -> &Self {
unsafe { CStr::from_bytes_with_nul_unchecked(bytes) }
}
#[inline]
fn self_as_bytes(&self) -> &[u8] {
self.to_bytes()
}
#[inline]
fn self_as_raw_bytes(&self) -> &[u8] {
self.to_bytes_with_nul()
}
#[inline]
fn empty_raw_bytes() -> &'static [u8] {
&[0]
}
}