I got this piece of code that won’t compile.
[dependencies] tokio = {version = "1.37.0", features = ["full"]}
use std::error::Error;
async fn external_function() -> Result<i32, Box<dyn Error>>{
println!("This is an external function!");
Ok(1)
}
async fn my_function(something: i32) {
println!("This is my function {something}");
}
#[tokio::main]
async fn main() {
println!("Hello, world!");
let runtime = tokio::runtime::Runtime::new().unwrap();
runtime.spawn(async move {
let v = external_function().await;
if let Ok(x) = v {
my_function(x).await;
}
});
}
The issue is that I got an async function from a crate that returns a Result<T, Box<dyn Error>>
I want to process these results, and if it’s Ok()
call my function with the resulting value.
But I got this compiler error: error: future cannot be sent between threads safely
What would be the best practice to implement this without errors, given that I cannot modify the external_function()
?
One solution would be to make my_function()
non async, but what if I’d want that to be async as well?