File: free-fn-to-trait-method.rs

package info (click to toggle)
rustc 1.87.0%2Bdfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 925,564 kB
  • sloc: xml: 158,127; python: 36,039; javascript: 19,761; sh: 19,737; cpp: 18,981; ansic: 13,133; asm: 4,376; makefile: 710; perl: 29; lisp: 28; ruby: 19; sql: 11
file content (56 lines) | stat: -rw-r--r-- 1,367 bytes parent folder | download | duplicates (15)
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
54
55
56
#![feature(fn_delegation)]
#![allow(incomplete_features)]

mod default_param {
    pub trait Trait<T = u32> {
        fn foo(&self, _: T) {}
    }

    impl<T> Trait<T> for u8 {}
}

mod types_and_lifetimes {
    pub trait Trait<'a, T> {
        fn foo<'b: 'b>(&'a self, x: &'b T) {
            loop {}
        }
    }
    impl<'a, T> Trait<'a, T> for u8 {}
}

mod bounds {
    pub trait Trait<T> {
        fn foo<U: Clone>(&self, t: T, u: U) where T: Copy {}
    }

    impl<T> Trait<T> for u8 {}
}

mod generic_arguments {
    trait Trait<T> {
        fn foo<U>(&self, _: U, _: T) {}
    }

    impl<T> Trait<T> for u8 {}

    reuse Trait::<_>::foo::<i32> as generic_arguments1;
    //~^ ERROR mismatched types
    reuse <u8 as Trait<_>>::foo as generic_arguments2;
    //~^ ERROR mismatched types
    reuse <_ as Trait<_>>::foo as generic_arguments3; // OK
}

reuse default_param::Trait::foo as default_param;
reuse types_and_lifetimes::Trait::foo as types_and_lifetimes;
reuse bounds::Trait::foo as bounds;

fn main() {
    default_param(&0u8, "hello world"); // OK, default params are not substituted
    types_and_lifetimes::<'static, 'static, _, _>(&0u8, &0u16); // OK, lifetimes go first

    struct S;
    struct U;
    bounds(&0u8, S, U);
    //~^ ERROR the trait bound `S: Copy` is not satisfied
    //~| ERROR the trait bound `U: Clone` is not satisfied
}