]>
Commit | Line | Data |
---|---|---|
1a4d82fc JJ |
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 | // General test that function items in static blocks | |
12 | // can be generated with a macro. | |
13 | ||
c34b1796 | 14 | |
1a4d82fc JJ |
15 | struct MyType { |
16 | desc: &'static str, | |
c34b1796 AL |
17 | data: usize, |
18 | code: fn(usize, usize) -> usize | |
1a4d82fc JJ |
19 | } |
20 | ||
21 | impl MyType { | |
c34b1796 | 22 | fn eval(&self, a: usize) -> usize { |
1a4d82fc JJ |
23 | (self.code)(self.data, a) |
24 | } | |
25 | } | |
26 | ||
27 | macro_rules! codegen { | |
28 | ($e:expr, $v:expr) => { | |
29 | { | |
c34b1796 | 30 | fn generated(a: usize, b: usize) -> usize { |
1a4d82fc JJ |
31 | a - ($e * b) |
32 | } | |
33 | MyType { | |
34 | desc: "test", | |
35 | data: $v, | |
36 | code: generated | |
37 | } | |
38 | } | |
39 | } | |
40 | } | |
41 | ||
42 | static GENERATED_CODE_1: MyType = codegen!(2, 100); | |
43 | static GENERATED_CODE_2: MyType = codegen!(5, 1000); | |
44 | ||
45 | pub fn main() { | |
46 | assert_eq!(GENERATED_CODE_1.eval(10), 80); | |
47 | assert_eq!(GENERATED_CODE_2.eval(100), 500); | |
48 | } |