Skip to content

Commit c39271e

Browse files
committed
Allow built-in traits to be derived
[RFC #3] cc #13231
1 parent a51be8e commit c39271e

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

src/libsyntax/ext/deriving/bounds.rs

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
use ast::{MetaItem, MetaWord, Item};
12+
use codemap::Span;
13+
use ext::base::ExtCtxt;
14+
use ext::deriving::generic::*;
15+
16+
pub fn expand_deriving_bound(cx: &mut ExtCtxt,
17+
span: Span,
18+
mitem: @MetaItem,
19+
item: @Item,
20+
push: |@Item|) {
21+
22+
let name = match mitem.node {
23+
MetaWord(ref tname) => {
24+
match tname.get() {
25+
"Copy" => "Copy",
26+
"Send" => "Send",
27+
"Share" => "Share",
28+
ref tname => cx.span_bug(span,
29+
format!("expected built-in trait name but found {}",
30+
*tname))
31+
}
32+
},
33+
_ => return cx.span_err(span, "unexpected value in deriving, expected a trait")
34+
};
35+
36+
let trait_def = TraitDef {
37+
span: span,
38+
attributes: Vec::new(),
39+
path: Path::new(vec!("std", "kinds", name)),
40+
additional_bounds: Vec::new(),
41+
generics: LifetimeBounds::empty(),
42+
methods: vec!()
43+
};
44+
45+
trait_def.expand(cx, mitem, item, push)
46+
}

src/libsyntax/ext/deriving/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use ast::{Item, MetaItem, MetaList, MetaNameValue, MetaWord};
2222
use ext::base::ExtCtxt;
2323
use codemap::Span;
2424

25+
pub mod bounds;
2526
pub mod clone;
2627
pub mod encodable;
2728
pub mod decodable;
@@ -90,6 +91,10 @@ pub fn expand_meta_deriving(cx: &mut ExtCtxt,
9091

9192
"FromPrimitive" => expand!(primitive::expand_deriving_from_primitive),
9293

94+
"Send" => expand!(bounds::expand_deriving_bound),
95+
"Share" => expand!(bounds::expand_deriving_bound),
96+
"Copy" => expand!(bounds::expand_deriving_bound),
97+
9398
ref tname => {
9499
cx.span_err(titem.span, format!("unknown \
95100
`deriving` trait: `{}`", *tname));
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//NOTE: Remove in the next snapshot
12+
#[cfg(not(stage0))]
13+
#[deriving(Share(Bad),Send,Copy)]
14+
//~^ ERROR unexpected value in deriving, expected a trait
15+
struct Test;
16+
17+
pub fn main() {}

src/test/run-pass/deriving-bounds.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//NOTE: Remove in the next snapshot
12+
#[cfg(not(stage0))]
13+
#[deriving(Share,Send,Copy)]
14+
struct Test;
15+
16+
pub fn main() {}

0 commit comments

Comments
 (0)