Skip to content

Commit

Permalink
Add #[inline] attribute to a few more methods (#334, #317)
Browse files Browse the repository at this point in the history
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. rust-lang/rust#117727

It also explicitly doesn't add the attribute to the methods of `Error`,
since those are almost never called in hot code paths.
  • Loading branch information
JelteF committed Jan 23, 2024
1 parent 7aaa55c commit a7116ad
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions impl/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions impl/src/fmt/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub fn expand(input: &syn::DeriveInput, _: &str) -> syn::Result<TokenStream> {
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 {
Expand Down
1 change: 1 addition & 0 deletions impl/src/is_variant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub fn expand(input: &DeriveInput, trait_name: &'static str) -> Result<TokenStre
#[doc = "Returns `true` if this value is of type `"]
#[doc = #variant_name]
#[doc = "`. Returns `false` otherwise"]
#[inline]
pub const fn #fn_name(&self) -> bool {
match self {
#enum_name ::#variant_ident #data_pattern => true,
Expand Down

0 comments on commit a7116ad

Please sign in to comment.