]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/traits-conditional-dispatch.rs
fallout: run-pass tests that use box. (many could be ported to `Box::new` instead...
[rust.git] / src / test / run-pass / 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 // Test that we are able to resolve conditional dispatch.  Here, the
12 // blanket impl for T:Copy coexists with an impl for Box<T>, because
13 // Box does not impl Copy.
14
15 #![allow(unknown_features)]
16 #![feature(box_syntax)]
17
18 trait Get {
19     fn get(&self) -> Self;
20 }
21
22 impl<T:Copy> Get for T {
23     fn get(&self) -> T { *self }
24 }
25
26 impl<T:Get> Get for Box<T> {
27     fn get(&self) -> Box<T> { box get_it(&**self) }
28 }
29
30 fn get_it<T:Get>(t: &T) -> T {
31     (*t).get()
32 }
33
34 fn main() {
35     assert_eq!(get_it(&1_u32), 1_u32);
36     assert_eq!(get_it(&1_u16), 1_u16);
37     assert_eq!(get_it(&Some(1_u16)), Some(1_u16));
38     assert_eq!(get_it(&box 1i), box 1i);
39 }