From a7116ada498808989b62cae863f91bb73fc93d5b Mon Sep 17 00:00:00 2001 From: Jelte Fennema-Nio Date: Tue, 23 Jan 2024 12:26:50 +0100 Subject: [PATCH] Add `#[inline]` attribute to a few more methods (#334, #317) 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 explicitly doesn't add the attribute to the methods of `Error`, since those are almost never called in hot code paths. --- 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..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 diff --git a/impl/src/error.rs b/impl/src/error.rs index cc8e76f7..f02f90a4 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]` 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; @@ -46,6 +48,8 @@ pub fn expand( }); let provide = provide.map(|provide| { + // 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 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,