Skip to content

Commit

Permalink
chore: wrap Bytes methods which return Self (#206)
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes authored Jul 26, 2023
1 parent dbaae69 commit 440b1a4
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion crates/primitives/src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alloc::{string::String, vec::Vec};
use core::{
borrow::Borrow,
fmt,
ops::{Deref, DerefMut},
ops::{Deref, DerefMut, RangeBounds},
};

#[cfg(feature = "rlp")]
Expand All @@ -14,6 +14,7 @@ mod serde;
/// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum
/// hex strings.
#[derive(Clone, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct Bytes(pub bytes::Bytes);

impl fmt::Debug for Bytes {
Expand Down Expand Up @@ -237,6 +238,32 @@ impl Bytes {
Self(bytes::Bytes::copy_from_slice(src))
}

/// Returns a slice of self for the provided range.
#[inline]
pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
Self(self.0.slice(range))
}

/// Returns a slice of self that is equivalent to the given `subset`.
#[inline]
pub fn slice_ref(&self, subset: &[u8]) -> Self {
Self(self.0.slice_ref(subset))
}

/// Splits the bytes into two at the given index.
#[must_use = "consider Bytes::truncate if you don't need the other half"]
#[inline]
pub fn split_off(&mut self, at: usize) -> Self {
Self(self.0.split_off(at))
}

/// Splits the bytes into two at the given index.
#[must_use = "consider Bytes::advance if you don't need the other half"]
#[inline]
pub fn split_to(&mut self, at: usize) -> Self {
Self(self.0.split_to(at))
}

#[inline]
fn hex_encode(&self) -> String {
hex::encode_prefixed(self.0.as_ref())
Expand Down

0 comments on commit 440b1a4

Please sign in to comment.