From b232afaabc05b067a81ab632a1a54e7ae2a46a6b Mon Sep 17 00:00:00 2001 From: Dan Cline Date: Mon, 4 Jul 2022 14:47:00 -0400 Subject: [PATCH] Implement fastrlp traits for Bytes (#1443) * implement fastrlp traits for Bytes * cargo fmt * bump fastrlp to 0.1.2 --- ethers-core/src/types/bytes.rs | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/ethers-core/src/types/bytes.rs b/ethers-core/src/types/bytes.rs index 35c0e28d..a23f0521 100644 --- a/ethers-core/src/types/bytes.rs +++ b/ethers-core/src/types/bytes.rs @@ -1,6 +1,5 @@ +use fastrlp::{Decodable, Encodable}; use serde::{Deserialize, Deserializer, Serialize, Serializer}; -use thiserror::Error; - use std::{ borrow::Borrow, clone::Clone, @@ -8,6 +7,7 @@ use std::{ ops::Deref, str::FromStr, }; +use thiserror::Error; /// Wrapper type around Bytes to deserialize/serialize "0x" prefixed ethereum hex strings #[derive(Clone, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize, Ord, PartialOrd)] @@ -131,6 +131,21 @@ impl PartialEq for Bytes { } } +impl Encodable for Bytes { + fn length(&self) -> usize { + self.0.length() + } + fn encode(&self, out: &mut dyn bytes::BufMut) { + self.0.encode(out) + } +} + +impl Decodable for Bytes { + fn decode(buf: &mut &[u8]) -> Result { + Ok(Self(bytes::Bytes::decode(buf)?)) + } +} + #[derive(Debug, Clone, Error)] #[error("Failed to parse bytes: {0}")] pub struct ParseBytesError(String);