From d17d553a39aa3bffdfc19b7fe4801b85d6bd80f7 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 11 Apr 2019 16:41:06 +0200 Subject: Add lifetime parameters to Parsed and Resolved Future-proofing --- dhall/src/traits/deserialize.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'dhall/src/traits') diff --git a/dhall/src/traits/deserialize.rs b/dhall/src/traits/deserialize.rs index 1fbdfe1..3e8cc5e 100644 --- a/dhall/src/traits/deserialize.rs +++ b/dhall/src/traits/deserialize.rs @@ -5,18 +5,18 @@ pub trait Deserialize<'a>: Sized { fn from_str(s: &'a str, ty: Option<&Type>) -> Result; } -impl<'a> Deserialize<'a> for Parsed { +impl<'de: 'a, 'a> Deserialize<'de> for Parsed<'a> { /// Simply parses the provided string. Ignores the /// provided type. - fn from_str(s: &'a str, _: Option<&Type>) -> Result { + fn from_str(s: &'de str, _: Option<&Type>) -> Result { Ok(Parsed::parse_str(s)?) } } -impl<'a> Deserialize<'a> for Resolved { +impl<'de: 'a, 'a> Deserialize<'de> for Resolved<'a> { /// Parses and resolves the provided string. Ignores the /// provided type. - fn from_str(s: &'a str, ty: Option<&Type>) -> Result { + fn from_str(s: &'de str, ty: Option<&Type>) -> Result { Ok(Parsed::from_str(s, ty)?.resolve()?) } } -- cgit v1.2.3 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/traits/deserialize.rs | 12 +++++------ dhall/src/traits/dynamic_type.rs | 16 +++++++------- dhall/src/traits/static_type.rs | 45 ++++++++++++++++++++-------------------- 3 files changed, 37 insertions(+), 36 deletions(-) (limited to 'dhall/src/traits') diff --git a/dhall/src/traits/deserialize.rs b/dhall/src/traits/deserialize.rs index 3e8cc5e..f1be054 100644 --- a/dhall/src/traits/deserialize.rs +++ b/dhall/src/traits/deserialize.rs @@ -21,9 +21,9 @@ impl<'de: 'a, 'a> Deserialize<'de> for Resolved<'a> { } } -impl<'a> Deserialize<'a> for Typed { +impl<'de: 'a, 'a> Deserialize<'de> for Typed<'a> { /// Parses, resolves and typechecks the provided string. - fn from_str(s: &'a str, ty: Option<&Type>) -> Result { + fn from_str(s: &'de str, ty: Option<&Type>) -> Result { let resolved = Resolved::from_str(s, ty)?; match ty { None => Ok(resolved.typecheck()?), @@ -32,15 +32,15 @@ impl<'a> Deserialize<'a> for Typed { } } -impl<'a> Deserialize<'a> for Normalized { +impl<'de: 'a, 'a> Deserialize<'de> for Normalized<'a> { /// Parses, resolves, typechecks and normalizes the provided string. - fn from_str(s: &'a str, ty: Option<&Type>) -> Result { + fn from_str(s: &'de str, ty: Option<&Type>) -> Result { Ok(Typed::from_str(s, ty)?.normalize()) } } -impl<'a> Deserialize<'a> for Type { - fn from_str(s: &'a str, ty: Option<&Type>) -> Result { +impl<'de: 'a, 'a> Deserialize<'de> for Type<'a> { + fn from_str(s: &'de str, ty: Option<&Type>) -> Result { Ok(Normalized::from_str(s, ty)?.into_type()) } } diff --git a/dhall/src/traits/dynamic_type.rs b/dhall/src/traits/dynamic_type.rs index d03f8cd..28ed76d 100644 --- a/dhall/src/traits/dynamic_type.rs +++ b/dhall/src/traits/dynamic_type.rs @@ -6,17 +6,17 @@ use dhall_core::{Const, ExprF}; use std::borrow::Cow; pub trait DynamicType { - fn get_type<'a>(&'a self) -> Result, TypeError>; + fn get_type<'a>(&'a self) -> Result>, TypeError>; } impl DynamicType for T { - fn get_type<'a>(&'a self) -> Result, TypeError> { + fn get_type<'a>(&'a self) -> Result>, TypeError> { Ok(Cow::Owned(T::get_static_type())) } } -impl DynamicType for Type { - fn get_type(&self) -> Result, TypeError> { +impl<'a> DynamicType for Type<'a> { + fn get_type(&self) -> Result>, TypeError> { use TypeInternal::*; match &self.0 { Expr(e) => e.get_type(), @@ -29,8 +29,8 @@ impl DynamicType for Type { } } -impl DynamicType for Normalized { - fn get_type(&self) -> Result, TypeError> { +impl<'a> DynamicType for Normalized<'a> { + fn get_type(&self) -> Result>, TypeError> { match &self.1 { Some(t) => Ok(Cow::Borrowed(t)), None => Err(TypeError::new( @@ -42,8 +42,8 @@ impl DynamicType for Normalized { } } -impl DynamicType for Typed { - fn get_type(&self) -> Result, TypeError> { +impl<'a> DynamicType for Typed<'a> { + fn get_type(&self) -> Result>, TypeError> { match &self.1 { Some(t) => Ok(Cow::Borrowed(t)), None => Err(TypeError::new( diff --git a/dhall/src/traits/static_type.rs b/dhall/src/traits/static_type.rs index 6b0f5c5..35d91e1 100644 --- a/dhall/src/traits/static_type.rs +++ b/dhall/src/traits/static_type.rs @@ -3,7 +3,7 @@ use dhall_core::*; use dhall_generator::*; pub trait StaticType { - fn get_static_type() -> Type; + fn get_static_type() -> Type<'static>; } /// Trait for rust types that can be represented in dhall in @@ -12,79 +12,80 @@ pub trait StaticType { /// is `HashMap` because dhall cannot represent records with a /// variable number of fields. pub trait SimpleStaticType { - fn get_simple_static_type() -> SimpleType; + fn get_simple_static_type<'a>() -> SimpleType<'a>; } -fn mktype(x: SubExpr) -> SimpleType { - SimpleType(x) +fn mktype<'a>(x: SubExpr) -> SimpleType<'a> { + SimpleType(x, std::marker::PhantomData) } impl StaticType for T { - fn get_static_type() -> Type { + fn get_static_type() -> Type<'static> { crate::expr::Normalized( T::get_simple_static_type().into(), Some(Type::const_type()), + std::marker::PhantomData, ) .into_type() } } -impl StaticType for SimpleType { - fn get_static_type() -> Type { +impl<'a> StaticType for SimpleType<'a> { + fn get_static_type() -> Type<'static> { Type::const_type() } } impl SimpleStaticType for bool { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { mktype(dhall_expr!(Bool)) } } impl SimpleStaticType for Natural { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { mktype(dhall_expr!(Natural)) } } impl SimpleStaticType for u32 { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { mktype(dhall_expr!(Natural)) } } impl SimpleStaticType for u64 { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { mktype(dhall_expr!(Natural)) } } impl SimpleStaticType for Integer { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { mktype(dhall_expr!(Integer)) } } impl SimpleStaticType for i32 { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { mktype(dhall_expr!(Integer)) } } impl SimpleStaticType for i64 { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { mktype(dhall_expr!(Integer)) } } impl SimpleStaticType for String { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { mktype(dhall_expr!(Text)) } } impl SimpleStaticType for (A, B) { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { let ta: SubExpr<_, _> = A::get_simple_static_type().into(); let tb: SubExpr<_, _> = B::get_simple_static_type().into(); mktype(dhall_expr!({ _1: ta, _2: tb })) @@ -92,27 +93,27 @@ impl SimpleStaticType for (A, B) { } impl SimpleStaticType for Option { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { let t: SubExpr<_, _> = T::get_simple_static_type().into(); mktype(dhall_expr!(Optional t)) } } impl SimpleStaticType for Vec { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { let t: SubExpr<_, _> = T::get_simple_static_type().into(); mktype(dhall_expr!(List t)) } } -impl<'a, T: SimpleStaticType> SimpleStaticType for &'a T { - fn get_simple_static_type() -> SimpleType { +impl<'b, T: SimpleStaticType> SimpleStaticType for &'b T { + fn get_simple_static_type<'a>() -> SimpleType<'a> { T::get_simple_static_type() } } impl SimpleStaticType for std::marker::PhantomData { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { mktype(dhall_expr!({})) } } @@ -120,7 +121,7 @@ impl SimpleStaticType for std::marker::PhantomData { impl SimpleStaticType for std::result::Result { - fn get_simple_static_type() -> SimpleType { + fn get_simple_static_type<'a>() -> SimpleType<'a> { let tt: SubExpr<_, _> = T::get_simple_static_type().into(); let te: SubExpr<_, _> = E::get_simple_static_type().into(); mktype(dhall_expr!(< Ok: tt | Err: te>)) -- cgit v1.2.3