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

Reduce post-macro-expansion code in integer deserialize impls #1965

Merged
merged 1 commit into from Jan 25, 2021
Merged

Conversation

dtolnay
Copy link
Member

@dtolnay dtolnay commented Jan 25, 2021

This new implementation is less than half the code (+187, -389) and avoids creating work for the optimizer in quite a lot of places.

For example compare what happens when deserializing an i8 via visit_i8. Before:

            #[inline]
            fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
            where
                E: Error,
            {
                match FromPrimitive::from_i8(v) {
                    Some(v) => Ok(v),
                    None => Err(Error::invalid_value(Unexpected::Signed(v as i64), &self)),
                }
            }
...
    impl FromPrimitive for i8 {
        #[inline]
        fn from_i8(n: i8) -> Option<Self> {
            if i8::min_value() as i64 <= n as i64 && n as i64 <= i8::max_value() as i64 {
                Some(n as i8)
            } else {
                None
            }
        }
        ...
    }

After:

            #[inline]
            fn visit_i8<E>(self, v: i8) -> Result<Self::Value, E>
            where
                E: Error,
            {
                Ok(v)
            }

@dtolnay dtolnay merged commit deaf600 into master Jan 25, 2021
@dtolnay dtolnay deleted the int branch January 25, 2021 02:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant