Skip to content

Commit

Permalink
payload: inline functions
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-abramov committed Dec 13, 2024
1 parent 4eb13ac commit 5a5586a
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/protocol/frame/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum Payload {

impl Payload {
/// Returns a slice of the payload.
#[inline]
pub fn as_slice(&self) -> &[u8] {
match self {
Payload::Owned(v) => v,
Expand All @@ -26,6 +27,7 @@ impl Payload {
/// and there are other references to the same data. No allocation
/// would happen if the payload is owned or if there is only one
/// `Bytes` instance referencing the data.
#[inline]
pub fn as_mut_slice(&mut self) -> &mut [u8] {
match self {
Payload::Owned(v) => &mut *v,
Expand All @@ -43,12 +45,14 @@ impl Payload {
}

/// Returns the length of the payload.
#[inline]
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
self.as_slice().len()
}

/// Consumes the payload and returns the underlying data as a vector.
#[inline]
pub fn into_data(self) -> Vec<u8> {
match self {
Payload::Owned(v) => v,
Expand All @@ -57,6 +61,7 @@ impl Payload {
}

/// Consumes the payload and returns the underlying data as a string.
#[inline]
pub fn into_text(self) -> Result<String, FromUtf8Error> {
match self {
Payload::Owned(v) => Ok(String::from_utf8(v)?),
Expand All @@ -73,7 +78,7 @@ impl From<Vec<u8>> for Payload {

impl From<String> for Payload {
fn from(v: String) -> Self {
Payload::Owned(v.into_bytes())
Payload::Owned(v.into())
}
}

Expand Down

0 comments on commit 5a5586a

Please sign in to comment.