From 4979a1bdd938a0131105b142d0b32cdee9310d50 Mon Sep 17 00:00:00 2001 From: Jelte Fennema Date: Mon, 22 Jan 2024 21:58:31 +0100 Subject: [PATCH 1/2] Add #[inline] attribute to a few more methods To be clear the `#[inline]` does not hint that inlining is beneficial, but it does give the compiler the option to inline if the compiler things it would be beneficial. This starts adding the `#[inline]` attribute to: 1. `IsVariant`: it's expected that this is often beneficial since its body is tiny. 2. `Debug`: This is to stay in line with the `std` implementation of the `Debug` derive. https://github.com/rust-lang/rust/pull/117727 It also explicitely doesn't add the attribute to the methods of `Error`, since those are almost never called in hot code paths. Fixes #317 --- CHANGELOG.md | 2 ++ impl/src/error.rs | 4 ++++ impl/src/fmt/debug.rs | 1 + impl/src/is_variant.rs | 1 + 4 files changed, 8 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 81ce2aba..7cd31d83 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -80,6 +80,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Upgrade to `syn` 2.0. - The `Error` derive now works in nightly `no_std` environments when enabling `#![feature(error_in_core)]`. +- `#[inline]` attributes are added to `IsVariant` and `Debug` implementations. + ([#334](https://github.com/JelteF/derive_more/pull/334) ### Fixed diff --git a/impl/src/error.rs b/impl/src/error.rs index cc8e76f7..d513a2c4 100644 --- a/impl/src/error.rs +++ b/impl/src/error.rs @@ -37,6 +37,8 @@ pub fn expand( }; let source = source.map(|source| { + // not using #[inline] on purpose since this is almost never part of a + // hot codepath quote! { fn source(&self) -> Option<&(dyn ::derive_more::Error + 'static)> { use ::derive_more::__private::AsDynError; @@ -46,6 +48,8 @@ pub fn expand( }); let provide = provide.map(|provide| { + // not using #[inline] on purpose since this is almost never part of a + // hot codepath quote! { fn provide<'_request>(&'_request self, request: &mut ::derive_more::core::error::Request<'_request>) { #provide diff --git a/impl/src/fmt/debug.rs b/impl/src/fmt/debug.rs index 15d59bfc..b09c9c35 100644 --- a/impl/src/fmt/debug.rs +++ b/impl/src/fmt/debug.rs @@ -47,6 +47,7 @@ pub fn expand(input: &syn::DeriveInput, _: &str) -> syn::Result { Ok(quote! { #[automatically_derived] impl #impl_gens ::derive_more::Debug for #ident #ty_gens #where_clause { + #[inline] fn fmt( &self, __derive_more_f: &mut ::derive_more::core::fmt::Formatter<'_> ) -> ::derive_more::core::fmt::Result { diff --git a/impl/src/is_variant.rs b/impl/src/is_variant.rs index 123e3fb7..d667fc2e 100644 --- a/impl/src/is_variant.rs +++ b/impl/src/is_variant.rs @@ -44,6 +44,7 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result bool { match self { #enum_name ::#variant_ident #data_pattern => true, From 3c5fda0332613992a5c3d702898d45d3e520fab0 Mon Sep 17 00:00:00 2001 From: tyranron Date: Tue, 23 Jan 2024 13:19:34 +0200 Subject: [PATCH 2/2] Minor stuff --- CHANGELOG.md | 4 ++-- impl/src/error.rs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7cd31d83..270eee3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ([#298](https://github.com/JelteF/derive_more/pull/298)) - Add `TryFrom` derive for enums to convert from their discriminant. ([#300](https://github.com/JelteF/derive_more/pull/300)) +- `#[inline]` attributes to `IsVariant` and `Debug` implementations. + ([#334](https://github.com/JelteF/derive_more/pull/334) ### Changed @@ -80,8 +82,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - Upgrade to `syn` 2.0. - The `Error` derive now works in nightly `no_std` environments when enabling `#![feature(error_in_core)]`. -- `#[inline]` attributes are added to `IsVariant` and `Debug` implementations. - ([#334](https://github.com/JelteF/derive_more/pull/334) ### Fixed diff --git a/impl/src/error.rs b/impl/src/error.rs index d513a2c4..f02f90a4 100644 --- a/impl/src/error.rs +++ b/impl/src/error.rs @@ -37,8 +37,8 @@ pub fn expand( }; let source = source.map(|source| { - // not using #[inline] on purpose since this is almost never part of a - // hot codepath + // Not using `#[inline]` here on purpose, since this is almost never part + // of a hot codepath. quote! { fn source(&self) -> Option<&(dyn ::derive_more::Error + 'static)> { use ::derive_more::__private::AsDynError; @@ -48,8 +48,8 @@ pub fn expand( }); let provide = provide.map(|provide| { - // not using #[inline] on purpose since this is almost never part of a - // hot codepath + // Not using `#[inline]` here on purpose, since this is almost never part + // of a hot codepath. quote! { fn provide<'_request>(&'_request self, request: &mut ::derive_more::core::error::Request<'_request>) { #provide