From d9a2a77a19e56edd8eb96eba002e39bc7be3bde3 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 11 Apr 2019 20:45:43 +0200 Subject: Thread lifetimes through other newtypes Closes #55 --- dhall/src/expr.rs | 80 +++++++++++++++++++++++++------------------------------ 1 file changed, 36 insertions(+), 44 deletions(-) (limited to 'dhall/src/expr.rs') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index e3a2fe5..3987896 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -3,25 +3,6 @@ use dhall_core::*; use std::marker::PhantomData; macro_rules! derive_other_traits { - ($ty:ident) => { - impl std::cmp::PartialEq for $ty { - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } - } - - impl std::fmt::Display for $ty { - fn fmt( - &self, - f: &mut std::fmt::Formatter, - ) -> Result<(), std::fmt::Error> { - self.0.fmt(f) - } - } - }; -} - -macro_rules! derive_other_traits_ { ($ty:ident) => { impl<'a> std::cmp::PartialEq for $ty<'a> { fn eq(&self, other: &Self) -> bool { @@ -46,85 +27,96 @@ pub struct Parsed<'a>( pub(crate) ImportRoot, pub(crate) PhantomData<&'a ()>, ); -derive_other_traits_!(Parsed); +derive_other_traits!(Parsed); #[derive(Debug, Clone, Eq)] pub struct Resolved<'a>( - pub(crate) SubExpr, + pub(crate) SubExpr>, pub(crate) PhantomData<&'a ()>, ); -derive_other_traits_!(Resolved); +derive_other_traits!(Resolved); #[derive(Debug, Clone, Eq)] -pub struct Typed(pub(crate) SubExpr, pub(crate) Option); +pub struct Typed<'a>( + pub(crate) SubExpr>, + pub(crate) Option>, + pub(crate) PhantomData<&'a ()>, +); derive_other_traits!(Typed); #[derive(Debug, Clone, Eq)] -pub struct Normalized(pub(crate) SubExpr, pub(crate) Option); +pub struct Normalized<'a>( + pub(crate) SubExpr, + pub(crate) Option>, + pub(crate) PhantomData<&'a ()>, +); derive_other_traits!(Normalized); /// An expression of type `Type` (like `Bool` or `Natural -> Text`, but not `Type`) #[derive(Debug, Clone, Eq)] -pub struct SimpleType(pub(crate) SubExpr); +pub struct SimpleType<'a>( + pub(crate) SubExpr, + pub(crate) PhantomData<&'a ()>, +); derive_other_traits!(SimpleType); #[derive(Debug, Clone, PartialEq, Eq)] -pub struct Type(pub(crate) TypeInternal); +pub struct Type<'a>(pub(crate) TypeInternal<'a>); #[derive(Debug, Clone, PartialEq, Eq)] -pub(crate) enum TypeInternal { - Expr(Box), +pub(crate) enum TypeInternal<'a> { + Expr(Box>), // The type of `Sort` SuperType, } // Exposed for the macros #[doc(hidden)] -impl From for SubExpr { - fn from(x: SimpleType) -> SubExpr { +impl<'a> From> for SubExpr { + fn from(x: SimpleType<'a>) -> SubExpr { x.0 } } // Exposed for the macros #[doc(hidden)] -impl From> for SimpleType { - fn from(x: SubExpr) -> SimpleType { - SimpleType(x) +impl<'a> From> for SimpleType<'a> { + fn from(x: SubExpr) -> SimpleType<'a> { + SimpleType(x, PhantomData) } } // Exposed for the macros #[doc(hidden)] -impl From for Typed { - fn from(x: Normalized) -> Typed { - Typed(x.0.absurd(), x.1) +impl<'a> From> for Typed<'a> { + fn from(x: Normalized<'a>) -> Typed<'a> { + Typed(x.0.absurd(), x.1, x.2) } } -impl Typed { - pub(crate) fn as_expr(&self) -> &SubExpr { +impl<'a> Typed<'a> { + pub(crate) fn as_expr(&self) -> &SubExpr> { &self.0 } - pub(crate) fn into_expr(self) -> SubExpr { + pub(crate) fn into_expr(self) -> SubExpr> { self.0 } } -impl Normalized { +impl<'a> Normalized<'a> { pub(crate) fn as_expr(&self) -> &SubExpr { &self.0 } pub(crate) fn into_expr(self) -> SubExpr { self.0.absurd() } - pub(crate) fn into_type(self) -> Type { + pub(crate) fn into_type(self) -> Type<'a> { crate::expr::Type(TypeInternal::Expr(Box::new(self))) } } -impl SimpleType { - pub(crate) fn into_type(self) -> Type { - Normalized(self.0, Some(Type::const_type())).into_type() +impl<'a> SimpleType<'a> { + pub(crate) fn into_type(self) -> Type<'a> { + Normalized(self.0, Some(Type::const_type()), PhantomData).into_type() } } -- cgit v1.2.3