Hello Rustaceans,
I need to send a struct with a lifetime to a thread. I'd rather use some standard options instead of some crate solutions, if possible of course
Here follows one example:
If I go this:
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};
use std::thread;
struct SomeStruct<'a> {
phantom: PhantomData<&'a ()>,
}
struct UpperStruct<'a> {
somearc: Arc<Mutex<SomeStruct<'a>>>,
}
impl<'a> UpperStruct<'a> {
fn another_method(&mut self) {
let data = Arc::clone(&self.somearc);
thread::spawn(move || {
let mut data = data.lock().unwrap();
});
}
}
fn main() {}
I receive that:
error[E0477]: the type `[closure@src/main.rs:16:23: 18:10 data:std::sync::Arc<std::sync::Mutex<SomeStruct<'a>>>]` does not fulfill the required lifetime
--> src/main.rs:16:9
|
16 | thread::spawn(move || {
| ^^^^^^^^^^^^^
|
= note: type must satisfy the static lifetime
Naturally, the Arc usage without lifetime works normally
use std::marker::PhantomData;
use std::sync::{Arc, Mutex};
use std::thread;
struct SomeStruct<'a> {
phantom: PhantomData<&'a ()>,
}
struct UpperStruct<'a> {
//somearc: Arc<Mutex<SomeStruct<'a>>>,
somearc: Arc<Mutex<bool>>,
phantom: PhantomData<&'a ()>,
}
impl<'a> UpperStruct<'a> {
fn another_method(&mut self) {
let data = Arc::clone(&self.somearc);
thread::spawn(move || {
let mut data = data.lock().unwrap();
});
}
}
fn main() {}