Skip to content

Commit

Permalink
Message::{encode_to_vec,encode_length_delimited_to_vec}
Browse files Browse the repository at this point in the history
  • Loading branch information
rubdos committed Jan 29, 2021
1 parent 79f0dfd commit f565986
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/message.rs
Expand Up @@ -59,6 +59,18 @@ pub trait Message: Debug + Send + Sync {
Ok(())
}

#[cfg(feature = "std")]
/// Encodes the message to a newly allocated buffer.
fn encode_to_vec(&self) -> Vec<u8>
where
Self: Sized,
{
let mut buf = Vec::with_capacity(self.encoded_len());

self.encode_raw(&mut buf);
buf
}

/// Encodes the message with a length-delimiter to a buffer.
///
/// An error will be returned if the buffer does not have sufficient capacity.
Expand All @@ -78,6 +90,20 @@ pub trait Message: Debug + Send + Sync {
Ok(())
}

#[cfg(feature = "std")]
/// Encodes the message with a length-delimiter to a newly allocated buffer.
fn encode_length_delimited_to_vec(&self) -> Vec<u8>
where
Self: Sized,
{
let len = self.encoded_len();
let mut buf = Vec::with_capacity(len + encoded_len_varint(len as u64));

encode_varint(len as u64, &mut buf);
self.encode_raw(&mut buf);
buf
}

/// Decodes an instance of the message from a buffer.
///
/// The entire buffer will be consumed.
Expand Down

0 comments on commit f565986

Please sign in to comment.