Skip to content

Commit

Permalink
feat/refactor(rlp): improve implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
DaniPopes committed Jul 9, 2023
1 parent 1c2373e commit 474d0d7
Show file tree
Hide file tree
Showing 10 changed files with 500 additions and 446 deletions.
2 changes: 1 addition & 1 deletion crates/primitives/src/bits/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ macro_rules! impl_rlp {
($t:ty) => {
impl $crate::private::alloy_rlp::Decodable for $t {
#[inline]
fn decode(buf: &mut &[u8]) -> Result<Self, $crate::private::alloy_rlp::DecodeError> {
fn decode(buf: &mut &[u8]) -> $crate::private::alloy_rlp::Result<Self> {
$crate::private::alloy_rlp::Decodable::decode(buf).map(Self)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/primitives/src/bits/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloy_rlp::{impl_max_encoded_len, length_of_length, Decodable, Encodable};

impl<const N: usize> Decodable for FixedBytes<N> {
#[inline]
fn decode(buf: &mut &[u8]) -> Result<Self, alloy_rlp::DecodeError> {
fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
Decodable::decode(buf).map(Self)
}
}
Expand Down
7 changes: 5 additions & 2 deletions crates/primitives/src/bytes/rlp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ use super::Bytes;
use alloy_rlp::{Decodable, Encodable};

impl Encodable for Bytes {
#[inline]
fn length(&self) -> usize {
self.0.length()
}

#[inline]
fn encode(&self, out: &mut dyn bytes::BufMut) {
self.0.encode(out)
}
}

impl Decodable for Bytes {
fn decode(buf: &mut &[u8]) -> Result<Self, alloy_rlp::DecodeError> {
Ok(Self(bytes::Bytes::decode(buf)?))
#[inline]
fn decode(buf: &mut &[u8]) -> Result<Self, alloy_rlp::Error> {
bytes::Bytes::decode(buf).map(Self)
}
}
8 changes: 5 additions & 3 deletions crates/rlp/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[dependencies]
arrayvec.workspace = true
bytes.workspace = true

alloy-rlp-derive = { workspace = true, optional = true }
arrayvec = { workspace = true, optional = true }
smol_str = { version = "0.2", default-features = false, optional = true }

[dev-dependencies]
hex-literal.workspace = true

[features]
default = ["std"]
std = ["arrayvec/std", "bytes/std"]
std = ["bytes/std", "arrayvec?/std", "smol_str?/std"]
derive = ["dep:alloy-rlp-derive"]
arrayvec = ["dep:arrayvec"]
smol_str = ["dep:smol_str"]
Loading

0 comments on commit 474d0d7

Please sign in to comment.