Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(primitives): improve Bytes #269

Merged
merged 1 commit into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/primitives/src/bits/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ impl<const N: usize> ops::BitXorAssign for FixedBytes<N> {
}
}

impl<const N: usize> core::str::FromStr for FixedBytes<N> {
impl<const N: usize> str::FromStr for FixedBytes<N> {
type Err = hex::FromHexError;

#[inline]
Expand Down
38 changes: 22 additions & 16 deletions crates/primitives/src/bytes/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ mod rlp;
#[cfg(feature = "serde")]
mod serde;

/// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum
/// hex strings.
/// Wrapper type around [`bytes::Bytes`] to support "0x" prefixed hex strings.
#[derive(Clone, Default, Hash, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct Bytes(pub bytes::Bytes);
Expand Down Expand Up @@ -97,49 +96,56 @@ impl<'a> IntoIterator for &'a Bytes {

#[inline]
fn into_iter(self) -> Self::IntoIter {
self.as_ref().iter()
self.iter()
}
}

impl From<bytes::Bytes> for Bytes {
#[inline]
fn from(src: bytes::Bytes) -> Self {
Self(src)
fn from(value: bytes::Bytes) -> Self {
Self(value)
}
}

impl From<Bytes> for bytes::Bytes {
#[inline]
fn from(value: Bytes) -> Self {
value.0
}
}

impl From<Vec<u8>> for Bytes {
#[inline]
fn from(src: Vec<u8>) -> Self {
Self(src.into())
fn from(value: Vec<u8>) -> Self {
Self(value.into())
}
}

impl<const N: usize> From<[u8; N]> for Bytes {
#[inline]
fn from(src: [u8; N]) -> Self {
src.to_vec().into()
fn from(value: [u8; N]) -> Self {
value.to_vec().into()
}
}

impl<'a, const N: usize> From<&'a [u8; N]> for Bytes {
impl<const N: usize> From<&'static [u8; N]> for Bytes {
#[inline]
fn from(src: &'a [u8; N]) -> Self {
src.to_vec().into()
fn from(value: &'static [u8; N]) -> Self {
Self::from_static(value)
}
}

impl From<&'static [u8]> for Bytes {
#[inline]
fn from(value: &'static [u8]) -> Self {
Self(value.into())
Self::from_static(value)
}
}

impl From<&'static str> for Bytes {
#[inline]
fn from(value: &'static str) -> Self {
Self(value.into())
Self::from_static(value.as_bytes())
}
}

Expand Down Expand Up @@ -234,8 +240,8 @@ impl Bytes {

/// Creates a new `Bytes` instance from a slice by copying it.
#[inline]
pub fn copy_from_slice(src: &[u8]) -> Self {
Self(bytes::Bytes::copy_from_slice(src))
pub fn copy_from_slice(data: &[u8]) -> Self {
Self(bytes::Bytes::copy_from_slice(data))
}

/// Returns a slice of self for the provided range.
Expand Down