diff options
author | Nadrieril Feneanar | 2019-04-06 00:43:09 +0200 |
---|---|---|
committer | GitHub | 2019-04-06 00:43:09 +0200 |
commit | 5eccde86fc3ccdeb34c9f8bb44de33d25e77f30c (patch) | |
tree | 76d0f0fc848887d2945d586b58847575ca31d0da /dhall_core/src/text.rs | |
parent | f78af6d1e7f6c1dc39bde6cf97138327004ddb06 (diff) | |
parent | 6a675a13fcfafa057c44db84c3b0ca3b344cfdab (diff) |
Merge pull request #48 from Nadrieril/exprf
Move recursion out of Expr for enhanced genericity
Diffstat (limited to 'dhall_core/src/text.rs')
-rw-r--r-- | dhall_core/src/text.rs | 56 |
1 files changed, 34 insertions, 22 deletions
diff --git a/dhall_core/src/text.rs b/dhall_core/src/text.rs index d377877..9500f32 100644 --- a/dhall_core/src/text.rs +++ b/dhall_core/src/text.rs @@ -1,18 +1,16 @@ -use crate::*; use std::iter::FromIterator; use std::ops::Add; -use std::rc::Rc; #[derive(Debug, Clone, PartialEq, Eq)] -pub struct InterpolatedText<Note, Embed> { +pub struct InterpolatedText<SubExpr> { head: String, - tail: Vec<(Rc<Expr<Note, Embed>>, String)>, + tail: Vec<(SubExpr, String)>, } -impl<N, E> From<(String, Vec<(Rc<Expr<N, E>>, String)>)> - for InterpolatedText<N, E> +impl<SubExpr> From<(String, Vec<(SubExpr, String)>)> + for InterpolatedText<SubExpr> { - fn from(x: (String, Vec<(Rc<Expr<N, E>>, String)>)) -> Self { + fn from(x: (String, Vec<(SubExpr, String)>)) -> Self { InterpolatedText { head: x.0, tail: x.1, @@ -20,7 +18,7 @@ impl<N, E> From<(String, Vec<(Rc<Expr<N, E>>, String)>)> } } -impl<N, E> From<String> for InterpolatedText<N, E> { +impl<SubExpr> From<String> for InterpolatedText<SubExpr> { fn from(s: String) -> Self { InterpolatedText { head: s, @@ -30,41 +28,55 @@ impl<N, E> From<String> for InterpolatedText<N, E> { } #[derive(Debug, Clone, PartialEq, Eq)] -pub enum InterpolatedTextContents<Note, Embed> { +pub enum InterpolatedTextContents<SubExpr> { Text(String), - Expr(SubExpr<Note, Embed>), + Expr(SubExpr), } -impl<N, E> InterpolatedText<N, E> { - pub fn map<N2, E2, F>(&self, mut f: F) -> InterpolatedText<N2, E2> +impl<SubExpr> InterpolatedText<SubExpr> { + pub fn map<SubExpr2, F>(self, mut f: F) -> InterpolatedText<SubExpr2> where - F: FnMut(&Rc<Expr<N, E>>) -> Rc<Expr<N2, E2>>, + F: FnMut(SubExpr) -> SubExpr2, { InterpolatedText { head: self.head.clone(), - tail: self.tail.iter().map(|(e, s)| (f(e), s.clone())).collect(), + tail: self + .tail + .into_iter() + .map(|(e, s)| (f(e), s.clone())) + .collect(), + } + } + + pub fn as_ref(&self) -> InterpolatedText<&SubExpr> { + InterpolatedText { + head: self.head.clone(), + tail: self.tail.iter().map(|(e, s)| (e, s.clone())).collect(), } } pub fn iter<'a>( &'a self, - ) -> impl Iterator<Item = InterpolatedTextContents<N, E>> + 'a { + ) -> impl Iterator<Item = InterpolatedTextContents<SubExpr>> + 'a + where + SubExpr: Clone, + { use std::iter::once; once(InterpolatedTextContents::Text(self.head.clone())).chain( self.tail.iter().flat_map(|(e, s)| { - once(InterpolatedTextContents::Expr(Rc::clone(e))) + once(InterpolatedTextContents::Expr(SubExpr::clone(e))) .chain(once(InterpolatedTextContents::Text(s.clone()))) }), ) } } -impl<'a, N: 'a, E: 'a> FromIterator<InterpolatedTextContents<N, E>> - for InterpolatedText<N, E> +impl<'a, SubExpr: Clone + 'a> FromIterator<InterpolatedTextContents<SubExpr>> + for InterpolatedText<SubExpr> { fn from_iter<T>(iter: T) -> Self where - T: IntoIterator<Item = InterpolatedTextContents<N, E>>, + T: IntoIterator<Item = InterpolatedTextContents<SubExpr>>, { let mut res = InterpolatedText { head: "".to_owned(), @@ -84,9 +96,9 @@ impl<'a, N: 'a, E: 'a> FromIterator<InterpolatedTextContents<N, E>> } } -impl<N, E> Add for &InterpolatedText<N, E> { - type Output = InterpolatedText<N, E>; - fn add(self, rhs: &InterpolatedText<N, E>) -> Self::Output { +impl<SubExpr: Clone> Add for &InterpolatedText<SubExpr> { + type Output = InterpolatedText<SubExpr>; + fn add(self, rhs: &InterpolatedText<SubExpr>) -> Self::Output { self.iter().chain(rhs.iter()).collect() } } |