diff --git a/CHANGELOG.md b/CHANGELOG.md index 99aa2e2e..8035422f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,11 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Changed ### Fixed + +- Fixed using `#[automock]` with `#[tracing::instrument]`. The mock function + won't be instrumented, but at least it will compile. + ([#256](https://github.com/asomers/mockall/pull/256)) + ### Removed ## [0.9.0] - 2020-12-21 diff --git a/mockall/Cargo.toml b/mockall/Cargo.toml index f2b9506c..49801fab 100644 --- a/mockall/Cargo.toml +++ b/mockall/Cargo.toml @@ -49,6 +49,7 @@ mockall_double = { version = "^0.2.0", path = "../mockall_double" } serde = "1.0" serde_derive = "1.0" serde_json = "1.0" +tracing = "0.1.23" [[example]] name = "serde" diff --git a/mockall/tests/automock_instrument.rs b/mockall/tests/automock_instrument.rs new file mode 100644 index 00000000..4f210ff9 --- /dev/null +++ b/mockall/tests/automock_instrument.rs @@ -0,0 +1,22 @@ +// vim: tw=80 +//! A trait that uses tracing::instrument should be automockable. The mock +//! method won't be instrumented, though. +#![deny(warnings)] + +use mockall::*; +use tracing::instrument; + +#[derive(Debug)] +pub struct Foo {} + +#[automock] +impl Foo { + #[instrument] + fn foo(&self) {} + #[instrument] + fn bar() {} + #[tracing::instrument] + fn fooz(&self) {} + #[tracing::instrument] + fn barz() {} +} diff --git a/mockall_derive/src/lib.rs b/mockall_derive/src/lib.rs index 57d44ba0..e13bf534 100644 --- a/mockall_derive/src/lib.rs +++ b/mockall_derive/src/lib.rs @@ -552,17 +552,23 @@ impl<'a> AttrFormatter<'a> { fn format(&mut self) -> Vec { self.attrs.iter() .cloned() - .filter(|attr| - ( self.doc || - attr.path.get_ident() - .map(|i| i != "doc") - .unwrap_or(false) - ) && (self.async_trait || - attr.path.get_ident() - .map(|i| i != "async_trait") - .unwrap_or(false) - ) - ).collect() + .filter(|attr| { + let i = attr.path.get_ident(); + if i.is_none() { + false + } else if *i.as_ref().unwrap() == "doc" { + self.doc + } else if *i.as_ref().unwrap() == "async_trait" { + self.async_trait + } else if *i.as_ref().unwrap() == "instrument" { + // We can't usefully instrument the mock method, so just + // ignore this attribute. + // https://docs.rs/tracing/0.1.23/tracing/attr.instrument.html + false + } else { + true + } + }).collect() } }