File: generic-default-type-params.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, sid, trixie
  • size: 893,396 kB
  • sloc: xml: 158,127; python: 35,830; javascript: 19,497; cpp: 19,002; sh: 17,245; ansic: 13,127; asm: 4,376; makefile: 1,051; perl: 29; lisp: 29; ruby: 19; sql: 11
file content (53 lines) | stat: -rw-r--r-- 1,028 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
//@ run-pass
struct Foo<A = (isize, char)> {
    a: A
}

impl Foo<isize> {
    fn bar_int(&self) -> isize {
        self.a
    }
}

impl Foo<char> {
    fn bar_char(&self) -> char {
        self.a
    }
}

impl Foo {
    fn bar(&self) {
        let (i, c): (isize, char) = self.a;
        assert_eq!(Foo { a: i }.bar_int(), i);
        assert_eq!(Foo { a: c }.bar_char(), c);
    }
}

impl<A: Clone> Foo<A> {
    fn baz(&self) -> A {
        self.a.clone()
    }
}

fn default_foo(x: Foo) {
    let (i, c): (isize, char) = x.a;
    assert_eq!(i, 1);
    assert_eq!(c, 'a');

    x.bar();
    assert_eq!(x.baz(), (1, 'a'));
}

#[derive(PartialEq, Debug)]
struct BazHelper<T>(T);

#[derive(PartialEq, Debug)]
// Ensure that we can use previous type parameters in defaults.
struct Baz<T, U = BazHelper<T>, V = Option<U>>(T, U, V);

fn main() {
    default_foo(Foo { a: (1, 'a') });

    let x: Baz<bool> = Baz(true, BazHelper(false), Some(BazHelper(true)));
    assert_eq!(x, Baz(true, BazHelper(false), Some(BazHelper(true))));
}