diff options
author | Nadrieril | 2019-05-10 19:49:38 +0200 |
---|---|---|
committer | Nadrieril | 2019-05-10 19:49:38 +0200 |
commit | a8e696f62f14296b94964adb1946d7e2b5ef5ebd (patch) | |
tree | 8ac9c17c4e0cf7470b047c46ac7042d1db1bb1e9 /dhall_syntax/src/core/visitor.rs | |
parent | 36bcec6c91d3192b5c84c96af96961ff6b79f0f0 (diff) |
Write a custom map type that allows duplicates
Diffstat (limited to 'dhall_syntax/src/core/visitor.rs')
-rw-r--r-- | dhall_syntax/src/core/visitor.rs | 29 |
1 files changed, 17 insertions, 12 deletions
diff --git a/dhall_syntax/src/core/visitor.rs b/dhall_syntax/src/core/visitor.rs index dd539e3..99a9c11 100644 --- a/dhall_syntax/src/core/visitor.rs +++ b/dhall_syntax/src/core/visitor.rs @@ -1,4 +1,5 @@ use crate::*; +use std::iter::FromIterator; /// A way too generic Visitor trait. pub trait GenericVisitor<Input, Return>: Sized { @@ -62,25 +63,29 @@ where None => None, }) } - fn vecmap<'a, V, Ret, SE, E>( - x: &'a Vec<(Label, SE)>, + fn dupmap<'a, V, Ret, SE, E, T>( + x: impl IntoIterator<Item = (&'a Label, &'a SE)>, mut v: V, - ) -> Result<Vec<(Label, V::SE2)>, V::Error> + ) -> Result<T, V::Error> where + SE: 'a, + T: FromIterator<(Label, V::SE2)>, V: ExprFVeryGenericVisitor<'a, Ret, SE, E>, { - x.iter() + x.into_iter() .map(|(k, x)| Ok((k.clone(), v.visit_subexpr(x)?))) .collect() } - fn vecoptmap<'a, V, Ret, SE, E>( - x: &'a Vec<(Label, Option<SE>)>, + fn optdupmap<'a, V, Ret, SE, E, T>( + x: impl IntoIterator<Item = (&'a Label, &'a Option<SE>)>, mut v: V, - ) -> Result<Vec<(Label, Option<V::SE2>)>, V::Error> + ) -> Result<T, V::Error> where + SE: 'a, + T: FromIterator<(Label, Option<V::SE2>)>, V: ExprFVeryGenericVisitor<'a, Ret, SE, E>, { - x.iter() + x.into_iter() .map(|(k, x)| { Ok(( k.clone(), @@ -137,11 +142,11 @@ where v.visit_subexpr(t)?, ), SomeLit(e) => SomeLit(v.visit_subexpr(e)?), - RecordType(kts) => RecordType(vecmap(kts, v)?), - RecordLit(kvs) => RecordLit(vecmap(kvs, v)?), - UnionType(kts) => UnionType(vecoptmap(kts, v)?), + RecordType(kts) => RecordType(dupmap(kts, v)?), + RecordLit(kvs) => RecordLit(dupmap(kvs, v)?), + UnionType(kts) => UnionType(optdupmap(kts, v)?), UnionLit(k, x, kts) => { - UnionLit(k.clone(), v.visit_subexpr(x)?, vecoptmap(kts, v)?) + UnionLit(k.clone(), v.visit_subexpr(x)?, optdupmap(kts, v)?) } Merge(x, y, t) => Merge( v.visit_subexpr(x)?, |