#[derive(PartialOrd)] code generation

Hello Rustaceans,

I wonder if there is a possibility to get a look at the code 'generated' by the #[derive(PartialOrd)] attribute.

I have the following struct :

#[derive(PartialEq, PartialOrd)]
pub struct BigUInt
{
    bits: [u64; NB64BLOC]
}

The PartialEq automatic code is probably straightforward and performs array member equality/inequality sequential tests, but the PartialOrd is different, and I just can't figure out how comparison is handled in automatic code. Are there any documentation on how this code is generated, and on which types those derivations can be applied ?

Thanks !

[As for my previous posts, this is a learning purpose project.]

1 Like

The derived code compares the first field in the struct, and breaks ties by comparing the second, and then the third, etc. This is called lexicographic ordering.

Tuples, vectors, and arrays use the same scheme, but arrays have to have 32 elements or less due to a technical limitation in the compiler.

1 Like

Sure, it’s just macro-expansion. There’s a tool for it and an unstable compiler option.

2 Likes

I suppose array are compared element by element, but what if first elements are different ? only the first get compared ?

Thanks for signaling such a tool. I gonna have a look !

I guess, you could always just try it out yourself.

2 Likes

Yes, that's it. I should think more often to the Rust Playground.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.