My use case:
- I have a large complex struct.
- I want to take a snapshot of this struct to send it to a thread to do some calculation.
- Many large fields within this struct are not neccessary for calculation.
- Many fields within the struct are partially required (a field may be a struct and only a few parameters from this struct are required).
At the moment I simply call .clone()
and pass a clone of the entire struct to the thread.
It is difficult to give a good example, but this is a summary of my current method:
use tokio::task;
fn main() {
let compex_struct = ComplexStruct::new(...);
// some extra async stuff I don't think is 100% neccessary to this question
let future = async_function(compex_struct.clone()); // Ideally not cloning whole struct
// some extra async stuff I don't think is 100% neccessary to this question
}
fn async_function(complex_struct:ComplexStruct) -> task::JoinHandle<_> {
task::spawn_blocking(move || {
// bunch of work, then return something
})
}
My current working idea is to have a seperate struct such as ThreadData
which is instantiated with ThreadData::new(ComplexStruct)
and effectively clones the required fields. I then pass ThreadData
to the thread instead.
What is the best solution to this problem?