Skip to content

Commit

Permalink
Associated types support for deriving::generic::TraitDef
Browse files Browse the repository at this point in the history
  • Loading branch information
kvark committed Jan 25, 2015
1 parent 4be79d6 commit e563215
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/bounds.rs
Expand Up @@ -51,7 +51,8 @@ pub fn expand_deriving_bound<F>(cx: &mut ExtCtxt,
path: Path::new(vec!("std", "marker", name)),
additional_bounds: Vec::new(),
generics: LifetimeBounds::empty(),
methods: vec!()
methods: Vec::new(),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/clone.rs
Expand Up @@ -44,7 +44,8 @@ pub fn expand_deriving_clone<F>(cx: &mut ExtCtxt,
cs_clone("Clone", c, s, sub)
}),
}
)
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/cmp/eq.rs
Expand Up @@ -88,7 +88,8 @@ pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
methods: vec!(
md!("eq", cs_eq),
md!("ne", cs_ne)
)
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/cmp/ord.rs
Expand Up @@ -78,7 +78,8 @@ pub fn expand_deriving_ord<F>(cx: &mut ExtCtxt,
md!("le", true, true),
md!("gt", false, false),
md!("ge", false, true)
]
],
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/cmp/totaleq.rs
Expand Up @@ -61,7 +61,8 @@ pub fn expand_deriving_totaleq<F>(cx: &mut ExtCtxt,
cs_total_eq_assert(a, b, c)
})
}
)
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/cmp/totalord.rs
Expand Up @@ -45,7 +45,8 @@ pub fn expand_deriving_totalord<F>(cx: &mut ExtCtxt,
cs_cmp(a, b, c)
}),
}
)
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/ext/deriving/decodable.rs
Expand Up @@ -79,7 +79,9 @@ fn expand_deriving_decodable_imp<F>(cx: &mut ExtCtxt,
combine_substructure: combine_substructure(box |a, b, c| {
decodable_substructure(a, b, c, krate)
}),
})
}
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/ext/deriving/default.rs
Expand Up @@ -43,7 +43,9 @@ pub fn expand_deriving_default<F>(cx: &mut ExtCtxt,
combine_substructure: combine_substructure(box |a, b, c| {
default_substructure(a, b, c)
})
})
}
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/ext/deriving/encodable.rs
Expand Up @@ -155,7 +155,9 @@ fn expand_deriving_encodable_imp<F>(cx: &mut ExtCtxt,
combine_substructure: combine_substructure(box |a, b, c| {
encodable_substructure(a, b, c)
}),
})
}
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
24 changes: 23 additions & 1 deletion src/libsyntax/ext/deriving/generic/mod.rs
Expand Up @@ -228,6 +228,8 @@ pub struct TraitDef<'a> {
pub generics: LifetimeBounds<'a>,

pub methods: Vec<MethodDef<'a>>,

pub associated_types: Vec<(ast::Ident, Ty<'a>)>,
}


Expand Down Expand Up @@ -387,6 +389,22 @@ impl<'a> TraitDef<'a> {
methods: Vec<P<ast::Method>>) -> P<ast::Item> {
let trait_path = self.path.to_path(cx, self.span, type_ident, generics);

// Transform associated types from `deriving::ty::Ty` into `ast::Typedef`
let associated_types = self.associated_types.iter().map(|&(ident, ref type_def)| {
P(ast::Typedef {
id: ast::DUMMY_NODE_ID,
span: self.span,
ident: ident,
vis: ast::Inherited,
attrs: Vec::new(),
typ: type_def.to_ty(cx,
self.span,
type_ident,
generics
),
})
});

let Generics { mut lifetimes, ty_params, mut where_clause } =
self.generics.to_generics(cx, self.span, type_ident, generics);
let mut ty_params = ty_params.into_vec();
Expand Down Expand Up @@ -494,7 +512,11 @@ impl<'a> TraitDef<'a> {
methods.into_iter()
.map(|method| {
ast::MethodImplItem(method)
}).collect()))
}).chain(
associated_types.map(|type_| {
ast::TypeImplItem(type_)
})
).collect()))
}

fn expand_struct_def(&self,
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/hash.rs
Expand Up @@ -54,7 +54,8 @@ pub fn expand_deriving_hash<F>(cx: &mut ExtCtxt,
hash_substructure(a, b, c)
})
}
)
),
associated_types: Vec::new(),
};

hash_trait_def.expand(cx, mitem, item, push);
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax/ext/deriving/primitive.rs
Expand Up @@ -65,7 +65,9 @@ pub fn expand_deriving_from_primitive<F>(cx: &mut ExtCtxt,
combine_substructure: combine_substructure(box |c, s, sub| {
cs_from("u64", c, s, sub)
}),
})
}
),
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
Expand Down
3 changes: 2 additions & 1 deletion src/libsyntax/ext/deriving/rand.rs
Expand Up @@ -49,7 +49,8 @@ pub fn expand_deriving_rand<F>(cx: &mut ExtCtxt,
rand_substructure(a, b, c)
})
}
)
),
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
Expand Down
7 changes: 4 additions & 3 deletions src/libsyntax/ext/deriving/show.rs
Expand Up @@ -35,10 +35,10 @@ pub fn expand_deriving_show<F>(cx: &mut ExtCtxt,
let trait_def = TraitDef {
span: span,
attributes: Vec::new(),
path: Path::new(vec!("std", "fmt", "Debug")),
path: Path::new(vec!["std", "fmt", "Debug"]),
additional_bounds: Vec::new(),
generics: LifetimeBounds::empty(),
methods: vec!(
methods: vec![
MethodDef {
name: "fmt",
generics: LifetimeBounds::empty(),
Expand All @@ -50,7 +50,8 @@ pub fn expand_deriving_show<F>(cx: &mut ExtCtxt,
show_substructure(a, b, c)
})
}
)
],
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
}
Expand Down

0 comments on commit e563215

Please sign in to comment.