File: hrtb-perfect-forwarding.rs

package info (click to toggle)
rustc 1.85.0%2Bdfsg3-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, 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 (56 lines) | stat: -rw-r--r-- 1,633 bytes parent folder | download | duplicates (11)
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
// Test a case where you have an impl of `Foo<X>` for all `X` that
// is being applied to `for<'a> Foo<&'a mut X>`. Issue #19730.

trait Foo<X> {
    fn foo(&mut self, x: X) {}
}

trait Bar<X> {
    fn bar(&mut self, x: X) {}
}

impl<'a, X, F> Foo<X> for &'a mut F where F: Foo<X> + Bar<X> {}

impl<'a, X, F> Bar<X> for &'a mut F where F: Bar<X> {}

fn no_hrtb<'b, T>(mut t: T) //~ WARN function cannot return
where
    T: Bar<&'b isize>,
{
    // OK -- `T : Bar<&'b isize>`, and thus the impl above ensures that
    // `&mut T : Bar<&'b isize>`.
    no_hrtb(&mut t);
}

fn bar_hrtb<T>(mut t: T) //~ WARN function cannot return
where
    T: for<'b> Bar<&'b isize>,
{
    // OK -- `T : for<'b> Bar<&'b isize>`, and thus the impl above
    // ensures that `&mut T : for<'b> Bar<&'b isize>`.  This is an
    // example of a "perfect forwarding" impl.
    bar_hrtb(&mut t);
}

fn foo_hrtb_bar_not<'b, T>(mut t: T) //~ WARN function cannot return
where
    T: for<'a> Foo<&'a isize> + Bar<&'b isize>,
{
    // Not OK -- The forwarding impl for `Foo` requires that `Bar` also
    // be implemented. Thus to satisfy `&mut T : for<'a> Foo<&'a
    // isize>`, we require `T : for<'a> Bar<&'a isize>`, but the where
    // clause only specifies `T : Bar<&'b isize>`.
    foo_hrtb_bar_not(&mut t);
    //~^ ERROR implementation of `Bar` is not general enough
    //~^^ ERROR lifetime may not live long enough
}

fn foo_hrtb_bar_hrtb<T>(mut t: T) //~ WARN function cannot return
where
    T: for<'a> Foo<&'a isize> + for<'b> Bar<&'b isize>,
{
    // OK -- now we have `T : for<'b> Bar<&'b isize>`.
    foo_hrtb_bar_hrtb(&mut t);
}

fn main() {}