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

chore: remove derivative dep #34

Merged
merged 3 commits into from
Nov 15, 2024

Conversation

mattsse
Copy link
Contributor

@mattsse mattsse commented Nov 10, 2024

there's an advisory for derivative https://rustsec.org/advisories/RUSTSEC-2024-0388

replaces derivative dep with the expanded code (omits phantom field, because noop)

expanded code on main:

    #[allow(unused_qualifications)]
    impl<T> ::std::cmp::Eq for Bitfield<T> {}
    #[allow(unused_qualifications)]
    impl<T> ::std::hash::Hash for Bitfield<T> {
        fn hash<__HT>(&self, __state: &mut __HT)
        where
            __HT: ::std::hash::Hasher,
        {
            match *self {
                Bitfield {
                    bytes: ref __arg_0,
                    len: ref __arg_1,
                    _phantom: ref __arg_2,
                } => {
                    ::std::hash::Hash::hash(&(*__arg_0), __state);
                    ::std::hash::Hash::hash(&(*__arg_1), __state);
                    ::std::hash::Hash::hash(&(*__arg_2), __state);
                }
            }
        }
    }
    #[allow(unused_qualifications)]
    #[allow(clippy::unneeded_field_pattern)]
    impl<T> ::std::cmp::PartialEq for Bitfield<T> {
        fn eq(&self, other: &Self) -> bool {
            true
                && match *self {
                    Bitfield {
                        bytes: ref __self_0,
                        len: ref __self_1,
                        _phantom: ref __self_2,
                    } => {
                        match *other {
                            Bitfield {
                                bytes: ref __other_0,
                                len: ref __other_1,
                                _phantom: ref __other_2,
                            } => {
                                true && &(*__self_0) == &(*__other_0)
                                    && &(*__self_1) == &(*__other_1)
                                    && &(*__self_2) == &(*__other_2)
                            }
                        }
                    }
                }
        }
    }

@mattsse mattsse changed the title chore: remove derivate dep chore: remove derivative dep Nov 10, 2024
@michaelsproul
Copy link
Member

I would prefer to switch to one of the alternatives as we also use derivative extensively in Lighthouse

Have you used derive_more or educe?

@michaelsproul
Copy link
Member

I think I prefer the look of derive_more. And it's already part of Lighthouse's dep tree.

@michaelsproul
Copy link
Member

Ah, didn't realise that derive_more doesn't allow customisation of PartialEq, etc. I guess this means we go with educe.

@mattsse
Copy link
Contributor Author

mattsse commented Nov 12, 2024

right, looks like derive_more doesn't support custom bounds.

although the tradeoff here is +~15 lines vs an additional dep

@michaelsproul
Copy link
Member

I think Rust's derive has actually gotten smarter and we don't need any custom implementations at all.

cargo expand for just straight derive shows:

    impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for Bitfield<T> {
        #[inline]
        fn eq(&self, other: &Bitfield<T>) -> bool {
            self.bytes == other.bytes && self.len == other.len
                && self._phantom == other._phantom
        }
    }
    
    impl<T: ::core::hash::Hash> ::core::hash::Hash for Bitfield<T> {
        #[inline]
        fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
            ::core::hash::Hash::hash(&self.bytes, state);
            ::core::hash::Hash::hash(&self.len, state);
            ::core::hash::Hash::hash(&self._phantom, state)
        }
    }

Which is perfect. No extra bounds.

Copy link

codecov bot commented Nov 15, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 77.76%. Comparing base (545f25a) to head (293ca9f).
Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main      #34   +/-   ##
=======================================
  Coverage   77.76%   77.76%           
=======================================
  Files          12       12           
  Lines         715      715           
=======================================
  Hits          556      556           
  Misses        159      159           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@michaelsproul michaelsproul merged commit a751223 into sigp:main Nov 15, 2024
7 checks passed
@DaniPopes
Copy link

DaniPopes commented Nov 18, 2024

I think Rust's derive has actually gotten smarter and we don't need any custom implementations at all.

cargo expand for just straight derive shows:

    impl<T: ::core::cmp::PartialEq> ::core::cmp::PartialEq for Bitfield<T> {
        #[inline]
        fn eq(&self, other: &Bitfield<T>) -> bool {
            self.bytes == other.bytes && self.len == other.len
                && self._phantom == other._phantom
        }
    }
    
    impl<T: ::core::hash::Hash> ::core::hash::Hash for Bitfield<T> {
        #[inline]
        fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) -> () {
            ::core::hash::Hash::hash(&self.bytes, state);
            ::core::hash::Hash::hash(&self.len, state);
            ::core::hash::Hash::hash(&self._phantom, state)
        }
    }

Which is perfect. No extra bounds.

There are still bounds with the builtin derive (impl<T: Trait> Trait for BitField<T>), these are unnecessary because the implementation for T is not used as it's only stored in a PhantomData.

Changing to builtin derives is a breaking change; I don't know the impact or if this is acceptable for you, just a heads up

@michaelsproul
Copy link
Member

Oh damn I totally read that wrong

@michaelsproul
Copy link
Member

Fixing here:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants