Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #585 from ethcore/uint-deserialize
Browse files Browse the repository at this point in the history
deserialization for uint generic
  • Loading branch information
NikVolf committed Mar 4, 2016
2 parents a4f4764 + d59972a commit 3112742
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions util/bigint/src/uint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,35 @@ macro_rules! construct_uint {
}
}

impl serde::Deserialize for $name {
fn deserialize<D>(deserializer: &mut D) -> Result<$name, D::Error>
where D: serde::Deserializer {
struct UintVisitor;

impl serde::de::Visitor for UintVisitor {
type Value = $name;

fn visit_str<E>(&mut self, value: &str) -> Result<Self::Value, E> where E: serde::Error {
// 0x + len
if value.len() != 2 + $n_words / 8 {
return Err(serde::Error::custom("Invalid length."));
}

match $name::from_str(&value[2..]) {
Ok(val) => Ok(val),
Err(_) => { return Err(serde::Error::custom("Invalid length.")); }
}
}

fn visit_string<E>(&mut self, value: String) -> Result<Self::Value, E> where E: serde::Error {
self.visit_str(value.as_ref())
}
}

deserializer.deserialize(UintVisitor)
}
}

impl From<u64> for $name {
fn from(value: u64) -> $name {
let mut ret = [0; $n_words];
Expand Down

0 comments on commit 3112742

Please sign in to comment.