1

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.

4
  • 1
    You can make the check interface async, and in the blocking implementation call tokio::task::spawn_blocking(|| blocking_code_here()) to integrate it with the async ecosystem. Commented Feb 21 at 13:46
  • On the other hand you can keep sync interface and use actor model to have an async version of fetcher, and bridge those two with channel that supports both sync and async reads and writes (like for example tokio channels). Commented Feb 21 at 14:27
  • And as a third way you can use a sans-io approach. Don't pass a Fetcher as an argument, but already fetched results. And let the caller of check function worry how to perform actual fetching. Commented Feb 21 at 14:31
  • If you do not use a reactor like tokio, you could simply convert the async fetcher to a blocking one using futures::executor::block_on(). Commented Feb 22 at 23:16

1 Answer 1

0

It seems this is not possible and this apparently inactive initiative from 2022 wanted to make it possible:

https://blog.rust-lang.org/inside-rust/2022/07/27/keyword-generics.html

There is https://docs.rs/maybe-async/0.2.6/maybe_async but it is not important enough for me to pull in another dependency.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.