Why Does this Need 'static When it Should Be Owned?

I've got this code and I'm trying to have the thread I'm spawning take ownership if the arguments to this function, but for some reason, even though it isn't a reference, it is complaining about needing 'static lifetime when I try to rayon::spawn:

vagrant@ubuntu-bionic:~/git/katharostech-github/cast2gif$ cargo run --release --features cli -- -f lucky.gitignore.cast test.gitignore.gif
   Compiling cast2gif v0.1.0 (/home/vagrant/git/katharostech-github/cast2gif)
error[E0310]: the parameter type `C` may not live long enough
   --> src/lib.rs:134:5
    |
119 | pub fn convert_to_gif_with_progress<R, W, C>(
    |                                           - help: consider adding an explicit lifetime bound `C: 'static`...
...
134 |     rayon::spawn(move || progress_thread(progress_receiver, update_progress));
    |     ^^^^^^^^^^^^
    |
note: ...so that the type `[closure@src/lib.rs:134:18: 134:77 progress_receiver:flume::Receiver<types::ProgressCmd>, update_progress:C]` will meet its required lifetime bounds
   --> src/lib.rs:134:5
    |
134 |     rayon::spawn(move || progress_thread(progress_receiver, update_progress));
    |     ^^^^^^^^^^^^

I think that C should be an owned CastProgressHandler that should then be moveed into the thread.

C: 'static means that C does not contain any non-static references. Basically it means that C owns all its data, and is not restricted to any local stack frame.

6 Likes

Got it. That makes sense. Because even though it ownes C, C itself could be something like MyStruct<'a> and contain a reference to something else. 'static protects against this.

3 Likes

Minor adendum: C could not only hold a reference but can be a reference itself as &'a T is just a type and different from the type T.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.