Hey, I am trying to write a dyn trait which has two functions inside that are async and two sync ones.
They look like this:
pub trait Provider {
fn id(&self) -> &str;
fn name(&self) -> &str;
async fn fetch_data(&self, game_id: &str) -> Result<ApiData, MinesweeperError>;
async fn fetch_name(&self, uuid: &str) -> Result<PlayerData, MinesweeperError>;
}
later in the code, I then want to select the appropriate provider based on the ID and run the async fetch functions:
let possible_providers: Vec<&dyn Provider> = vec![&GreevProvider, &McPlayHdProvider];
let optional_provider = possible_providers
.iter()
.find(|x| x.id() == provider.as_str());
let Some(provider) = optional_provider else {
return Response::error("Unknown Provider", 400);
};
let result_api_data = provider.fetch_data(game_id).await;
But when compiling, I get this error:
the trait `Provider` cannot be made into an object
Full error: https://pastebin.com/raw/SeebEGqN
And honestly, I have no clue on what exactly I would have to do different, to get something like I intended to have. The compiler tells me to move my async functions into a different trait and implement an enum. However, I don't exactly know what that would solve or better said how that would solve my issue.