Consider the following code:
use std::error::Error;
use tokio::sync::mpsc;
fn gen_data() -> Result<i32, Box<dyn Error>> {
Ok(42)
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let (tx, rx) = mpsc::channel(10);
// TODO: future cannot be sent between threads safely the trait `Send` is not implemented for `dyn std::error::Error`
tokio::spawn(async move {
if let Ok(data) = gen_data() {
tx.send(data).await;
}
});
while let Some(res) = rx.recv().await {
println!("{}", res);
}
Ok(())
}
I have a general sense of what the error is trying to say. Is it saying the Err result of my gen_data function is not being dropped before I'm awaiting?
I do not need the error data from my function. The logic I'm trying to achieve is to do nothing if the gen_data returned an error, if gen_data works, then send it.
How can I refactor the code to get it to compile?