]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/generic-default-type-params.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / generic-default-type-params.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 struct Foo<A = (isize, char)> {
12     a: A
13 }
14
15 impl Foo<isize> {
16     fn bar_int(&self) -> isize {
17         self.a
18     }
19 }
20
21 impl Foo<char> {
22     fn bar_char(&self) -> char {
23         self.a
24     }
25 }
26
27 impl Foo {
28     fn bar(&self) {
29         let (i, c): (isize, char) = self.a;
30         assert_eq!(Foo { a: i }.bar_int(), i);
31         assert_eq!(Foo { a: c }.bar_char(), c);
32     }
33 }
34
35 impl<A: Clone> Foo<A> {
36     fn baz(&self) -> A {
37         self.a.clone()
38     }
39 }
40
41 fn default_foo(x: Foo) {
42     let (i, c): (isize, char) = x.a;
43     assert_eq!(i, 1);
44     assert_eq!(c, 'a');
45
46     x.bar();
47     assert_eq!(x.baz(), (1, 'a'));
48 }
49
50 #[derive(PartialEq, Debug)]
51 struct BazHelper<T>(T);
52
53 #[derive(PartialEq, Debug)]
54 // Ensure that we can use previous type parameters in defaults.
55 struct Baz<T, U = BazHelper<T>, V = Option<U>>(T, U, V);
56
57 fn main() {
58     default_foo(Foo { a: (1, 'a') });
59
60     let x: Baz<bool> = Baz(true, BazHelper(false), Some(BazHelper(true)));
61     assert_eq!(x, Baz(true, BazHelper(false), Some(BazHelper(true))));
62 }