]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/reflect-visit-type.rs
b471d13901e093a4878c370ad2df856ca81ab7b0
[rust.git] / src / test / run-pass / reflect-visit-type.rs
1 // Copyright 2012 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 #![feature(managed_boxes)]
12
13 use std::intrinsics::{TyDesc, get_tydesc, visit_tydesc, TyVisitor, Disr, Opaque};
14
15 struct MyVisitor {
16     types: Vec<String> ,
17 }
18
19 impl TyVisitor for MyVisitor {
20     fn visit_bot(&mut self) -> bool {
21         self.types.push("bot".to_string());
22         println!("visited bot type");
23         true
24     }
25     fn visit_nil(&mut self) -> bool {
26         self.types.push("nil".to_string());
27         println!("visited nil type");
28         true
29     }
30     fn visit_bool(&mut self) -> bool {
31         self.types.push("bool".to_string());
32         println!("visited bool type");
33         true
34     }
35     fn visit_int(&mut self) -> bool {
36         self.types.push("int".to_string());
37         println!("visited int type");
38         true
39     }
40     fn visit_i8(&mut self) -> bool {
41         self.types.push("i8".to_string());
42         println!("visited i8 type");
43         true
44     }
45     fn visit_i16(&mut self) -> bool {
46         self.types.push("i16".to_string());
47         println!("visited i16 type");
48         true
49     }
50     fn visit_i32(&mut self) -> bool { true }
51     fn visit_i64(&mut self) -> bool { true }
52
53     fn visit_uint(&mut self) -> bool { true }
54     fn visit_u8(&mut self) -> bool { true }
55     fn visit_u16(&mut self) -> bool { true }
56     fn visit_u32(&mut self) -> bool { true }
57     fn visit_u64(&mut self) -> bool { true }
58
59     fn visit_f32(&mut self) -> bool { true }
60     fn visit_f64(&mut self) -> bool { true }
61     fn visit_f128(&mut self) -> bool { true }
62
63     fn visit_char(&mut self) -> bool { true }
64
65     fn visit_estr_slice(&mut self) -> bool { true }
66     fn visit_estr_fixed(&mut self,
67                         _sz: uint, _sz2: uint,
68                         _align: uint) -> bool { true }
69
70     fn visit_box(&mut self, _mtbl: uint, _inner: *TyDesc) -> bool { true }
71     fn visit_uniq(&mut self, _mtbl: uint, _inner: *TyDesc) -> bool { true }
72     fn visit_ptr(&mut self, _mtbl: uint, _inner: *TyDesc) -> bool { true }
73     fn visit_rptr(&mut self, _mtbl: uint, _inner: *TyDesc) -> bool { true }
74
75     fn visit_evec_slice(&mut self, _mtbl: uint, _inner: *TyDesc) -> bool { true }
76     fn visit_evec_fixed(&mut self, _n: uint, _sz: uint, _align: uint,
77                         _mtbl: uint, _inner: *TyDesc) -> bool { true }
78
79     fn visit_enter_rec(&mut self, _n_fields: uint,
80                        _sz: uint, _align: uint) -> bool { true }
81     fn visit_rec_field(&mut self, _i: uint, _name: &str,
82                        _mtbl: uint, _inner: *TyDesc) -> bool { true }
83     fn visit_leave_rec(&mut self, _n_fields: uint,
84                        _sz: uint, _align: uint) -> bool { true }
85
86     fn visit_enter_class(&mut self, _name: &str, _named_fields: bool, _n_fields: uint,
87                          _sz: uint, _align: uint) -> bool { true }
88     fn visit_class_field(&mut self, _i: uint, _name: &str, _named: bool,
89                          _mtbl: uint, _inner: *TyDesc) -> bool { true }
90     fn visit_leave_class(&mut self, _name: &str, _named_fields: bool, _n_fields: uint,
91                          _sz: uint, _align: uint) -> bool { true }
92
93     fn visit_enter_tup(&mut self, _n_fields: uint,
94                        _sz: uint, _align: uint) -> bool { true }
95     fn visit_tup_field(&mut self, _i: uint, _inner: *TyDesc) -> bool { true }
96     fn visit_leave_tup(&mut self, _n_fields: uint,
97                        _sz: uint, _align: uint) -> bool { true }
98
99     fn visit_enter_enum(&mut self, _n_variants: uint,
100                         _get_disr: unsafe extern fn(ptr: *Opaque) -> Disr,
101                         _sz: uint, _align: uint) -> bool { true }
102     fn visit_enter_enum_variant(&mut self,
103                                 _variant: uint,
104                                 _disr_val: Disr,
105                                 _n_fields: uint,
106                                 _name: &str) -> bool { true }
107     fn visit_enum_variant_field(&mut self, _i: uint, _offset: uint, _inner: *TyDesc)
108         -> bool { true }
109     fn visit_leave_enum_variant(&mut self,
110                                 _variant: uint,
111                                 _disr_val: Disr,
112                                 _n_fields: uint,
113                                 _name: &str) -> bool { true }
114     fn visit_leave_enum(&mut self,
115                         _n_variants: uint,
116                         _get_disr: unsafe extern fn(ptr: *Opaque) -> Disr,
117                         _sz: uint, _align: uint) -> bool { true }
118
119     fn visit_enter_fn(&mut self, _purity: uint, _proto: uint,
120                       _n_inputs: uint, _retstyle: uint) -> bool { true }
121     fn visit_fn_input(&mut self, _i: uint, _mode: uint, _inner: *TyDesc) -> bool { true }
122     fn visit_fn_output(&mut self, _retstyle: uint, _variadic: bool, _inner: *TyDesc)
123         -> bool { true }
124     fn visit_leave_fn(&mut self, _purity: uint, _proto: uint,
125                       _n_inputs: uint, _retstyle: uint) -> bool { true }
126
127
128     fn visit_trait(&mut self, _name: &str) -> bool { true }
129     fn visit_param(&mut self, _i: uint) -> bool { true }
130     fn visit_self(&mut self) -> bool { true }
131 }
132
133 fn visit_ty<T>(v: &mut MyVisitor) {
134     unsafe { visit_tydesc(get_tydesc::<T>(), v as &mut TyVisitor) }
135 }
136
137 pub fn main() {
138     let mut v = MyVisitor {types: Vec::new()};
139
140     visit_ty::<bool>(&mut v);
141     visit_ty::<int>(&mut v);
142     visit_ty::<i8>(&mut v);
143     visit_ty::<i16>(&mut v);
144
145     for s in v.types.iter() {
146         println!("type: {}", (*s).clone());
147     }
148
149     let vec_types: Vec<String> = v.types.clone().move_iter().collect();
150     assert_eq!(vec_types, vec!("bool".to_string(), "int".to_string(),
151                                "i8".to_string(), "i16".to_string()));
152 }