-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unify & optimise serialization (#139)
- Loading branch information
Denis Varlakov
authored
Sep 15, 2021
1 parent
6126e62
commit 5ee0400
Showing
4 changed files
with
57 additions
and
6 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
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,43 @@ | ||
use std::fmt; | ||
|
||
use serde::de::Visitor; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
use super::traits::Converter; | ||
use super::BigInt; | ||
|
||
impl Serialize for BigInt { | ||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
let bytes = self.to_bytes(); | ||
serializer.serialize_bytes(&bytes) | ||
} | ||
} | ||
|
||
impl<'de> Deserialize<'de> for BigInt { | ||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
struct BigintVisitor; | ||
|
||
impl<'de> Visitor<'de> for BigintVisitor { | ||
type Value = BigInt; | ||
|
||
fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
write!(f, "bigint") | ||
} | ||
|
||
fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Ok(BigInt::from_bytes(v)) | ||
} | ||
} | ||
|
||
deserializer.deserialize_bytes(BigintVisitor) | ||
} | ||
} |