I'm having a hard time getting this to compile:
use std::thread::{self, JoinHandle};
struct Foo<'c> {
foo: &'c str,
}
impl<'c> Foo<'c> {
fn use_in_another_thread<F>(self, mut cb: F) -> JoinHandle<Foo<'c>>
where F: FnOnce(&mut Foo),
F: Send
{
thread::spawn(move || {
cb(&mut self);
self
})
}
}
fn main() {}
As far as I can see the lifetimes are sound, but I'm getting errors...
error[E0477]: the type `[closure@src/main.rs:12:23: 15:10 cb:F, self:Foo<'c>]` does not fulfill the required lifetime
--> src/main.rs:12:9
|
12 | thread::spawn(move || {
| ^^^^^^^^^^^^^
|
= note: type must outlive the static lifetime
error[E0495]: cannot infer an appropriate lifetime due to conflicting requirements
--> src/main.rs:14:13
|
14 | self
| ^^^^
|
note: first, the lifetime cannot outlive the lifetime 'c as defined on the body at 11:4...
--> src/main.rs:11:5
|
11 | {
| _____^ starting here...
12 | | thread::spawn(move || {
13 | | cb(&mut self);
14 | | self
15 | | })
16 | | }
| |_____^ ...ending here
note: ...so that expression is assignable (expected std::thread::JoinHandle<Foo<'c>>, found std::thread::JoinHandle<Foo<'_>>)
--> src/main.rs:12:9
|
12 | thread::spawn(move || {
| _________^ starting here...
13 | | cb(&mut self);
14 | | self
15 | | })
| |__________^ ...ending here
= note: but, the lifetime must be valid for the static lifetime...
note: ...so that the type `Foo<'_>` will meet its required lifetime bounds
--> src/main.rs:12:9
|
12 | thread::spawn(move || {
| ^^^^^^^^^^^^^
I don't understand what lifetimes the errors are referring to - the lifetime of the closure body? - or why they must outlive the static lifetime.