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 --- CHANGELOG.md | 2 ++ README.md | 2 +- dhall/src/builtins.rs | 6 +++--- dhall/src/syntax/ast/expr.rs | 5 ++--- dhall/src/syntax/text/parser.rs | 6 +++--- serde_dhall/src/deserialize.rs | 32 ++++---------------------------- serde_dhall/src/lib.rs | 4 ++-- serde_dhall/src/options.rs | 6 +++--- serde_dhall/tests/de.rs | 12 +++++------- 9 files changed, 25 insertions(+), 50 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f27bd74..5329eca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ #### [Unreleased] +- BREAKING CHANGE: use u64/i64 instead of usize/isize in `NumKind` + #### [0.7.5] - 2020-10-28 - Make `SimpleValue` deserializable within other types (https://github.com/Nadrieril/dhall-rust/issues/184) diff --git a/README.md b/README.md index cbda836..dea7eec 100644 --- a/README.md +++ b/README.md @@ -58,7 +58,7 @@ use std::collections::BTreeMap; let data = "{ x = 1, y = 1 + 1 } : { x: Natural, y: Natural }"; // Deserialize it to a Rust type. -let deserialized_map: BTreeMap = serde_dhall::from_str(data).parse().unwrap(); +let deserialized_map: BTreeMap = serde_dhall::from_str(data).parse().unwrap(); let mut expected_map = BTreeMap::new(); expected_map.insert("x".to_string(), 1); diff --git a/dhall/src/builtins.rs b/dhall/src/builtins.rs index e80bf6b..41a9f75 100644 --- a/dhall/src/builtins.rs +++ b/dhall/src/builtins.rs @@ -348,7 +348,7 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { _ => Ret::DoneAsIs, }, (Builtin::NaturalToInteger, [n]) => match &*n.kind() { - Num(Natural(n)) => Ret::NirKind(Num(Integer(*n as isize))), + Num(Natural(n)) => Ret::NirKind(Num(Integer(*n as i64))), _ => Ret::DoneAsIs, }, (Builtin::NaturalShow, [n]) => match &*n.kind() { @@ -449,7 +449,7 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { } (Builtin::ListLength, [_, l]) => match &*l.kind() { EmptyListLit(_) => Ret::NirKind(Num(Natural(0))), - NEListLit(xs) => Ret::NirKind(Num(Natural(xs.len()))), + NEListLit(xs) => Ret::NirKind(Num(Natural(xs.len() as u64))), _ => Ret::DoneAsIs, }, (Builtin::ListHead, [_, l]) => match &*l.kind() { @@ -495,7 +495,7 @@ fn apply_builtin(b: Builtin, args: Vec, env: NzEnv) -> NirKind { let mut kvs = HashMap::new(); kvs.insert( "index".into(), - Nir::from_kind(Num(Natural(i))), + Nir::from_kind(Num(Natural(i as u64))), ); kvs.insert("value".into(), e.clone()); Nir::from_kind(RecordLit(kvs)) diff --git a/dhall/src/syntax/ast/expr.rs b/dhall/src/syntax/ast/expr.rs index d9badb9..b1a978f 100644 --- a/dhall/src/syntax/ast/expr.rs +++ b/dhall/src/syntax/ast/expr.rs @@ -7,9 +7,8 @@ use crate::semantics::Universe; use crate::syntax::visitor; use crate::syntax::*; -// TODO: `usize` was a mistake. Should use `u64`. -pub type Integer = isize; -pub type Natural = usize; +pub type Integer = i64; +pub type Natural = u64; pub type Double = NaiveDouble; /// Double with bitwise equality diff --git a/dhall/src/syntax/text/parser.rs b/dhall/src/syntax/text/parser.rs index 377f5e4..37f28e5 100644 --- a/dhall/src/syntax/text/parser.rs +++ b/dhall/src/syntax/text/parser.rs @@ -378,7 +378,7 @@ impl DhallParser { let s = input.as_str().trim(); if s.starts_with("0x") { let without_prefix = s.trim_start_matches("0x"); - usize::from_str_radix(without_prefix, 16) + u64::from_str_radix(without_prefix, 16) .map_err(|e| input.error(format!("{}", e))) } else { s.parse().map_err(|e| input.error(format!("{}", e))) @@ -391,7 +391,7 @@ impl DhallParser { if rest.starts_with("0x") { let without_prefix = sign.to_owned() + rest.trim_start_matches("0x"); - isize::from_str_radix(&without_prefix, 16) + i64::from_str_radix(&without_prefix, 16) .map_err(|e| input.error(format!("{}", e))) } else { s.parse().map_err(|e| input.error(format!("{}", e))) @@ -408,7 +408,7 @@ impl DhallParser { fn variable(input: ParseInput) -> ParseResult { Ok(match_nodes!(input.into_children(); - [label(l), natural_literal(idx)] => V(l, idx), + [label(l), natural_literal(idx)] => V(l, idx as usize), [label(l)] => V(l, 0), )) } 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(()) diff --git a/serde_dhall/tests/de.rs b/serde_dhall/tests/de.rs index 549a753..3abaad2 100644 --- a/serde_dhall/tests/de.rs +++ b/serde_dhall/tests/de.rs @@ -69,7 +69,7 @@ fn test_de_untyped() { // Test tuples on record of wrong type assert_eq!( - parse::<(u64, String, isize)>(r#"{ y = "foo", x = 1, z = +42 }"#), + parse::<(u64, String, i64)>(r#"{ y = "foo", x = 1, z = +42 }"#), (1, "foo".to_owned(), 42) ); @@ -77,11 +77,11 @@ fn test_de_untyped() { expected_map.insert("x".to_string(), 1); expected_map.insert("y".to_string(), 2); assert_eq!( - parse::>("{ x = 1, y = 2 }"), + parse::>("{ x = 1, y = 2 }"), expected_map ); assert_eq!( - parse::>("toMap { x = 1, y = 2 }"), + parse::>("toMap { x = 1, y = 2 }"), expected_map ); @@ -90,9 +90,7 @@ fn test_de_untyped() { expected_map.insert("FOO_BAR".to_string(), 2); expected_map.insert("baz-kux".to_string(), 3); assert_eq!( - parse::>( - "{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }" - ), + parse::>("{ `if` = 1, FOO_BAR = 2, baz-kux = 3 }"), expected_map ); @@ -100,7 +98,7 @@ fn test_de_untyped() { expected_map.insert("x".to_string(), 1); expected_map.insert("y".to_string(), 2); assert_eq!( - parse::>("{ x = 1, y = 2 }"), + parse::>("{ x = 1, y = 2 }"), expected_map ); -- cgit v1.2.3