-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
80 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters