]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/const-block-item-macro-codegen.rs
rollup merge of #20608: nikomatsakis/assoc-types-method-dispatch
[rust.git] / src / test / run-pass / const-block-item-macro-codegen.rs
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
14 #![feature(macro_rules)]
15
16 struct MyType {
17     desc: &'static str,
18     data: uint,
19     code: fn(uint, uint) -> uint
20 }
21
22 impl MyType {
23     fn eval(&self, a: uint) -> uint {
24         (self.code)(self.data, a)
25     }
26 }
27
28 macro_rules! codegen {
29     ($e:expr, $v:expr) => {
30         {
31             fn generated(a: uint, b: uint) -> uint {
32                 a - ($e * b)
33             }
34             MyType {
35                 desc: "test",
36                 data: $v,
37                 code: generated
38             }
39         }
40     }
41 }
42
43 static GENERATED_CODE_1: MyType = codegen!(2, 100);
44 static GENERATED_CODE_2: MyType = codegen!(5, 1000);
45
46 pub fn main() {
47     assert_eq!(GENERATED_CODE_1.eval(10), 80);
48     assert_eq!(GENERATED_CODE_2.eval(100), 500);
49 }