Repository navigation
Add Discriminant::unstable_cmp - #164231
bushrat011899 wants to merge 1 commit into
Conversation
|
rustbot has assigned @Mark-Simulacrum. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
efbd650 to
3dbf96a
Compare
| /// | ||
| /// // Inequivalent arms will compare not-equal | ||
| /// assert_ne!(discriminant(&Foo::A(1)).unstable_cmp(&discriminant(&Foo::B(1))), Ordering::Equal); | ||
| /// ``` |
There was a problem hiding this comment.
Is there a good way to make a non-example? Like
// !!! DONT DO THIS, this assert may or may not pass, no guarantees are provided
assert_eq!(b_discriminant.unstable_cmp(a_discriminant), Ordering::Less);(note: i intentionally used an assert that will not pass with the current obvious implementation)
I know we have some UB non-examples in the standard library, but is there a standard for other non-examples? and if so, would it be a good idea to call this out?
There was a problem hiding this comment.
Ok so what I've done is swapped the assertions to use assert!(matches!(...)) instead, since I think it's clearer to say assert!(matches!(..., Less | Greater)) than assert_ne!(..., Equal). To cover the non-example case, I've used a little hidden-line trickery to change the ordering of variants and write this as a should_panic example, so in the docs it'll get a nice red highlight saying it'll panic (only kind of a lie).
Provides an unstable ordering between `Discriminant<T>` values.
3dbf96a to
6b272b0
Compare
Tracking Issue: Not Yet Created
Supersedes: #106418
Zulip Discussion: #t-libs > Impl Partial/Ord for Discriminant
Based on this concern on the implementation of
Partial/OrdforDiscriminant, I'm proposing we instead offer anunstable_cmpfunction. This mirrors thefN::total_cmpfunction used to provideOrd-like functionality tof16/32/64/128without actually guaranteeing the type itself isOrd.Notes
unstable_cmphas been chosen to explicitly document at each usage that the ordering ofDiscriminantvalues is not stable and may change.const, but that requires either makingDiscriminantKind::Discriminant: const Ordor adding a fairly verbosewhere <T as DiscriminantKind>::Discriminant: [const] Ordtounstable_cmp. As such I'm deferring its inclusion. It would remain unstable untilconst_cmpis stabilized anyway.