Skip to content

Feature request: conditional compilation #18

@coriolinus

Description

@coriolinus

I'm maintaining a library for which we want to expose both sync and async versions, but not necessarily at the same time. For both compilation speed and crate size, we'd prefer to allow users to enable one or both of these versions independently.

I don't think this feature exists yet. If it does, then simply adding relevant documentation would be great! If it doesn't yet exist, I imagine it to look like this:

#[async_generic(sync_cfg(feature = "sync"), async_cfg(feature = "async"))]
fn foo() {}

Which expands to

#[cfg(feature = "sync")]
fn foo() {}

#[cfg(feature = "async")]
async fn foo_async() {}

This implies the following rules:

  • The #[cfg()] attribute only appears if the relevant sync_cfg or async_cfg attribute is in the macro invocation
  • Anything which is valid in a #[cfg()] attribute can go into the sync_cfg or async_cfg space

This allows for conditional compilation based on things other than raw features. For example, you might have

#[async_generic(async_cfg(any(target_family = "wasm", feature = "docs")))]
fn bar() {}

which expands to

fn bar() {}

#[cfg(any(target_family = "wasm", feature = "docs"))]
async fn bar_async() {}

If this feature does not exist, would you be positively inclined toward a PR adding it?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions