-
-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Labels
C-enhancementCategory: A new feature or an improvement for an existing oneCategory: A new feature or an improvement for an existing one
Description
Context
When I put tracing::instrument on the method in the extension trait I get a compile error
use tracing::instrument;
use easy_ext::ext;
#[ext]
impl String {
#[instrument(skip_all)]
fn foo(&self) -> u32 {
42
}
}Compile error:
error[E0308]: mismatched types
--> src/lib.rs:5:5
|
5 | #[instrument(skip_all)]
| ^^^^^^^^^^^^^^^^^^^^^^^ expected `u32`, found `()`
|
= note: this error originates in the attribute macro `instrument` (in Nightly builds, run with -Z macro-backtrace for more info)
This is because #[instrument] must not be used on the trait method declaration. The same compile error is reproduced with this code:
use tracing::instrument;
trait StringExt {
#[instrument(skip_all)]
fn foo(&self) -> u32;
}Summary
I propose to make it possible to specify with attributes to put on the trait declaration and on trait implementation. We may leave the default behavior of duplicating the attributes both on the trait declaration and implementation. This won't be a breaking change.
It could look like this. for example, in the most verbose form, with optional configs trait_only/impl_only:
#[ext(
TraitName,
trait_only(trait_attr_foo, trait_attr_bar),
impl_only(impl_attr_foo, impl_attr_bar)
)]
impl String {
#[ext(
impl_only(instrument(skip_all), impl_attr_baz, impl_attr_bruh),
trait_only(trait_attr_baz, trait_attr_bruh)
)]
fn foo(&self) -> u32 {
42
}
}This will generate:
#[trait_attr_foo]
#[trait_attr_bar]
trait TraitName {
#[trait_attr_baz]
#[trait_attr_bruh]
fn foo(&self) -> u32;
)
#[impl_attr_foo]
#[impl_attr_bar]
impl TraitName for String {
#[instrument(skip_all)]
#[impl_attr_baz]
#[impl_attr_bruh]
fn foo(&self) -> u32 {
42
}
}shepmaster and Chaoses-Ib
Metadata
Metadata
Assignees
Labels
C-enhancementCategory: A new feature or an improvement for an existing oneCategory: A new feature or an improvement for an existing one