Skip to content

Commit

Permalink
add mod encode, decode
Browse files Browse the repository at this point in the history
  • Loading branch information
nkysg committed Sep 15, 2024
1 parent f551195 commit c88195c
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 74 deletions.
34 changes: 34 additions & 0 deletions crates/storage/codecs/src/decode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use crate::DecodeError;
use alloc::{string::String, vec::Vec};
use alloy_primitives::{Address, B256};
use core::fmt::Debug;

/// Trait that will transform the data to be read from the DB.
pub trait Decode: Send + Sync + Sized + Debug {
/// Decodes data coming from the database.
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError>;
}

impl Decode for Vec<u8> {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError> {
Ok(value.as_ref().to_vec())
}
}

impl Decode for Address {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError> {
Ok(Self::from_slice(value.as_ref()))
}
}

impl Decode for B256 {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError> {
Ok(Self::new(value.as_ref().try_into().map_err(|_| DecodeError)?))
}
}

impl Decode for String {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError> {
Self::from_utf8(value.as_ref().to_vec()).map_err(|_| DecodeError)
}
}
41 changes: 41 additions & 0 deletions crates/storage/codecs/src/encode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use alloc::{string::String, vec::Vec};
use alloy_primitives::{Address, B256};
use core::fmt::Debug;

/// Trait that will transform the data to be saved in the DB.
pub trait Encode: Send + Sync + Sized + Debug {
/// Encoded type.
type Encoded: AsRef<[u8]> + Into<Vec<u8>> + Send + Sync + Ord + Debug;

/// Encodes data going into the database.
fn encode(self) -> Self::Encoded;
}

impl Encode for Vec<u8> {
type Encoded = Self;

fn encode(self) -> Self::Encoded {
self
}
}
impl Encode for Address {
type Encoded = [u8; 20];

fn encode(self) -> Self::Encoded {
self.0 .0
}
}
impl Encode for B256 {
type Encoded = [u8; 32];

fn encode(self) -> Self::Encoded {
self.0
}
}
impl Encode for String {
type Encoded = Vec<u8>;

fn encode(self) -> Self::Encoded {
self.into_bytes()
}
}
79 changes: 5 additions & 74 deletions crates/storage/codecs/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,22 @@

pub use reth_codecs_derive::*;

use alloy_primitives::{Address, Bloom, Bytes, FixedBytes, B256, U256};
use alloy_primitives::{Address, Bloom, Bytes, FixedBytes, U256};
use bytes::{Buf, BufMut};

extern crate alloc;
use alloc::vec::Vec;

use core::fmt::Debug;

#[cfg(any(test, feature = "alloy"))]
mod alloy;

mod error;
pub use error::DecodeError;

mod encode;
pub use encode::Encode;
mod decode;
pub use decode::Decode;
/// Trait that implements the `Compact` codec.
///
/// When deriving the trait for custom structs, be aware of certain limitations/recommendations:
Expand Down Expand Up @@ -482,21 +484,6 @@ const fn decode_varuint_panic() -> ! {
panic!("could not decode varuint");
}

/// Trait that will transform the data to be saved in the DB.
pub trait Encode: Send + Sync + Sized + Debug {
/// Encoded type.
type Encoded: AsRef<[u8]> + Into<Vec<u8>> + Send + Sync + Ord + Debug;

/// Encodes data going into the database.
fn encode(self) -> Self::Encoded;
}

/// Trait that will transform the data to be read from the DB.
pub trait Decode: Send + Sync + Sized + Debug {
/// Decodes data coming from the database.
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError>;
}

/// Macro that implements [`Encode`] and [`Decode`] for uint types.
macro_rules! impl_uints {
($($name:tt),+) => {
Expand Down Expand Up @@ -524,62 +511,6 @@ macro_rules! impl_uints {

impl_uints!(u64, u32, u16, u8);

impl Encode for Vec<u8> {
type Encoded = Self;

fn encode(self) -> Self::Encoded {
self
}
}

impl Decode for Vec<u8> {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError> {
Ok(value.as_ref().to_vec())
}
}

impl Encode for Address {
type Encoded = [u8; 20];

fn encode(self) -> Self::Encoded {
self.0 .0
}
}

impl Decode for Address {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError> {
Ok(Self::from_slice(value.as_ref()))
}
}

impl Encode for B256 {
type Encoded = [u8; 32];

fn encode(self) -> Self::Encoded {
self.0
}
}

impl Decode for B256 {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError> {
Ok(Self::new(value.as_ref().try_into().map_err(|_| DecodeError)?))
}
}

impl Encode for String {
type Encoded = Vec<u8>;

fn encode(self) -> Self::Encoded {
self.into_bytes()
}
}

impl Decode for String {
fn decode<B: AsRef<[u8]>>(value: B) -> Result<Self, DecodeError> {
Self::from_utf8(value.as_ref().to_vec()).map_err(|_| DecodeError)
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit c88195c

Please sign in to comment.