simplified packet construction by using concat instead of IoSlice

This commit is contained in:
Michael 2024-01-31 17:09:05 +00:00
parent 41f0e79f97
commit dd9468788a
Signed by: michael
GPG Key ID: 523BD9EF68BDD44C

View File

@ -1,5 +1,4 @@
use std::io::prelude::*;
use std::io::{IoSlice, Result};
use std::io::Result;
pub struct I3msg<'payload> {
magic_string: &'static str,
@ -21,15 +20,14 @@ impl<'payload> I3msg<'payload> {
pub fn construct_packet(&mut self) -> Result<Vec<u8>> {
let length = self.payload.len().to_ne_bytes();
let payload_type = self.payload_type.to_ne_bytes();
let bufs = &mut [
IoSlice::new(self.magic_string.as_bytes()),
IoSlice::new(&length.as_slice()[..4]),
IoSlice::new(payload_type.as_slice()),
IoSlice::new(self.payload.as_bytes()),
];
self.buffer.write_vectored(bufs)?;
self.buffer.append(
&mut [
self.magic_string.as_bytes().to_vec(),
length.as_slice()[..4].to_vec(),
payload_type.as_slice().to_vec(),
self.payload.as_bytes().to_vec(),
].concat()
);
Ok(self.buffer.to_owned())
}
}