1use alloc::{borrow::Borrow, ffi::CString, vec::Vec};
2use codec::{Decode, Encode};
3use core::{ffi::CStr, ops::Deref};
4
5#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
7pub struct Arg(pub CString);
8
9impl Encode for Arg {
10 fn encode_to<O: codec::Output + ?Sized>(&self, output: &mut O) {
11 self.0.to_bytes().encode_to(output)
12 }
13}
14
15impl Decode for Arg {
16 fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
17 let mut bytes: Vec<u8> = Decode::decode(input)?;
18 bytes.push(0_u8);
19 let name = CString::from_vec_with_nul(bytes).map_err(|_| "Invalid C-string")?;
20 Ok(Self(name))
21 }
22}
23
24impl AsRef<CStr> for Arg {
25 fn as_ref(&self) -> &CStr {
26 self.0.as_c_str()
27 }
28}
29
30impl Deref for Arg {
31 type Target = CStr;
32
33 fn deref(&self) -> &Self::Target {
34 self.0.as_c_str()
35 }
36}
37
38impl Borrow<CStr> for Arg {
39 fn borrow(&self) -> &CStr {
40 self.0.as_c_str()
41 }
42}
43
44impl Borrow<[u8]> for Arg {
45 fn borrow(&self) -> &[u8] {
46 self.0.to_bytes()
47 }
48}