]>
Commit | Line | Data |
---|---|---|
f9f354fc XL |
1 | pub trait Get<T> { |
2 | fn get(self) -> T; | |
3 | } | |
4 | ||
5 | struct Foo { | |
6 | x: usize, | |
7 | } | |
8 | ||
9 | impl Get<usize> for Foo { | |
10 | fn get(self) -> usize { | |
11 | self.x | |
12 | } | |
13 | } | |
14 | ||
15 | fn foo<G, T>(g: G, dest: &mut T) -> impl FnOnce() | |
16 | where | |
17 | G: Get<T> | |
18 | { | |
cdc7bbd5 | 19 | move || { |
f9f354fc XL |
20 | *dest = g.get(); |
21 | } | |
22 | } | |
23 | ||
24 | // After applying suggestion for `foo`: | |
25 | fn bar<G, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ | |
f9f354fc XL |
26 | where |
27 | G: Get<T> | |
28 | { | |
29 | move || { | |
30 | *dest = g.get(); | |
31 | } | |
32 | } | |
33 | ||
34 | ||
35 | // After applying suggestion for `bar`: | |
36 | fn baz<G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ //~ ERROR undeclared lifetime | |
37 | where | |
38 | G: Get<T> | |
39 | { | |
40 | move || { | |
41 | *dest = g.get(); | |
42 | } | |
43 | } | |
44 | ||
45 | // After applying suggestion for `baz`: | |
46 | fn qux<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ | |
f9f354fc XL |
47 | where |
48 | G: Get<T> | |
49 | { | |
50 | move || { | |
51 | *dest = g.get(); | |
52 | } | |
53 | } | |
54 | ||
55 | // Same as above, but show that we pay attention to lifetime names from parent item | |
56 | impl<'a> Foo { | |
57 | fn qux<'b, G: Get<T> + 'b, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ { | |
f9f354fc XL |
58 | move || { |
59 | *dest = g.get(); | |
60 | } | |
61 | } | |
62 | } | |
63 | ||
64 | // After applying suggestion for `qux`: | |
65 | fn bat<'a, G: 'a, T>(g: G, dest: &mut T) -> impl FnOnce() + '_ + 'a | |
f9f354fc XL |
66 | where |
67 | G: Get<T> | |
68 | { | |
69 | move || { | |
70 | *dest = g.get(); | |
71 | } | |
72 | } | |
73 | ||
74 | // Potential incorrect attempt: | |
75 | fn bak<'a, G, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a | |
f9f354fc XL |
76 | where |
77 | G: Get<T> | |
78 | { | |
79 | move || { | |
80 | *dest = g.get(); | |
81 | } | |
82 | } | |
83 | ||
84 | ||
85 | // We need to tie the lifetime of `G` with the lifetime of `&mut T` and the returned closure: | |
86 | fn ok<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + 'a | |
87 | where | |
88 | G: Get<T> | |
89 | { | |
90 | move || { | |
91 | *dest = g.get(); | |
92 | } | |
93 | } | |
94 | ||
95 | // This also works. The `'_` isn't necessary but it's where we arrive to following the suggestions: | |
96 | fn ok2<'a, G: 'a, T>(g: G, dest: &'a mut T) -> impl FnOnce() + '_ + 'a | |
97 | where | |
98 | G: Get<T> | |
99 | { | |
100 | move || { | |
101 | *dest = g.get(); | |
102 | } | |
103 | } | |
104 | ||
105 | fn main() {} |