Maybe Implements
Maybe Implements provides Rust procedural macro attributes that can be used to optionally implement traits.
Optional Traits in Action
Technically, the provided attribute always implements the specified traits. It can be combined with the
existing cfg_attr
attribute to conditionally implement one or more traits. The primary use case for such
behavior is optionally implementing Send
and Sync
.
First, consider that asynchronous behavior is an optional feature in your crate:
[]
= ["maybe-impl"]
[]
= { = "0.1.0", = true }
Consider the crate has the following trait:
When the crate is used with the default features, there is no change in behavior. When the async
feature is enabled:
The trait is expanded to:
Using Generics
If you define a trait with generics that requires Send
and/or Sync
, specifically, that will require the generic type constraint to match. Trait aliases are currently unstable, however, you can bridge these two concepts with a pseudo trait alias by defining a marker trait with a blanket implementation.
The default, synchronous build remains vanilla, while the asynchronous path expands to the equivalent of:
License
This project is licensed under the MIT license.