Skip to content

Commit

Permalink
Get rid of the 'a lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
yannham committed Oct 25, 2024
1 parent 545d4d2 commit deb58c3
Showing 1 changed file with 35 additions and 35 deletions.
70 changes: 35 additions & 35 deletions core/src/bytecode/ast/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,21 @@ use crate::term;
/// - `'a`: the lifetime of the reference to the mainline Nickel object, which doesn't need to be
/// related to `'ast` (we will copy any required data into the allocator)
/// - `T`: the type of the mainline Nickel object ([term::Term], [term::pattern::Pattern], etc.)
pub trait FromMainline<'ast, 'a, T> {
fn from_mainline(alloc: &'ast AstAlloc, mainline: &'a T) -> Self;
pub trait FromMainline<'ast, T> {
fn from_mainline(alloc: &'ast AstAlloc, mainline: &T) -> Self;
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::Pattern> for &'ast Pattern<'ast> {
impl<'ast> FromMainline<'ast, term::pattern::Pattern> for &'ast Pattern<'ast> {
fn from_mainline(
alloc: &'ast AstAlloc,
pattern: &'a term::pattern::Pattern,
pattern: &term::pattern::Pattern,
) -> &'ast Pattern<'ast> {
alloc.move_pattern(pattern.to_ast(alloc))
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::Pattern> for Pattern<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, pattern: &'a term::pattern::Pattern) -> Self {
impl<'ast> FromMainline<'ast, term::pattern::Pattern> for Pattern<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, pattern: &term::pattern::Pattern) -> Self {
Pattern {
data: pattern.data.to_ast(alloc),
alias: pattern.alias,
Expand All @@ -39,8 +39,8 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::Pattern> for Pattern<'ast>
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::PatternData> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, data: &'a term::pattern::PatternData) -> Self {
impl<'ast> FromMainline<'ast, term::pattern::PatternData> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, data: &term::pattern::PatternData) -> Self {
match data {
term::pattern::PatternData::Wildcard => PatternData::Wildcard,
term::pattern::PatternData::Any(id) => PatternData::Any(*id),
Expand All @@ -55,8 +55,8 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::PatternData> for PatternDat
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::RecordPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, record_pat: &'a term::pattern::RecordPattern) -> Self {
impl<'ast> FromMainline<'ast, term::pattern::RecordPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, record_pat: &term::pattern::RecordPattern) -> Self {
let patterns = record_pat
.patterns
.iter()
Expand All @@ -72,8 +72,8 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::RecordPattern> for PatternD
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::FieldPattern> for FieldPattern<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, field_pat: &'a term::pattern::FieldPattern) -> Self {
impl<'ast> FromMainline<'ast, term::pattern::FieldPattern> for FieldPattern<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, field_pat: &term::pattern::FieldPattern) -> Self {
let pattern = field_pat.pattern.to_ast(alloc);

let default = field_pat.default.as_ref().map(|term| term.to_ast(alloc));
Expand All @@ -90,8 +90,8 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::FieldPattern> for FieldPatt
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::ArrayPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, array_pat: &'a term::pattern::ArrayPattern) -> Self {
impl<'ast> FromMainline<'ast, term::pattern::ArrayPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, array_pat: &term::pattern::ArrayPattern) -> Self {
let patterns = array_pat.patterns.iter().map(|pat| pat.to_ast(alloc));

let tail = match array_pat.tail {
Expand All @@ -104,15 +104,15 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::ArrayPattern> for PatternDa
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::EnumPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, enum_pat: &'a term::pattern::EnumPattern) -> Self {
impl<'ast> FromMainline<'ast, term::pattern::EnumPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, enum_pat: &term::pattern::EnumPattern) -> Self {
let pattern = enum_pat.pattern.as_ref().map(|pat| (**pat).to_ast(alloc));
PatternData::Enum(alloc.enum_pattern(enum_pat.tag, pattern, enum_pat.pos))
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::ConstantPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, pattern: &'a term::pattern::ConstantPattern) -> Self {
impl<'ast> FromMainline<'ast, term::pattern::ConstantPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, pattern: &term::pattern::ConstantPattern) -> Self {
let data = match &pattern.data {
term::pattern::ConstantPatternData::Bool(b) => ConstantPatternData::Bool(*b),
term::pattern::ConstantPatternData::Number(n) => {
Expand All @@ -128,8 +128,8 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::ConstantPattern> for Patter
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::OrPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, pattern: &'a term::pattern::OrPattern) -> Self {
impl<'ast> FromMainline<'ast, term::pattern::OrPattern> for PatternData<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, pattern: &term::pattern::OrPattern) -> Self {
let patterns = pattern
.patterns
.iter()
Expand All @@ -140,8 +140,8 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::pattern::OrPattern> for PatternData<
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::TypeAnnotation> for Annotation<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, annot: &'a term::TypeAnnotation) -> Self {
impl<'ast> FromMainline<'ast, term::TypeAnnotation> for Annotation<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, annot: &term::TypeAnnotation) -> Self {
let typ = annot.typ.as_ref().map(|typ| typ.typ.clone());

let contracts = alloc
Expand All @@ -152,17 +152,17 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::TypeAnnotation> for Annotation<'ast>
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::record::Field> for record::Field<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, field: &'a term::record::Field) -> Self {
impl<'ast> FromMainline<'ast, term::record::Field> for record::Field<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, field: &term::record::Field) -> Self {
record::Field {
value: field.value.as_ref().map(|term| term.to_ast(alloc)),
metadata: field.metadata.to_ast(alloc),
}
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::record::FieldMetadata> for record::FieldMetadata<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, metadata: &'a term::record::FieldMetadata) -> Self {
impl<'ast> FromMainline<'ast, term::record::FieldMetadata> for record::FieldMetadata<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, metadata: &term::record::FieldMetadata) -> Self {
let doc = metadata.doc.as_ref().map(|doc| rc::Rc::from(doc.as_str()));

record::FieldMetadata {
Expand All @@ -175,8 +175,8 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::record::FieldMetadata> for record::F
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::Term> for &'ast Node<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, term: &'a term::Term) -> Self {
impl<'ast> FromMainline<'ast, term::Term> for &'ast Node<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, term: &term::Term) -> Self {
use term::Term;

match term {
Expand Down Expand Up @@ -357,22 +357,22 @@ impl<'ast, 'a> FromMainline<'ast, 'a, term::Term> for &'ast Node<'ast> {
}
}

impl<'ast, 'a> FromMainline<'ast, 'a, term::RichTerm> for Ast<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, rterm: &'a term::RichTerm) -> Self {
impl<'ast> FromMainline<'ast, term::RichTerm> for Ast<'ast> {
fn from_mainline(alloc: &'ast AstAlloc, rterm: &term::RichTerm) -> Self {
alloc.ast(rterm.as_ref().to_ast(alloc), rterm.pos)
}
}

/// Symmetric to `FromMainline`, as `Into` is to `From`.
pub trait ToAst<'ast, 'a, T> {
fn to_ast(&'a self, alloc: &'ast AstAlloc) -> T;
pub trait ToAst<'ast, T> {
fn to_ast(&self, alloc: &'ast AstAlloc) -> T;
}

impl<'ast, 'a, S, T> ToAst<'ast, 'a, T> for S
impl<'ast, S, T> ToAst<'ast, T> for S
where
T: FromMainline<'ast, 'a, S>,
T: FromMainline<'ast, S>,
{
fn to_ast(&'a self, alloc: &'ast AstAlloc) -> T {
fn to_ast(&self, alloc: &'ast AstAlloc) -> T {
T::from_mainline(alloc, self)
}
}
Expand Down

0 comments on commit deb58c3

Please sign in to comment.