Derive macros for Eq, Ord, PartialEq and PartialOrd makes unecessary restrictions

Consider the following code:

#[derive(Debug)]
pub struct Monomial<O: ?Sized, I, P> {
    // Product is sorted in decreasing order of id:
    product: Vec<VariablePower<I, P>>,
    total_power: P,
    _phantom_ordering: PhantomData<O>,
}

impl<O, I: PartialEq, P: PartialEq> PartialEq for Monomial<O, I, P> { /* ... */ }
impl<O, I: Eq, P: Eq> Eq for Monomial<O, I, P> {}
impl<O: MyOrdering, I: Ord, P: Ord> Ord for Monomial<O, I, P> { /* ... */}
impl<O: MyOrdering, I: Id, P: Power> PartialOrd for Monomial<O, I, P> { /* ... */}

#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
struct Signature<O: Ordering, I: Id, P: Power> {
    idx: u32,
    monomial: Monomial<O, I, P>,
}

The derive macro for Eq, Ord, PartialEq and PartialOrd fails to implement those traits to Signature, even if each individual field implements all those traits. The problem is that the macro expects O to implement Eq + Ord, but that is not necessary for the field using O to also implement Eq + Ord.

Could the macros be made more generic, and not make unnecessary restrictions to the generic parameters? Or that is the best that can be done with the current macro system?

1 Like

This is a known problem. Search for "perfect derive".

3 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.