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