]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/traits/traits-conditional-dispatch.rs
New upstream version 1.31.0~beta.4+dfsg1
[rustc.git] / src / test / run-pass / traits / traits-conditional-dispatch.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 // run-pass
12 // Test that we are able to resolve conditional dispatch. Here, the
13 // blanket impl for T:Copy coexists with an impl for Box<T>, because
14 // Box does not impl Copy.
15
16 #![feature(box_syntax)]
17
18 trait Get {
19 fn get(&self) -> Self;
20 }
21
22 trait MyCopy { fn copy(&self) -> Self; }
23 impl MyCopy for u16 { fn copy(&self) -> Self { *self } }
24 impl MyCopy for u32 { fn copy(&self) -> Self { *self } }
25 impl MyCopy for i32 { fn copy(&self) -> Self { *self } }
26 impl<T:Copy> MyCopy for Option<T> { fn copy(&self) -> Self { *self } }
27
28 impl<T:MyCopy> Get for T {
29 fn get(&self) -> T { self.copy() }
30 }
31
32 impl Get for Box<i32> {
33 fn get(&self) -> Box<i32> { box get_it(&**self) }
34 }
35
36 fn get_it<T:Get>(t: &T) -> T {
37 (*t).get()
38 }
39
40 fn main() {
41 assert_eq!(get_it(&1_u32), 1_u32);
42 assert_eq!(get_it(&1_u16), 1_u16);
43 assert_eq!(get_it(&Some(1_u16)), Some(1_u16));
44 assert_eq!(get_it(&Box::new(1)), Box::new(1));
45 }