Skip to content

Commit

Permalink
Add Utf8Payload::share
Browse files Browse the repository at this point in the history
  • Loading branch information
alexheretic authored and daniel-abramov committed Dec 14, 2024
1 parent b9b9d75 commit d99fb9d
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/protocol/frame/payload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ impl Utf8Payload {
pub fn is_empty(&self) -> bool {
self.len() == 0
}

/// If owned converts into [`Bytes`] internals & then clones (cheaply).
#[inline]
pub fn share(&mut self) -> Self {
Self(self.0.share())
}
}

impl TryFrom<Payload> for Utf8Payload {
Expand Down Expand Up @@ -131,11 +137,16 @@ impl Payload {
Self::Shared(Bytes::from_owner(owner))
}

/// If owned converts into shared & then clones (cheaply).
#[inline]
/// Converts into [`Bytes`] internals & then clones (cheaply).
pub fn share(&mut self) -> Self {
if let Self::Owned(bytes) = self {
*self = Self::Shared(mem::take(bytes).freeze());
match self {
Self::Owned(data) => {
*self = Self::Shared(mem::take(data).freeze());
}
Self::Vec(data) => {
*self = Self::Shared(Bytes::from_owner(mem::take(data)));
}
Self::Shared(_) => {}
}
self.clone()
}
Expand Down

0 comments on commit d99fb9d

Please sign in to comment.