]> git.lizzy.rs Git - rust.git/blob - tests/ui/traits/conditional-dispatch.rs
Merge commit '598f0909568a51de8a2d1148f55a644fd8dffad0' into sync_cg_clif-2023-01-24
[rust.git] / tests / ui / traits / conditional-dispatch.rs
1 // run-pass
2 // Test that we are able to resolve conditional dispatch.  Here, the
3 // blanket impl for T:Copy coexists with an impl for Box<T>, because
4 // Box does not impl Copy.
5
6
7 trait Get {
8     fn get(&self) -> Self;
9 }
10
11 trait MyCopy { fn copy(&self) -> Self; }
12 impl MyCopy for u16 { fn copy(&self) -> Self { *self } }
13 impl MyCopy for u32 { fn copy(&self) -> Self { *self } }
14 impl MyCopy for i32 { fn copy(&self) -> Self { *self } }
15 impl<T:Copy> MyCopy for Option<T> { fn copy(&self) -> Self { *self } }
16
17 impl<T:MyCopy> Get for T {
18     fn get(&self) -> T { self.copy() }
19 }
20
21 impl Get for Box<i32> {
22     fn get(&self) -> Box<i32> { Box::new(get_it(&**self)) }
23 }
24
25 fn get_it<T:Get>(t: &T) -> T {
26     (*t).get()
27 }
28
29 fn main() {
30     assert_eq!(get_it(&1_u32), 1_u32);
31     assert_eq!(get_it(&1_u16), 1_u16);
32     assert_eq!(get_it(&Some(1_u16)), Some(1_u16));
33     assert_eq!(get_it(&Box::new(1)), Box::new(1));
34 }