-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mempool_infra): add serde utils
commit-id:93bc459d
- Loading branch information
1 parent
169929f
commit 74e4436
Showing
5 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,31 @@ | ||
use std::fmt::Debug; | ||
|
||
use bincode::{deserialize, serialize}; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[cfg(test)] | ||
#[path = "serde_utils_test.rs"] | ||
pub mod serde_utils_test; | ||
|
||
// Generic wrapper struct | ||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct SerdeWrapper<T> { | ||
data: T, | ||
} | ||
|
||
impl<T> SerdeWrapper<T> | ||
where | ||
T: Serialize + for<'de> Deserialize<'de> + Debug, | ||
{ | ||
pub fn new(data: T) -> Self { | ||
Self { data } | ||
} | ||
|
||
pub fn to_bincode(&self) -> Result<Vec<u8>, bincode::Error> { | ||
serialize(self) | ||
} | ||
|
||
pub fn from_bincode(bytes: &[u8]) -> Result<T, bincode::Error> { | ||
deserialize(bytes).map(|serde_wrapper: Self| serde_wrapper.data) | ||
} | ||
} |
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,42 @@ | ||
use std::fmt::Debug; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use starknet_types_core::felt::Felt; | ||
|
||
use crate::serde_utils::SerdeWrapper; | ||
|
||
fn test_generic_data_serde<T>(data: T) | ||
where | ||
T: Serialize + for<'de> Deserialize<'de> + Debug + Clone + Copy + PartialEq, | ||
{ | ||
// Serialize and deserialize the data. | ||
let encoded = SerdeWrapper::new(data).to_bincode().unwrap(); | ||
let decoded = SerdeWrapper::<T>::from_bincode(&encoded).unwrap(); | ||
|
||
// Assert that the data is the same after serialization and deserialization. | ||
assert_eq!(data, decoded); | ||
} | ||
|
||
#[test] | ||
fn test_serde_native_type() { | ||
let data: u32 = 8; | ||
test_generic_data_serde(data); | ||
} | ||
|
||
#[test] | ||
fn test_serde_struct_type() { | ||
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)] | ||
struct TestStruct { | ||
a: u32, | ||
b: u32, | ||
} | ||
|
||
let data: TestStruct = TestStruct { a: 17, b: 8 }; | ||
test_generic_data_serde(data); | ||
} | ||
|
||
#[test] | ||
fn test_serde_felt() { | ||
let data: Felt = Felt::ONE; | ||
test_generic_data_serde(data); | ||
} |