From 9474d4939db6f844285182fc15aad74f6aa18e21 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 18 Apr 2019 14:39:56 +0200 Subject: Remove more duplication --- dhall_core/src/core.rs | 66 ++++++++++++++++++++++++----------------------- dhall_core/src/visitor.rs | 30 --------------------- 2 files changed, 34 insertions(+), 62 deletions(-) (limited to 'dhall_core') diff --git a/dhall_core/src/core.rs b/dhall_core/src/core.rs index 158abe1..65762f2 100644 --- a/dhall_core/src/core.rs +++ b/dhall_core/src/core.rs @@ -205,14 +205,14 @@ pub enum ExprF { } impl ExprF { - pub fn visit<'a, V, Return>(&'a self, v: V) -> Return + pub(crate) fn visit<'a, V, Return>(&'a self, v: V) -> Return where V: visitor::GenericVisitor<&'a ExprF, Return>, { v.visit(self) } - pub fn traverse_ref_with_special_handling_of_binders< + fn traverse_ref_with_special_handling_of_binders< 'a, SE2, L2, @@ -250,7 +250,7 @@ impl ExprF { }) } - pub fn traverse_ref<'a, SE2, L2, N2, E2, Err, F1, F3, F4, F5>( + fn traverse_ref<'a, SE2, L2, N2, E2, Err, F1, F3, F4, F5>( &'a self, visit_subexpr: F1, visit_note: F3, @@ -343,7 +343,12 @@ impl ExprF { E: Clone, F1: FnMut(&'a SE) -> Result, { - self.visit(visitor::TraverseRefSimpleVisitor { visit_subexpr }) + self.traverse_ref( + visit_subexpr, + |x| Ok(N::clone(x)), + |x| Ok(E::clone(x)), + |x| Ok(L::clone(x)), + ) } pub fn map_ref_simple<'a, SE2, F1>( @@ -351,49 +356,35 @@ impl ExprF { map_subexpr: F1, ) -> ExprF where - L: Ord, - L: Clone, + L: Ord + Clone, N: Clone, E: Clone, F1: Fn(&'a SE) -> SE2, { - trivial_result(self.traverse_ref_simple(|x| Ok(map_subexpr(x)))) + self.map_ref(map_subexpr, N::clone, E::clone, L::clone) } } impl Expr { - pub fn traverse_embed( + fn traverse_embed( &self, - map_embed: F, + visit_embed: F, ) -> Result, Err> where N: Clone, F: FnMut(&E) -> Result, { - self.visit(&mut visitor::TraverseEmbedVisitor(map_embed)) + self.visit(&mut visitor::TraverseEmbedVisitor(visit_embed)) } - pub fn map_embed(&self, mut map_embed: F) -> Expr + fn map_embed(&self, mut map_embed: F) -> Expr where - E: Clone, N: Clone, F: FnMut(&E) -> E2, { trivial_result(self.traverse_embed(|x| Ok(map_embed(x)))) } - pub fn map_label(&self, map_label: &F) -> Self - where - E: Clone, - N: Clone, - F: Fn(&Label) -> Label, - { - let recurse = |e: &SubExpr| -> SubExpr { - rc(e.as_ref().map_label(map_label)) - }; - self.map_ref(recurse, N::clone, E::clone, map_label) - } - pub fn roll(&self) -> SubExpr where N: Clone, @@ -420,10 +411,6 @@ impl Expr { } impl Expr { - // Deprecated, use embed_absurd instead - pub fn absurd_rec(&self) -> Expr { - self.embed_absurd() - } pub fn embed_absurd(&self) -> Expr { self.visit(&mut visitor::EmbedAbsurdVisitor) } @@ -434,6 +421,25 @@ impl SubExpr { self.0.as_ref() } + pub fn traverse_embed( + &self, + visit_embed: F, + ) -> Result, Err> + where + N: Clone, + F: FnMut(&E) -> Result, + { + Ok(rc(self.as_ref().traverse_embed(visit_embed)?)) + } + + pub fn map_embed(&self, map_embed: F) -> SubExpr + where + N: Clone, + F: FnMut(&E) -> E2, + { + rc(self.as_ref().map_embed(map_embed)) + } + pub fn map_subexprs_with_special_handling_of_binders<'a, F1, F2>( &'a self, map_expr: F1, @@ -480,10 +486,6 @@ impl SubExpr { } impl SubExpr { - // Deprecated, use embed_absurd instead - pub fn absurd(&self) -> SubExpr { - self.embed_absurd() - } pub fn embed_absurd(&self) -> SubExpr { rc(self.as_ref().embed_absurd()) } diff --git a/dhall_core/src/visitor.rs b/dhall_core/src/visitor.rs index c2b0d06..16ad418 100644 --- a/dhall_core/src/visitor.rs +++ b/dhall_core/src/visitor.rs @@ -472,36 +472,6 @@ where } } -pub struct TraverseRefSimpleVisitor { - pub visit_subexpr: F1, -} - -impl<'a, SE, L, N, E, SE2, Err, F1> - ExprFFallibleVisitor<'a, SE, SE2, L, L, N, N, E, E> - for TraverseRefSimpleVisitor -where - SE: 'a, - L: Ord + Clone + 'a, - N: Clone + 'a, - E: Clone + 'a, - F1: FnMut(&'a SE) -> Result, -{ - type Error = Err; - - fn visit_subexpr(&mut self, subexpr: &'a SE) -> Result { - (self.visit_subexpr)(subexpr) - } - fn visit_note(self, note: &'a N) -> Result { - Ok(N::clone(note)) - } - fn visit_embed(self, embed: &'a E) -> Result { - Ok(E::clone(embed)) - } - fn visit_label(&mut self, label: &'a L) -> Result { - Ok(L::clone(label)) - } -} - pub struct TraverseEmbedVisitor(pub F1); impl<'a, 'b, N, E, E2, Err, F1> -- cgit v1.2.3