Skip to content

Commit f2b4937

Browse files
author
Jorge Aparicio
committed
tweak #[derive(Copy)], add #[derive(Pod)]
1 parent 30e5a30 commit f2b4937

File tree

15 files changed

+163
-141
lines changed

15 files changed

+163
-141
lines changed

src/libsyntax/ext/deriving/bounds.rs

Lines changed: 55 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,77 @@ use ext::deriving::generic::*;
1515
use ext::deriving::generic::ty::*;
1616
use ptr::P;
1717

18-
pub fn expand_deriving_unsafe_bound<F>(cx: &mut ExtCtxt,
19-
span: Span,
20-
_: &MetaItem,
21-
_: &Item,
22-
_: F) where
23-
F: FnOnce(P<Item>),
24-
{
18+
pub fn expand_deriving_unsafe_bound(cx: &mut ExtCtxt,
19+
span: Span,
20+
_: &MetaItem,
21+
_: &Item,
22+
_: &mut FnMut(P<Item>)) {
2523
cx.span_err(span, "this unsafe trait should be implemented explicitly");
2624
}
2725

28-
pub fn expand_deriving_copy<F>(cx: &mut ExtCtxt,
29-
span: Span,
30-
mitem: &MetaItem,
31-
item: &Item,
32-
push: F) where
33-
F: FnOnce(P<Item>),
34-
{
35-
let path = Path::new(vec![
26+
pub fn expand_deriving_copy(cx: &mut ExtCtxt,
27+
span: Span,
28+
mitem: &MetaItem,
29+
item: &Item,
30+
push: &mut FnMut(P<Item>)) {
31+
let pod_path = Path::new(vec![
32+
if cx.use_std { "std" } else { "core" },
33+
"marker",
34+
"Pod",
35+
]);
36+
37+
let trait_def = TraitDef {
38+
span: span,
39+
attributes: Vec::new(),
40+
path: pod_path.clone(),
41+
additional_bounds: Vec::new(),
42+
bound_self: true,
43+
generics: LifetimeBounds::empty(),
44+
methods: Vec::new(),
45+
associated_types: Vec::new(),
46+
};
47+
48+
// impl<T: Pod> Pod for Foo<T> {}
49+
trait_def.expand(cx, mitem, item, push);
50+
51+
let copy_path = Path::new(vec![
3652
if cx.use_std { "std" } else { "core" },
3753
"marker",
3854
"Copy",
3955
]);
4056

57+
let trait_def = TraitDef {
58+
path: copy_path,
59+
additional_bounds: vec![Ty::Literal(pod_path)],
60+
bound_self: false,
61+
.. trait_def
62+
};
63+
64+
// impl<T: Pod> Copy for Foo<T> {}
65+
trait_def.expand(cx, mitem, item, push);
66+
}
67+
68+
pub fn expand_deriving_pod(cx: &mut ExtCtxt,
69+
span: Span,
70+
mitem: &MetaItem,
71+
item: &Item,
72+
push: &mut FnMut(P<Item>)) {
73+
let path = Path::new(vec![
74+
if cx.use_std { "std" } else { "core" },
75+
"marker",
76+
"Pod",
77+
]);
78+
4179
let trait_def = TraitDef {
4280
span: span,
4381
attributes: Vec::new(),
4482
path: path,
4583
additional_bounds: Vec::new(),
84+
bound_self: true,
4685
generics: LifetimeBounds::empty(),
4786
methods: Vec::new(),
4887
associated_types: Vec::new(),
4988
};
5089

51-
trait_def.expand(cx, mitem, item, push)
90+
trait_def.expand(cx, mitem, item, push);
5291
}

src/libsyntax/ext/deriving/clone.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ use ext::deriving::generic::ty::*;
1717
use parse::token::InternedString;
1818
use ptr::P;
1919

20-
pub fn expand_deriving_clone<F>(cx: &mut ExtCtxt,
21-
span: Span,
22-
mitem: &MetaItem,
23-
item: &Item,
24-
push: F) where
25-
F: FnOnce(P<Item>),
26-
{
20+
pub fn expand_deriving_clone(cx: &mut ExtCtxt,
21+
span: Span,
22+
mitem: &MetaItem,
23+
item: &Item,
24+
push: &mut FnMut(P<Item>)) {
2725
let inline = cx.meta_word(span, InternedString::new("inline"));
2826
let attrs = vec!(cx.attribute(span, inline));
2927
let trait_def = TraitDef {
3028
span: span,
3129
attributes: Vec::new(),
3230
path: path_std!(cx, core::clone::Clone),
31+
bound_self: true,
3332
additional_bounds: Vec::new(),
3433
generics: LifetimeBounds::empty(),
3534
methods: vec!(

src/libsyntax/ext/deriving/cmp/eq.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ use ext::deriving::generic::ty::*;
1717
use parse::token::InternedString;
1818
use ptr::P;
1919

20-
pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
21-
span: Span,
22-
mitem: &MetaItem,
23-
item: &Item,
24-
push: F) where
25-
F: FnOnce(P<Item>),
26-
{
20+
pub fn expand_deriving_eq(cx: &mut ExtCtxt,
21+
span: Span,
22+
mitem: &MetaItem,
23+
item: &Item,
24+
push: &mut FnMut(P<Item>)) {
2725
// structures are equal if all fields are equal, and non equal, if
2826
// any fields are not equal or if the enum variants are different
2927
fn cs_eq(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
@@ -83,6 +81,7 @@ pub fn expand_deriving_eq<F>(cx: &mut ExtCtxt,
8381
span: span,
8482
attributes: Vec::new(),
8583
path: path_std!(cx, core::cmp::PartialEq),
84+
bound_self: true,
8685
additional_bounds: Vec::new(),
8786
generics: LifetimeBounds::empty(),
8887
methods: vec!(

src/libsyntax/ext/deriving/cmp/ord.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ use ext::deriving::generic::ty::*;
2020
use parse::token::InternedString;
2121
use ptr::P;
2222

23-
pub fn expand_deriving_ord<F>(cx: &mut ExtCtxt,
24-
span: Span,
25-
mitem: &MetaItem,
26-
item: &Item,
27-
push: F) where
28-
F: FnOnce(P<Item>),
29-
{
23+
pub fn expand_deriving_ord(cx: &mut ExtCtxt,
24+
span: Span,
25+
mitem: &MetaItem,
26+
item: &Item,
27+
push: &mut FnMut(P<Item>)) {
3028
macro_rules! md {
3129
($name:expr, $op:expr, $equal:expr) => { {
3230
let inline = cx.meta_word(span, InternedString::new("inline"));
@@ -70,6 +68,7 @@ pub fn expand_deriving_ord<F>(cx: &mut ExtCtxt,
7068
span: span,
7169
attributes: vec![],
7270
path: path_std!(cx, core::cmp::PartialOrd),
71+
bound_self: true,
7372
additional_bounds: vec![],
7473
generics: LifetimeBounds::empty(),
7574
methods: vec![

src/libsyntax/ext/deriving/cmp/totaleq.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,11 @@ use ext::deriving::generic::ty::*;
1717
use parse::token::InternedString;
1818
use ptr::P;
1919

20-
pub fn expand_deriving_totaleq<F>(cx: &mut ExtCtxt,
21-
span: Span,
22-
mitem: &MetaItem,
23-
item: &Item,
24-
push: F) where
25-
F: FnOnce(P<Item>),
26-
{
20+
pub fn expand_deriving_totaleq(cx: &mut ExtCtxt,
21+
span: Span,
22+
mitem: &MetaItem,
23+
item: &Item,
24+
push: &mut FnMut(P<Item>)) {
2725
fn cs_total_eq_assert(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P<Expr> {
2826
cs_same_method(|cx, span, exprs| {
2927
// create `a.<method>(); b.<method>(); c.<method>(); ...`
@@ -48,6 +46,7 @@ pub fn expand_deriving_totaleq<F>(cx: &mut ExtCtxt,
4846
span: span,
4947
attributes: Vec::new(),
5048
path: path_std!(cx, core::cmp::Eq),
49+
bound_self: true,
5150
additional_bounds: Vec::new(),
5251
generics: LifetimeBounds::empty(),
5352
methods: vec!(

src/libsyntax/ext/deriving/cmp/totalord.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,18 @@ use ext::deriving::generic::ty::*;
1818
use parse::token::InternedString;
1919
use ptr::P;
2020

21-
pub fn expand_deriving_totalord<F>(cx: &mut ExtCtxt,
22-
span: Span,
23-
mitem: &MetaItem,
24-
item: &Item,
25-
push: F) where
26-
F: FnOnce(P<Item>),
27-
{
21+
pub fn expand_deriving_totalord(cx: &mut ExtCtxt,
22+
span: Span,
23+
mitem: &MetaItem,
24+
item: &Item,
25+
push: &mut FnMut(P<Item>)) {
2826
let inline = cx.meta_word(span, InternedString::new("inline"));
2927
let attrs = vec!(cx.attribute(span, inline));
3028
let trait_def = TraitDef {
3129
span: span,
3230
attributes: Vec::new(),
3331
path: path_std!(cx, core::cmp::Ord),
32+
bound_self: true,
3433
additional_bounds: Vec::new(),
3534
generics: LifetimeBounds::empty(),
3635
methods: vec!(

src/libsyntax/ext/deriving/decodable.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,34 +21,28 @@ use parse::token::InternedString;
2121
use parse::token;
2222
use ptr::P;
2323

24-
pub fn expand_deriving_rustc_decodable<F>(cx: &mut ExtCtxt,
25-
span: Span,
26-
mitem: &MetaItem,
27-
item: &Item,
28-
push: F) where
29-
F: FnOnce(P<Item>),
30-
{
24+
pub fn expand_deriving_rustc_decodable(cx: &mut ExtCtxt,
25+
span: Span,
26+
mitem: &MetaItem,
27+
item: &Item,
28+
push: &mut FnMut(P<Item>)) {
3129
expand_deriving_decodable_imp(cx, span, mitem, item, push, "rustc_serialize")
3230
}
3331

34-
pub fn expand_deriving_decodable<F>(cx: &mut ExtCtxt,
35-
span: Span,
36-
mitem: &MetaItem,
37-
item: &Item,
38-
push: F) where
39-
F: FnOnce(P<Item>),
40-
{
32+
pub fn expand_deriving_decodable(cx: &mut ExtCtxt,
33+
span: Span,
34+
mitem: &MetaItem,
35+
item: &Item,
36+
push: &mut FnMut(P<Item>)) {
4137
expand_deriving_decodable_imp(cx, span, mitem, item, push, "serialize")
4238
}
4339

44-
fn expand_deriving_decodable_imp<F>(cx: &mut ExtCtxt,
45-
span: Span,
46-
mitem: &MetaItem,
47-
item: &Item,
48-
push: F,
49-
krate: &'static str) where
50-
F: FnOnce(P<Item>),
51-
{
40+
fn expand_deriving_decodable_imp(cx: &mut ExtCtxt,
41+
span: Span,
42+
mitem: &MetaItem,
43+
item: &Item,
44+
push: &mut FnMut(P<Item>),
45+
krate: &'static str) {
5246
if !cx.use_std {
5347
// FIXME(#21880): lift this requirement.
5448
cx.span_err(span, "this trait cannot be derived with #![no_std]");
@@ -59,6 +53,7 @@ fn expand_deriving_decodable_imp<F>(cx: &mut ExtCtxt,
5953
span: span,
6054
attributes: Vec::new(),
6155
path: Path::new_(vec!(krate, "Decodable"), None, vec!(), true),
56+
bound_self: true,
6257
additional_bounds: Vec::new(),
6358
generics: LifetimeBounds::empty(),
6459
methods: vec!(

src/libsyntax/ext/deriving/default.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ use ext::deriving::generic::ty::*;
1717
use parse::token::InternedString;
1818
use ptr::P;
1919

20-
pub fn expand_deriving_default<F>(cx: &mut ExtCtxt,
21-
span: Span,
22-
mitem: &MetaItem,
23-
item: &Item,
24-
push: F) where
25-
F: FnOnce(P<Item>),
26-
{
20+
pub fn expand_deriving_default(cx: &mut ExtCtxt,
21+
span: Span,
22+
mitem: &MetaItem,
23+
item: &Item,
24+
push: &mut FnMut(P<Item>)) {
2725
let inline = cx.meta_word(span, InternedString::new("inline"));
2826
let attrs = vec!(cx.attribute(span, inline));
2927
let trait_def = TraitDef {
3028
span: span,
3129
attributes: Vec::new(),
3230
path: path_std!(cx, core::default::Default),
31+
bound_self: true,
3332
additional_bounds: Vec::new(),
3433
generics: LifetimeBounds::empty(),
3534
methods: vec!(

src/libsyntax/ext/deriving/encodable.rs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -97,34 +97,28 @@ use ext::deriving::generic::ty::*;
9797
use parse::token;
9898
use ptr::P;
9999

100-
pub fn expand_deriving_rustc_encodable<F>(cx: &mut ExtCtxt,
101-
span: Span,
102-
mitem: &MetaItem,
103-
item: &Item,
104-
push: F) where
105-
F: FnOnce(P<Item>),
106-
{
100+
pub fn expand_deriving_rustc_encodable(cx: &mut ExtCtxt,
101+
span: Span,
102+
mitem: &MetaItem,
103+
item: &Item,
104+
push: &mut FnMut(P<Item>)) {
107105
expand_deriving_encodable_imp(cx, span, mitem, item, push, "rustc_serialize")
108106
}
109107

110-
pub fn expand_deriving_encodable<F>(cx: &mut ExtCtxt,
111-
span: Span,
112-
mitem: &MetaItem,
113-
item: &Item,
114-
push: F) where
115-
F: FnOnce(P<Item>),
116-
{
108+
pub fn expand_deriving_encodable(cx: &mut ExtCtxt,
109+
span: Span,
110+
mitem: &MetaItem,
111+
item: &Item,
112+
push: &mut FnMut(P<Item>)) {
117113
expand_deriving_encodable_imp(cx, span, mitem, item, push, "serialize")
118114
}
119115

120-
fn expand_deriving_encodable_imp<F>(cx: &mut ExtCtxt,
121-
span: Span,
122-
mitem: &MetaItem,
123-
item: &Item,
124-
push: F,
125-
krate: &'static str) where
126-
F: FnOnce(P<Item>),
127-
{
116+
fn expand_deriving_encodable_imp(cx: &mut ExtCtxt,
117+
span: Span,
118+
mitem: &MetaItem,
119+
item: &Item,
120+
push: &mut FnMut(P<Item>),
121+
krate: &'static str) {
128122
if !cx.use_std {
129123
// FIXME(#21880): lift this requirement.
130124
cx.span_err(span, "this trait cannot be derived with #![no_std]");
@@ -135,6 +129,7 @@ fn expand_deriving_encodable_imp<F>(cx: &mut ExtCtxt,
135129
span: span,
136130
attributes: Vec::new(),
137131
path: Path::new_(vec!(krate, "Encodable"), None, vec!(), true),
132+
bound_self: true,
138133
additional_bounds: Vec::new(),
139134
generics: LifetimeBounds::empty(),
140135
methods: vec!(

0 commit comments

Comments
 (0)