Skip to content

Commit

Permalink
Inline generated functions (#389)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 authored Nov 17, 2024
1 parent 3479cbb commit e42916a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 0 deletions.
3 changes: 3 additions & 0 deletions strum_macros/src/macros/enum_discriminants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub fn enum_discriminants_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let (impl_generics, ty_generics, where_clause) = ast.generics.split_for_impl();
let impl_from = quote! {
impl #impl_generics ::core::convert::From< #name #ty_generics > for #discriminants_name #where_clause {
#[inline]
fn from(val: #name #ty_generics) -> #discriminants_name {
#from_fn_body
}
Expand All @@ -150,6 +151,7 @@ pub fn enum_discriminants_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

quote! {
impl #impl_generics ::core::convert::From< #enum_life #name #ty_generics > for #discriminants_name #where_clause {
#[inline]
fn from(val: #enum_life #name #ty_generics) -> #discriminants_name {
#from_fn_body
}
Expand All @@ -169,6 +171,7 @@ pub fn enum_discriminants_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
impl #impl_generics #strum_module_path::IntoDiscriminant for #name #ty_generics #where_clause {
type Discriminant = #discriminants_name;

#[inline]
fn discriminant(&self) -> Self::Discriminant {
<Self::Discriminant as ::core::convert::From<&Self>>::from(self)
}
Expand Down
8 changes: 8 additions & 0 deletions strum_macros/src/macros/enum_iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

impl #impl_generics #strum_module_path::IntoEnumIterator for #name #ty_generics #where_clause {
type Iterator = #iter_name #ty_generics;

#[inline]
fn iter() -> #iter_name #ty_generics {
#iter_name {
idx: 0,
Expand All @@ -112,15 +114,18 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
impl #impl_generics Iterator for #iter_name #ty_generics #where_clause {
type Item = #name #ty_generics;

#[inline]
fn next(&mut self) -> ::core::option::Option<<Self as Iterator>::Item> {
self.nth(0)
}

#[inline]
fn size_hint(&self) -> (usize, ::core::option::Option<usize>) {
let t = if self.idx + self.back_idx >= #variant_count { 0 } else { #variant_count - self.idx - self.back_idx };
(t, Some(t))
}

#[inline]
fn nth(&mut self, n: usize) -> ::core::option::Option<<Self as Iterator>::Item> {
let idx = self.idx + n + 1;
if idx + self.back_idx > #variant_count {
Expand All @@ -137,12 +142,14 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
}

impl #impl_generics ExactSizeIterator for #iter_name #ty_generics #where_clause {
#[inline]
fn len(&self) -> usize {
self.size_hint().0
}
}

impl #impl_generics DoubleEndedIterator for #iter_name #ty_generics #where_clause {
#[inline]
fn next_back(&mut self) -> ::core::option::Option<<Self as Iterator>::Item> {
let back_idx = self.back_idx + 1;

Expand All @@ -162,6 +169,7 @@ pub fn enum_iter_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
impl #impl_generics ::core::iter::FusedIterator for #iter_name #ty_generics #where_clause { }

impl #impl_generics Clone for #iter_name #ty_generics #where_clause {
#[inline]
fn clone(&self) -> #iter_name #ty_generics {
#iter_name {
idx: self.idx,
Expand Down
4 changes: 4 additions & 0 deletions strum_macros/src/macros/enum_messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,28 @@ pub fn enum_message_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

Ok(quote! {
impl #impl_generics #strum_module_path::EnumMessage for #name #ty_generics #where_clause {
#[inline]
fn get_message(&self) -> ::core::option::Option<&'static str> {
match self {
#(#arms),*
}
}

#[inline]
fn get_detailed_message(&self) -> ::core::option::Option<&'static str> {
match self {
#(#detailed_arms),*
}
}

#[inline]
fn get_documentation(&self) -> ::core::option::Option<&'static str> {
match self {
#(#documentation_arms),*
}
}

#[inline]
fn get_serializations(&self) -> &'static [&'static str] {
match self {
#(#serializations),*
Expand Down
1 change: 1 addition & 0 deletions strum_macros/src/macros/enum_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ pub fn enum_properties_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

Ok(quote! {
impl #impl_generics #strum_module_path::EnumProperty for #name #ty_generics #where_clause {
#[inline]
fn get_str(&self, prop: &str) -> ::core::option::Option<&'static str> {
match self {
#(#arms),*
Expand Down
7 changes: 7 additions & 0 deletions strum_macros/src/macros/enum_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ pub fn enum_table_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

impl<T> #table_name<T> {
#[doc = #doc_new]
#[inline]
#vis fn new(
#(#snake_idents: T,)*
) -> #table_name<T> {
Expand All @@ -142,13 +143,15 @@ pub fn enum_table_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
}

#[doc = #doc_closure]
#[inline]
#vis fn from_closure<F: Fn(#name)->T>(func: F) -> #table_name<T> {
#table_name {
#(#closure_fields)*
}
}

#[doc = #doc_transform]
#[inline]
#vis fn transform<U, F: Fn(#name, &T)->U>(&self, func: F) -> #table_name<U> {
#table_name {
#(#transform_fields)*
Expand All @@ -160,6 +163,7 @@ pub fn enum_table_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
impl<T> ::core::ops::Index<#name> for #table_name<T> {
type Output = T;

#[inline]
fn index(&self, idx: #name) -> &T {
match idx {
#(#get_matches)*
Expand All @@ -169,6 +173,7 @@ pub fn enum_table_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
}

impl<T> ::core::ops::IndexMut<#name> for #table_name<T> {
#[inline]
fn index_mut(&mut self, idx: #name) -> &mut T {
match idx {
#(#get_matches_mut)*
Expand All @@ -179,6 +184,7 @@ pub fn enum_table_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

impl<T> #table_name<::core::option::Option<T>> {
#[doc = #doc_option_all]
#[inline]
#vis fn all(self) -> ::core::option::Option<#table_name<T>> {
if let #table_name {
#(#snake_idents: ::core::option::Option::Some(#snake_idents),)*
Expand All @@ -194,6 +200,7 @@ pub fn enum_table_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {

impl<T, E> #table_name<::core::result::Result<T, E>> {
#[doc = #doc_result_all_ok]
#[inline]
#vis fn all_ok(self) -> ::core::result::Result<#table_name<T>, E> {
::core::result::Result::Ok(#table_name {
#(#snake_idents: self.#snake_idents?,)*
Expand Down
1 change: 1 addition & 0 deletions strum_macros/src/macros/from_repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub fn from_repr_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
#[allow(clippy::use_self)]
impl #impl_generics #name #ty_generics #where_clause {
#[doc = "Try to create [Self] from the raw representation"]
#[inline]
#vis #const_if_possible fn from_repr(discriminant: #discriminant_type) -> Option<#name #ty_generics> {
#(#constant_defs)*
match discriminant {
Expand Down
4 changes: 4 additions & 0 deletions strum_macros/src/macros/strings/as_ref_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn as_ref_str_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
let arms = get_arms(ast)?;
Ok(quote! {
impl #impl_generics ::core::convert::AsRef<str> for #name #ty_generics #where_clause {
#[inline]
fn as_ref(&self) -> &str {
match *self {
#(#arms),*
Expand Down Expand Up @@ -92,6 +93,7 @@ pub fn as_static_str_inner(
Ok(match trait_variant {
GenerateTraitVariant::AsStaticStr => quote! {
impl #impl_generics #strum_module_path::AsStaticRef<str> for #name #ty_generics #where_clause {
#[inline]
fn as_static(&self) -> &'static str {
match *self {
#(#arms),*
Expand All @@ -101,13 +103,15 @@ pub fn as_static_str_inner(
},
GenerateTraitVariant::From => quote! {
impl #impl_generics ::core::convert::From<#name #ty_generics> for &'static str #where_clause {
#[inline]
fn from(x: #name #ty_generics) -> &'static str {
match x {
#(#arms2),*
}
}
}
impl #impl_generics2 ::core::convert::From<&'_derivative_strum #name #ty_generics> for &'static str #where_clause {
#[inline]
fn from(x: &'_derivative_strum #name #ty_generics) -> &'static str {
match *x {
#(#arms3),*
Expand Down
4 changes: 4 additions & 0 deletions strum_macros/src/macros/strings/from_string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ pub fn from_string_inner(ast: &DeriveInput) -> syn::Result<TokenStream> {
#[allow(clippy::use_self)]
impl #impl_generics ::core::str::FromStr for #name #ty_generics #where_clause {
type Err = #strum_module_path::ParseError;

#[inline]
fn from_str(s: &str) -> ::core::result::Result< #name #ty_generics , <Self as ::core::str::FromStr>::Err> {
#phf_body
#standard_match_body
Expand Down Expand Up @@ -190,6 +192,8 @@ fn try_from_str(
#[allow(clippy::use_self)]
impl #impl_generics ::core::convert::TryFrom<&str> for #name #ty_generics #where_clause {
type Error = #strum_module_path::ParseError;

#[inline]
fn try_from(s: &str) -> ::core::result::Result< #name #ty_generics , <Self as ::core::convert::TryFrom<&str>>::Error> {
::core::str::FromStr::from_str(s)
}
Expand Down

0 comments on commit e42916a

Please sign in to comment.