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.
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.
14 #[deriving(PartialEq, Show)]
17 static TEST: &'static str = "Test";
21 let (a, b, c) = (&5u as &Any, &TEST as &Any, &Test as &Any);
23 assert!(a.is::<uint>());
24 assert!(!b.is::<uint>());
25 assert!(!c.is::<uint>());
27 assert!(!a.is::<&'static str>());
28 assert!(b.is::<&'static str>());
29 assert!(!c.is::<&'static str>());
31 assert!(!a.is::<Test>());
32 assert!(!b.is::<Test>());
33 assert!(c.is::<Test>());
38 let (a, b, c) = (box 5u as Box<Any>, box TEST as Box<Any>, box Test as Box<Any>);
40 assert!(a.is::<uint>());
41 assert!(!b.is::<uint>());
42 assert!(!c.is::<uint>());
44 assert!(!a.is::<&'static str>());
45 assert!(b.is::<&'static str>());
46 assert!(!c.is::<&'static str>());
48 assert!(!a.is::<Test>());
49 assert!(!b.is::<Test>());
50 assert!(c.is::<Test>());
54 fn any_downcast_ref() {
57 match a.downcast_ref::<uint>() {
59 x => panic!("Unexpected value {}", x)
62 match a.downcast_ref::<Test>() {
64 x => panic!("Unexpected value {}", x)
69 fn any_downcast_mut() {
73 let a_r = &mut a as &mut Any;
74 let tmp: &mut uint = &mut *b;
75 let b_r = tmp as &mut Any;
77 match a_r.downcast_mut::<uint>() {
82 x => panic!("Unexpected value {}", x)
85 match b_r.downcast_mut::<uint>() {
90 x => panic!("Unexpected value {}", x)
93 match a_r.downcast_mut::<Test>() {
95 x => panic!("Unexpected value {}", x)
98 match b_r.downcast_mut::<Test>() {
100 x => panic!("Unexpected value {}", x)
103 match a_r.downcast_mut::<uint>() {
105 x => panic!("Unexpected value {}", x)
108 match b_r.downcast_mut::<uint>() {
110 x => panic!("Unexpected value {}", x)
116 let test = [0u, ..8];
117 let test = &test as &Any;
118 assert!(test.is::<[uint, ..8]>());
119 assert!(!test.is::<[uint, ..10]>());
124 fn bench_downcast_ref(b: &mut Bencher) {
127 let mut y = &mut x as &mut Any;
128 test::black_box(&mut y);
129 test::black_box(y.downcast_ref::<int>() == Some(&0));