Implement fastrlp traits for Bytes (#1443)

* implement fastrlp traits for Bytes

* cargo fmt

* bump fastrlp to 0.1.2
This commit is contained in:
Dan Cline 2022-07-04 14:47:00 -04:00 committed by GitHub
parent c077a633c1
commit b232afaabc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 2 deletions

View File

@ -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<bytes::Bytes> 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<Self, fastrlp::DecodeError> {
Ok(Self(bytes::Bytes::decode(buf)?))
}
}
#[derive(Debug, Clone, Error)]
#[error("Failed to parse bytes: {0}")]
pub struct ParseBytesError(String);