]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/traits-multidispatch-infer-convert-target.rs
cleanup: s/impl Copy/#[derive(Copy)]/g
[rust.git] / src / test / run-pass / traits-multidispatch-infer-convert-target.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 can infer the Target based on the Self or vice versa.
12
13 use std::mem;
14
15 trait Convert<Target> {
16     fn convert(&self) -> Target;
17 }
18
19 impl Convert<u32> for i16 {
20     fn convert(&self) -> u32 {
21         *self as u32
22     }
23 }
24
25 impl Convert<i16> for u32 {
26     fn convert(&self) -> i16 {
27         *self as i16
28     }
29 }
30
31 fn test<T,U>(_: T, _: U, t_size: uint, u_size: uint)
32 where T : Convert<U>
33 {
34     assert_eq!(mem::size_of::<T>(), t_size);
35     assert_eq!(mem::size_of::<U>(), u_size);
36 }
37
38 fn main() {
39     use std::default::Default;
40     // T = i16, U = u32
41     test(22_i16, Default::default(),  2, 4);
42
43     // T = u32, U = i16
44     test(22_u32, Default::default(), 4, 2);
45 }