Skip to content

Commit

Permalink
Lean on serde for small numeric coercion
Browse files Browse the repository at this point in the history
It appears that serde already expects to do coercion from u64/i64/f64
for the smaller numeric types. So don't try to do it ourselves.

Resolves: #9
Resolves: #10

Signed-off-by: Nathaniel McCallum <[email protected]>
  • Loading branch information
npmccallum committed Dec 10, 2020
1 parent 7c40978 commit 0a683d5
Showing 1 changed file with 2 additions and 22 deletions.
24 changes: 2 additions & 22 deletions src/value/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,19 +157,7 @@ impl<'a, 'de> de::Deserializer<'de> for Deserializer<&'a Value> {
Value::Null => visitor.visit_none(),

Value::Integer(x) => {
if let Ok(x) = u8::try_from(*x) {
visitor.visit_u8(x)
} else if let Ok(x) = i8::try_from(*x) {
visitor.visit_i8(x)
} else if let Ok(x) = u16::try_from(*x) {
visitor.visit_u16(x)
} else if let Ok(x) = i16::try_from(*x) {
visitor.visit_i16(x)
} else if let Ok(x) = u32::try_from(*x) {
visitor.visit_u32(x)
} else if let Ok(x) = i32::try_from(*x) {
visitor.visit_i32(x)
} else if let Ok(x) = u64::try_from(*x) {
if let Ok(x) = u64::try_from(*x) {
visitor.visit_u64(x)
} else if let Ok(x) = i64::try_from(*x) {
visitor.visit_i64(x)
Expand All @@ -182,15 +170,7 @@ impl<'a, 'de> de::Deserializer<'de> for Deserializer<&'a Value> {
}
}

Value::Float(x) => {
if let Ok(x) = f32::try_from(*x) {
visitor.visit_f32(x)
} else if let Ok(x) = f64::try_from(*x) {
visitor.visit_f64(x)
} else {
unreachable!()
}
}
Value::Float(x) => visitor.visit_f64(f64::from(*x)),
}
}

Expand Down

0 comments on commit 0a683d5

Please sign in to comment.