use alloc::{borrow::Borrow, ffi::CString, vec::Vec};
use codec::{Decode, Encode};
use core::{ffi::CStr, ops::Deref};
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
pub struct Arg(pub CString);
impl Encode for Arg {
fn encode_to<O: codec::Output + ?Sized>(&self, output: &mut O) {
self.0.to_bytes().encode_to(output)
}
}
impl Decode for Arg {
fn decode<I: codec::Input>(input: &mut I) -> Result<Self, codec::Error> {
let mut bytes: Vec<u8> = Decode::decode(input)?;
bytes.push(0_u8);
let name = CString::from_vec_with_nul(bytes).map_err(|_| "Invalid C-string")?;
Ok(Self(name))
}
}
impl AsRef<CStr> for Arg {
fn as_ref(&self) -> &CStr {
self.0.as_c_str()
}
}
impl Deref for Arg {
type Target = CStr;
fn deref(&self) -> &Self::Target {
self.0.as_c_str()
}
}
impl Borrow<CStr> for Arg {
fn borrow(&self) -> &CStr {
self.0.as_c_str()
}
}
impl Borrow<[u8]> for Arg {
fn borrow(&self) -> &[u8] {
self.0.to_bytes()
}
}