1use core::ffi::CStr;
2
3use bitstream_io::{BitWrite, Endianness};
4
5use crate::{util, BitEncode, Result};
6
7impl<Ctx> BitEncode<Ctx> for CStr {
8 fn encode<W, E>(&self, write: &mut W, ctx: &mut Ctx, (): ()) -> Result<()>
9 where
10 W: BitWrite,
11 E: Endianness,
12 {
13 util::encode_items::<_, E, _, _>(self.to_bytes_with_nul().iter(), write, ctx)
14 }
15}
16
17#[cfg(feature = "alloc")]
18#[allow(clippy::wildcard_imports)]
19mod decode {
20 use alloc::{boxed::Box, ffi::CString};
21 use bitstream_io::BitRead;
22
23 use crate::BitDecode;
24
25 use super::*;
26
27 #[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
28 impl<Ctx> BitDecode<Ctx> for Box<CStr> {
29 fn decode<R, E>(read: &mut R, ctx: &mut Ctx, tag: ()) -> Result<Self>
30 where
31 R: BitRead,
32 E: Endianness,
33 {
34 CString::decode::<_, E>(read, ctx, tag).map(Into::into)
35 }
36 }
37
38 test_decode!(Box<CStr>; [0x41, 0x42, 0x43, 0] => CString::new("ABC").unwrap().into());
39}
40
41test_encode!(
42 &CStr; CStr::from_bytes_with_nul(&[0x41, 0x42, 0x43, 0]).unwrap() => [0x41, 0x42, 0x43, 0]
43);