::fix_hidden_lifetime_bug
Are you getting one of the following errors (E700)?
-
error: hidden type for `+ Sized -
error: hidden type for `async -
error: hidden type for `async
Then you can add the attribute provided by this crate to automagically generate an equivalent signature that soothes such a grumpy compiler 🙃
-
See the lifetime bug
asyncissue, as well as this other comment for more info.The fix is thus to perform the unsugaring from an
async fnto anfnyielding aFuture, and then just adding the necessary+ Captures<'_>bounds. -
See also this post where I explain the issue more in detail.
Usage
-
cargo add fix_hidden_lifetime_bug, or add the following to yourCargo.tomlfile:[] = "x.y.z"- where you can find the version using
cargo search fix_hidden_lifetime_bug
- where you can find the version using
-
Add the following to your
lib.rsfile:extern crate fix_hidden_lifetime_bug; -
Slap a
#[fix_hidden_lifetime_bug]on the problematic function:// <-- Add this! + Sized// <-- Add this! async
Extra features
-
Full support for methods
In the case of methods, the
Selftype may be hiding lifetime parameters on its own, in which case a macro annotation on the method alone may not have enough syntactical information to generate the fix:use fix_hidden_lifetime_bug; ) , );In that case, the fix is to also decorate the whole
implblock with the attribute:use fix_hidden_lifetime_bug; ) , );
-
Displaying the expansions
By enabling the
"showme"Cargo feature:[] = "x.y.z" = ["showme"]you can then feed a
showmeparameter to specific#[fix_hidden_lifetime_params]annotations, as follows:use fix_hidden_lifetime_params; asyncoutputs:
+ Future + Captures + Captures + Captures where &'static : '__async_fut, &'_0 : '__async_fut, &'_1 : '__async_fut,This will make the attribute display the result of its expansion (and its expansion only! Hence yielding output that is way more readable than that from
cargo expandor other such tools), basically showing you how to manually fix a given function signature if you so wish (e.g., to avoid depending on proc-macro processing every time the annotated function is compiled, or to make the life easier for IDEs).Should you fix the signature, you may then be interested in:
-
Optional
opt-outof the magic proc-macro attributeIf you don't want to have to recompile each time the proc-macro able to fix function signatures for you (e.g., you rather want it to show you how to fix the signature so that you can do it through exclusive usage of
+ Captures<'…>additions), so as not to have to pay the proc-macro compilation time each time you compile from scratch, then you can opt out of it by disabling thedefault-featuresof the crate: this will disable theproc-macrosfeatures, which is the one that brings it to the table.That way, you can still use this then very lightweight crate just for its
Captures<'…>(and maybeMentionsTy<…>) definitions, and the documentation that goes with it![] … = "x.y.z" = false