From 9e3f68fc54babf24133cf66ae6be7d069ba2c271 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 10 May 2020 19:32:34 +0100 Subject: Prefer u64/i64 to usize/isize --- serde_dhall/src/deserialize.rs | 32 ++++---------------------------- serde_dhall/src/lib.rs | 4 ++-- serde_dhall/src/options.rs | 6 +++--- 3 files changed, 9 insertions(+), 33 deletions(-) (limited to 'serde_dhall/src') diff --git a/serde_dhall/src/deserialize.rs b/serde_dhall/src/deserialize.rs index 1206033..12b4703 100644 --- a/serde_dhall/src/deserialize.rs +++ b/serde_dhall/src/deserialize.rs @@ -1,6 +1,5 @@ use std::borrow::Cow; use std::collections::BTreeMap; -use std::convert::TryInto as _; use std::fmt; use serde::de::value::{ @@ -124,31 +123,14 @@ impl<'de: 'a, 'a> serde::Deserializer<'de> for Deserializer<'a> { where V: serde::de::Visitor<'de>, { - use std::convert::TryInto; use NumKind::*; use SimpleValue::*; let val = |x| Deserializer(Cow::Borrowed(x)); match self.0.as_ref() { Num(Bool(x)) => visitor.visit_bool(*x), - Num(Natural(x)) => { - if let Ok(x64) = (*x).try_into() { - visitor.visit_u64(x64) - } else if let Ok(x32) = (*x).try_into() { - visitor.visit_u32(x32) - } else { - unimplemented!() - } - } - Num(Integer(x)) => { - if let Ok(x64) = (*x).try_into() { - visitor.visit_i64(x64) - } else if let Ok(x32) = (*x).try_into() { - visitor.visit_i32(x32) - } else { - unimplemented!() - } - } + Num(Natural(x)) => visitor.visit_u64(*x), + Num(Integer(x)) => visitor.visit_i64(*x), Num(Double(x)) => visitor.visit_f64((*x).into()), Text(x) => visitor.visit_str(x), List(xs) => { @@ -210,17 +192,11 @@ impl<'de> serde::de::Visitor<'de> for SimpleValueVisitor { } fn visit_i64(self, value: i64) -> Result { - // TODO: use `i64` instead of `isize` in `NumKind`. - Ok(SimpleValue::Num(NumKind::Integer( - value.try_into().unwrap(), - ))) + Ok(SimpleValue::Num(NumKind::Integer(value))) } fn visit_u64(self, value: u64) -> Result { - // TODO: use `u64` instead of `usize` in `NumKind`. - Ok(SimpleValue::Num(NumKind::Natural( - value.try_into().unwrap(), - ))) + Ok(SimpleValue::Num(NumKind::Natural(value))) } fn visit_f64(self, value: f64) -> Result { diff --git a/serde_dhall/src/lib.rs b/serde_dhall/src/lib.rs index cf9343a..8291f74 100644 --- a/serde_dhall/src/lib.rs +++ b/serde_dhall/src/lib.rs @@ -30,7 +30,7 @@ //! let data = "{ x = 1, y = 1 + 1 } : { x: Natural, y: Natural }"; //! //! // Deserialize it to a Rust type. -//! let deserialized_map: HashMap = serde_dhall::from_str(data).parse()?; +//! let deserialized_map: HashMap = serde_dhall::from_str(data).parse()?; //! //! let mut expected_map = HashMap::new(); //! expected_map.insert("x".to_string(), 1); @@ -140,7 +140,7 @@ //! // the data matches the provided type. //! let deserialized_map = serde_dhall::from_str(point_data) //! .type_annotation(&point_type) -//! .parse::>()?; +//! .parse::>()?; //! //! let mut expected_map = HashMap::new(); //! expected_map.insert("x".to_string(), 1); diff --git a/serde_dhall/src/options.rs b/serde_dhall/src/options.rs index 76b1c5b..7b27114 100644 --- a/serde_dhall/src/options.rs +++ b/serde_dhall/src/options.rs @@ -76,7 +76,7 @@ impl HasAnnot for T { /// let ty = from_str("{ x: Natural, y: Natural }").parse()?; /// let data = from_file("foo.dhall") /// .type_annotation(&ty) -/// .parse::>()?; +/// .parse::>()?; /// # Ok(()) /// # } /// ``` @@ -130,7 +130,7 @@ impl<'a> Deserializer<'a, NoAnnot> { /// let data = "{ x = 1, y = 1 + 1 }"; /// let point = from_str(data) /// .type_annotation(&ty) - /// .parse::>()?; + /// .parse::>()?; /// assert_eq!(point.get("y"), Some(&2)); /// /// // Invalid data fails the type validation; deserialization would have succeeded otherwise. @@ -138,7 +138,7 @@ impl<'a> Deserializer<'a, NoAnnot> { /// assert!( /// from_str(invalid_data) /// .type_annotation(&ty) - /// .parse::>() + /// .parse::>() /// .is_err() /// ); /// # Ok(()) -- cgit v1.2.3