use std::io::{self, Read, Write};
use crate::util::io::*;
use super::{Element, SimpleElement, TopElement, ElementLength};
pub const REPLY_ID: u8 = 0xFF;
#[derive(Debug)]
pub struct ReplyHeader {
pub request_id: u32,
}
impl SimpleElement for ReplyHeader {
fn encode(&self, write: &mut impl Write) -> io::Result<()> {
write.write_u32(self.request_id)
}
fn decode(read: &mut impl Read, _len: usize) -> io::Result<Self> {
Ok(Self { request_id: read.read_u32()? })
}
}
impl TopElement for ReplyHeader {
const LEN: ElementLength = ElementLength::Variable32;
}
#[derive(Debug)]
pub struct Reply<E> {
pub request_id: u32,
pub element: E
}
impl<E> Reply<E> {
#[inline]
pub fn new(request_id: u32, element: E) -> Self {
Self { request_id, element }
}
}
impl<E: Element> Element for Reply<E> {
type Config = E::Config;
fn encode(&self, write: &mut impl Write, config: &Self::Config) -> io::Result<()> {
write.write_u32(self.request_id)?;
self.element.encode(write, config)
}
fn decode(read: &mut impl Read, len: usize, config: &Self::Config) -> io::Result<Self> {
Ok(Self {
request_id: read.read_u32()?,
element: E::decode(read, len - 4, config)?,
})
}
}
impl<E: Element> TopElement for Reply<E> {
const LEN: ElementLength = ElementLength::Variable32;
}