Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[OTHER] Inconsistent #[inline] attributes in ToLexical-like and FromLexical-like methods. #145

Closed
zheland opened this issue Sep 16, 2024 · 3 comments · Fixed by #151
Closed
Assignees
Labels
bug Something isn't working good first issue Good for newcomers
Milestone

Comments

@zheland
Copy link
Contributor

zheland commented Sep 16, 2024

Description

In general, there is some inconsistency in attributes in ToLexical-like and FromLexical-like implementations.
For example unsigned ints ToLexial implementations lack #[inline] attributes so they do not inlined across crates, while implementations for signed ints have these attributes.

This problem could probably be considered a bug.

Currently

macro trait inline attr $(#[$meta:meta])?
unsigned_to_lexical ToLexical no yes
unsigned_to_lexical ToLexicalWithOptions no yes
signed_to_lexical ToLexical #[inline] yes
signed_to_lexical ToLexicalWithOptions #[inline] duplicated
float_to_lexical ToLexical no yes
float_to_lexical ToLexicalWithOptions no yes
integer_from_lexical FromLexical #[cfg_attr(not(feature = "compact"), inline)] yes
integer_from_lexical FromLexicalWithOptions #[cfg_attr(not(feature = "compact"), inline)] yes
float_from_lexical FromLexical #[cfg_attr(not(feature = "compact"), inline)] no
float_from_lexical FromLexicalWithOptions #[cfg_attr(not(feature = "compact"), inline)] no

Expected

Either:

  • Same attributes for all implementations, probably: #[cfg_attr(not(feature = "compact"), inline)].
  • #[cfg_attr(not(feature = "compact"), inline)] for FromLexical-like and #[inline] for ToLexical-like.
  • Or the comments with the explanation for the inconsistency between attributes

Additionally

$(#[$meta:meta])? are not longer used. Maybe it is better to remove it as well instead of adding where it is missed?

Example diff

Example with all ToLexical and FromLexical methods using #[cfg_attr(not(feature = "compact"), inline)] and with $(#[$meta:meta])? removed.

diff --git a/lexical-parse-integer/src/api.rs b/lexical-parse-integer/src/api.rs
index 9240212..5cc08eb 100644
--- a/lexical-parse-integer/src/api.rs
+++ b/lexical-parse-integer/src/api.rs
@@ -17,5 +17,4 @@ use crate::parse::ParseInteger;
 macro_rules! integer_from_lexical {
-    ($($t:ident $unsigned:ident $(, #[$meta:meta])? ; )*) => ($(
+    ($($t:ident $unsigned:ident ; )*) => ($(
         impl FromLexical for $t {
-            $(#[$meta:meta])?
             #[cfg_attr(not(feature = "compact"), inline)]
@@ -26,3 +25,2 @@ macro_rules! integer_from_lexical {
 
-            $(#[$meta:meta])?
             #[cfg_attr(not(feature = "compact"), inline)]
@@ -39,3 +37,2 @@ macro_rules! integer_from_lexical {
 
-            $(#[$meta:meta])?
             #[cfg_attr(not(feature = "compact"), inline)]
@@ -53,3 +50,2 @@ macro_rules! integer_from_lexical {
 
-            $(#[$meta:meta])?
             #[cfg_attr(not(feature = "compact"), inline)]
diff --git a/lexical-write-float/src/api.rs b/lexical-write-float/src/api.rs
index 76c3356..f583618 100644
--- a/lexical-write-float/src/api.rs
+++ b/lexical-write-float/src/api.rs
@@ -20,5 +20,5 @@ const DEFAULT_OPTIONS: Options = Options::new();
 macro_rules! float_to_lexical {
-    ($($t:tt $(, #[$meta:meta])? ; )*) => ($(
+    ($($t:tt ; )*) => ($(
         impl ToLexical for $t {
-            $(#[$meta:meta])?
+            #[cfg_attr(not(feature = "compact"), inline)]
             fn to_lexical(self, bytes: &mut [u8])
@@ -33,3 +33,3 @@ macro_rules! float_to_lexical {
             type Options = Options;
-            $(#[$meta:meta])?
+            #[cfg_attr(not(feature = "compact"), inline)]
             fn to_lexical_with_options<'a, const FORMAT: u128>(
diff --git a/lexical-write-integer/src/api.rs b/lexical-write-integer/src/api.rs
index 5cc7f50..11d34d1 100644
--- a/lexical-write-integer/src/api.rs
+++ b/lexical-write-integer/src/api.rs
@@ -76,5 +76,5 @@ where
 macro_rules! unsigned_to_lexical {
-    ($($narrow:tt $wide:tt $(, #[$meta:meta])? ; )*) => ($(
+    ($($narrow:tt $wide:tt ; )*) => ($(
         impl ToLexical for $narrow {
-            $(#[$meta:meta])?
+            #[cfg_attr(not(feature = "compact"), inline)]
             fn to_lexical(self, bytes: &mut [u8])
@@ -90,3 +90,3 @@ macro_rules! unsigned_to_lexical {
 
-            $(#[$meta:meta])?
+            #[cfg_attr(not(feature = "compact"), inline)]
             fn to_lexical_with_options<'a, const FORMAT: u128>(
@@ -124,7 +124,5 @@ unsigned_to_lexical! { usize u64 ; }
 macro_rules! signed_to_lexical {
-    ($($narrow:tt $wide:tt $unsigned:tt $(, #[$meta:meta])? ; )*) => ($(
+    ($($narrow:tt $wide:tt $unsigned:tt ; )*) => ($(
         impl ToLexical for $narrow {
-            $(#[$meta:meta])?
-            $(#[$meta:meta])?
-            #[inline]
+            #[cfg_attr(not(feature = "compact"), inline)]
             fn to_lexical(self, bytes: &mut [u8])
@@ -139,4 +137,3 @@ macro_rules! signed_to_lexical {
             type Options = Options;
-            $(#[$meta:meta])?
-            #[inline]
+            #[cfg_attr(not(feature = "compact"), inline)]
             fn to_lexical_with_options<'a, const FORMAT: u128>(
@Alexhuszagh Alexhuszagh added good first issue Good for newcomers bug Something isn't working labels Sep 16, 2024
@Alexhuszagh
Copy link
Owner

This was likely introduced with the formatting API which added like many, many different layers of abstraction that needed to be removed at compilation when not used, causing inconsistency with inlining. Ideally, there should be soft inlining, at best, at API boundaries and only have the inlining internally within the crate.

@Alexhuszagh Alexhuszagh added this to the 1.0.2 milestone Sep 16, 2024
@Alexhuszagh
Copy link
Owner

Alexhuszagh commented Sep 16, 2024

I submitted a PR @zheland but this is all your work so if you'd like to submit it I'd be more than happy to close mine and accept it. All this lgtm. Thank you for this.

@zheland
Copy link
Contributor Author

zheland commented Sep 17, 2024

Uh, okay, done :-) #151

I also checked other pub functions and methods with clippy lint missing_inline_in_public_items and haven't find any other missing inline for publicly visible functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
2 participants