Conditional compilation for async keyword

I'm adding async support to my existed sync APIs. Thanks to async/await syntax I don't have to rewrite everything but simply add await and async keyword to some of my functions. This makes me thinking of using conditional compilation techniques to turn on async code by feature flag.

For await code, I can feature-gate them under a #[cfg(feature="async")] block. However, there seems to be no way to add async keyword to fn xxx() like this.

I wonder if any good practice for this?

There is a macro crate maybe_async. But it's async first which not work for my sync first APIs.

Generally speaking, you should not change function signatures based on a feature flag, because your crate might be used in multiple places in a larger project -- where they might disagree on which flavor they want. It's better to have separate functions that can coexist, or just commit to one way altogether.

2 Likes

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.