#![cfg(any(target_os="android", target_os="l4re", target_os="linux"))]
#![doc(cfg(any(target_os="android", target_os="l4re", target_os="linux")))]
use {
std::io::{Error, Read, Write},
crate::Result,
};
#[derive(Debug, Clone, Copy)]
pub (crate) enum Cmd {
DuplicateServerFd,
}
impl Cmd {
const fn id(&self) -> u8 {
*self as u8
}
pub fn encode<W>(&self, writer: &mut W) -> Result<()> where W: Write {
writer.write_all(&[self.id()])
}
pub fn decode<R>(reader: &mut R) -> Result<Self> where R: Read {
let mut buf = [u8::MIN];
reader.read_exact(&mut buf)?;
Self::try_from(&buf[0])
}
}
impl TryFrom<&u8> for Cmd {
type Error = Error;
fn try_from(id: &u8) -> Result<Self> {
for cmd in [Self::DuplicateServerFd] {
if &cmd.id() == id {
return Ok(cmd);
}
}
Err(err!("Unknown ID: {id}"))
}
}
impl TryFrom<u8> for Cmd {
type Error = Error;
fn try_from(id: u8) -> Result<Self> {
Self::try_from(&id)
}
}