-
Notifications
You must be signed in to change notification settings - Fork 224
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
86 additions
and
5 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
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,78 @@ | ||
/// Serializes and Deserializes [`Sealed`], flattening the struct. | ||
pub mod flat { | ||
use alloy_primitives::{Sealed, B256}; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
#[derive(Serialize)] | ||
struct FlatSer<'a, T> { | ||
seal: B256, | ||
#[serde(flatten)] | ||
t: &'a T, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct FlatDeser<T> { | ||
seal: B256, | ||
#[serde(flatten)] | ||
t: T, | ||
} | ||
|
||
/// Serializes a [`Sealed`] with the given serializer. | ||
pub fn serialize<S, T>(sealed: &Sealed<T>, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
T: Serialize, | ||
{ | ||
FlatSer { seal: sealed.seal(), t: sealed.inner() }.serialize(serializer) | ||
} | ||
|
||
/// Deserializes a [`Sealed`] with the given deserializer. | ||
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<Sealed<T>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
T: Deserialize<'de>, | ||
{ | ||
let FlatDeser { seal, t } = FlatDeser::deserialize(deserializer)?; | ||
Ok(Sealed::new_unchecked(t, seal)) | ||
} | ||
} | ||
|
||
/// Serializes and Deserializes [`Sealed`], flattening the struct and renaming | ||
/// the `seal` key to `hash`. | ||
pub mod flat_hash { | ||
use alloy_primitives::{Sealed, B256}; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
#[derive(Serialize)] | ||
struct FlatSer<'a, T> { | ||
hash: B256, | ||
#[serde(flatten)] | ||
t: &'a T, | ||
} | ||
|
||
#[derive(Deserialize)] | ||
struct FlatDeser<T> { | ||
hash: B256, | ||
#[serde(flatten)] | ||
t: T, | ||
} | ||
|
||
/// Serializes a [`Sealed`] with the given serializer. | ||
pub fn serialize<S, T>(sealed: &Sealed<T>, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
T: Serialize, | ||
{ | ||
FlatSer { hash: sealed.seal(), t: sealed.inner() }.serialize(serializer) | ||
} | ||
|
||
/// Deserializes a [`Sealed`] with the given deserializer. | ||
pub fn deserialize<'de, D, T>(deserializer: D) -> Result<Sealed<T>, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
T: Deserialize<'de>, | ||
{ | ||
let FlatDeser { hash, t } = FlatDeser::deserialize(deserializer)?; | ||
Ok(Sealed::new_unchecked(t, hash)) | ||
} | ||
} |