I have a rust struct that owns a cache of fetched robots.txt instances and a function to check whether my bot is allowed to crawl an url:
pub fn check(&mut self, url: &Url, fetcher: &mut Fetcher) -> Result<CheckResult>
The Fetcher at the moment is a blocking implementation that gets called if the robots.txt has not yet been fetched. Now I want to make the Fetcher abstract so that the check function works with an async Fetcher or a blocking one and does not care whether the application uses ureq (blocking) or reqwest (async).
However I'm afraid that this is not possible in Rust?
Theoretically it should not matter for the check function whether the crawler is async or blocking: No request is allowed for the checked domain until we know the robots.txt.
tokio::task::spawn_blocking(|| blocking_code_here())to integrate it with the async ecosystem.Fetcheras an argument, but already fetched results. And let the caller ofcheckfunction worry how to perform actual fetching.tokio, you could simply convert theasyncfetcher to a blocking one usingfutures::executor::block_on().