use tokio::io::AsyncWriteExt;
pub(crate) struct SimpleReplyRaw {
error: u32,
cookie: u64,
data: Vec<u8>,
}
impl SimpleReplyRaw {
const NBD_SIMPLE_REPLY_MAGIC: u32 = 0x67446698;
pub(crate) fn new(error: u32, cookie: u64, data: Vec<u8>) -> Self {
Self {
error,
cookie,
data,
}
}
pub(crate) async fn write<W>(&self, writer: &mut W) -> Result<(), std::io::Error>
where
W: AsyncWriteExt + Unpin,
{
writer.write_u32(Self::NBD_SIMPLE_REPLY_MAGIC).await?;
writer.write_u32(self.error).await?;
writer.write_u64(self.cookie).await?;
if !self.data.is_empty() {
writer.write_all(&self.data).await?;
}
Ok(())
}
}
impl std::fmt::Debug for SimpleReplyRaw {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
#[derive(Debug)]
struct SimpleReplyRaw<'a> {
error: &'a u32,
cookie: &'a u64,
}
let tmp = SimpleReplyRaw {
error: &self.error,
cookie: &self.cookie,
};
std::fmt::Debug::fmt(&tmp, f)
}
}