]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/specialization/defaultimpl/specialization-basics.rs
Auto merge of #41433 - estebank:constructor, r=michaelwoerister
[rust.git] / src / test / run-pass / specialization / defaultimpl / specialization-basics.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 #![feature(specialization)]
12
13 // Tests a variety of basic specialization scenarios and method
14 // dispatch for them.
15
16 trait Foo {
17     fn foo(&self) -> &'static str;
18 }
19
20 default impl<T> Foo for T {
21     fn foo(&self) -> &'static str {
22         "generic"
23     }
24 }
25
26 default impl<T: Clone> Foo for T {
27     fn foo(&self) -> &'static str {
28         "generic Clone"
29     }
30 }
31
32 default impl<T, U> Foo for (T, U) where T: Clone, U: Clone {
33     fn foo(&self) -> &'static str {
34         "generic pair"
35     }
36 }
37
38 default impl<T: Clone> Foo for (T, T) {
39     fn foo(&self) -> &'static str {
40         "generic uniform pair"
41     }
42 }
43
44 default impl Foo for (u8, u32) {
45     fn foo(&self) -> &'static str {
46         "(u8, u32)"
47     }
48 }
49
50 default impl Foo for (u8, u8) {
51     fn foo(&self) -> &'static str {
52         "(u8, u8)"
53     }
54 }
55
56 default impl<T: Clone> Foo for Vec<T> {
57     fn foo(&self) -> &'static str {
58         "generic Vec"
59     }
60 }
61
62 impl Foo for Vec<i32> {
63     fn foo(&self) -> &'static str {
64         "Vec<i32>"
65     }
66 }
67
68 impl Foo for String {
69     fn foo(&self) -> &'static str {
70         "String"
71     }
72 }
73
74 impl Foo for i32 {
75     fn foo(&self) -> &'static str {
76         "i32"
77     }
78 }
79
80 struct NotClone;
81
82 trait MyMarker {}
83 default impl<T: Clone + MyMarker> Foo for T {
84     fn foo(&self) -> &'static str {
85         "generic Clone + MyMarker"
86     }
87 }
88
89 #[derive(Clone)]
90 struct MarkedAndClone;
91 impl MyMarker for MarkedAndClone {}
92
93 fn  main() {
94     assert!(NotClone.foo() == "generic");
95     assert!(0u8.foo() == "generic Clone");
96     assert!(vec![NotClone].foo() == "generic");
97     assert!(vec![0u8].foo() == "generic Vec");
98     assert!(vec![0i32].foo() == "Vec<i32>");
99     assert!(0i32.foo() == "i32");
100     assert!(String::new().foo() == "String");
101     assert!(((), 0).foo() == "generic pair");
102     assert!(((), ()).foo() == "generic uniform pair");
103     assert!((0u8, 0u32).foo() == "(u8, u32)");
104     assert!((0u8, 0u8).foo() == "(u8, u8)");
105     assert!(MarkedAndClone.foo() == "generic Clone + MyMarker");
106 }