Skip to content

Commit

Permalink
impl PduEncode for Box<dyn PduEncode>
Browse files Browse the repository at this point in the history
which allows us to get rid of the *_box() functions
  • Loading branch information
Isaiah Becker-Mayer committed Aug 21, 2023
1 parent 925c420 commit 868d7ec
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 21 deletions.
33 changes: 14 additions & 19 deletions crates/ironrdp-pdu/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,28 +141,32 @@ pub trait PduEncode {

assert_obj_safe!(PduEncode);

impl PduEncode for Box<dyn PduEncode> {
fn encode(&self, dst: &mut WriteCursor<'_>) -> PduResult<()> {
self.as_ref().encode(dst)
}

fn name(&self) -> &'static str {
self.as_ref().name()
}

fn size(&self) -> usize {
self.as_ref().size()
}
}

/// Encodes the given PDU in-place into the provided buffer and returns the number of bytes written.
pub fn encode<T: PduEncode>(pdu: &T, dst: &mut [u8]) -> PduResult<usize> {
let mut cursor = WriteCursor::new(dst);
encode_cursor(pdu, &mut cursor)?;
Ok(cursor.pos())
}

pub fn encode_box(pdu: Box<dyn PduEncode>, dst: &mut [u8]) -> PduResult<usize> {
let mut cursor = WriteCursor::new(dst);
encode_cursor_box(pdu, &mut cursor)?;
Ok(cursor.pos())
}

/// Encodes the given PDU in-place using the provided `WriteCursor`.
pub fn encode_cursor<T: PduEncode>(pdu: &T, dst: &mut WriteCursor<'_>) -> PduResult<()> {
pdu.encode(dst)
}

pub fn encode_cursor_box(pdu: Box<dyn PduEncode>, dst: &mut WriteCursor<'_>) -> PduResult<()> {
pdu.encode(dst)
}

/// Same as `encode` but resizes the buffer when it is too small to fit the PDU.
#[cfg(feature = "alloc")]
pub fn encode_buf<T: PduEncode>(pdu: &T, buf: &mut WriteBuf) -> PduResult<usize> {
Expand All @@ -174,15 +178,6 @@ pub fn encode_buf<T: PduEncode>(pdu: &T, buf: &mut WriteBuf) -> PduResult<usize>
Ok(written)
}

pub fn encode_buf_box(pdu: Box<dyn PduEncode>, buf: &mut WriteBuf) -> PduResult<usize> {
let pdu_size = pdu.size();
let dst = buf.unfilled_to(pdu_size);
let written = encode_box(pdu, dst)?;
debug_assert_eq!(written, pdu_size);
buf.advance(written);
Ok(written)
}

/// Same as `encode` but allocates and returns a new buffer each time.
///
/// This is a convenience function, but it’s not very ressource efficient.
Expand Down
4 changes: 2 additions & 2 deletions crates/ironrdp-svc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use bitflags::bitflags;
use core::any::{Any, TypeId};
use core::fmt;
use pdu::cursor::WriteCursor;
use pdu::{encode_buf, encode_buf_box, PduEncode};
use pdu::{encode_buf, PduEncode};

use ironrdp_pdu::gcc::{ChannelName, ChannelOptions};
use ironrdp_pdu::write_buf::WriteBuf;
Expand Down Expand Up @@ -77,7 +77,7 @@ pub fn chunkify(pdus: Vec<Box<dyn PduEncode>>, max_chunk_len: usize) -> PduResul
/// [[ ChannelPDUHeader | 1600 bytes of PDU data ] [ ChannelPDUHeader | 1600 bytes of PDU data ] [ ChannelPDUHeader | 800 bytes of PDU data ]]
fn chunkify_one(pdu: Box<dyn PduEncode>, max_chunk_len: usize) -> PduResult<Vec<WriteBuf>> {
let mut encoded_pdu = WriteBuf::new(); // TODO(perf): reuse this buffer using `clear` and `filled` as appropriate
encode_buf_box(pdu, &mut encoded_pdu)?;
encode_buf(&pdu, &mut encoded_pdu)?;

let mut chunks = Vec::new();

Expand Down

0 comments on commit 868d7ec

Please sign in to comment.