Safer sort partition by kornelski · Pull Request #147378 · rust-lang/rust (original) (raw)

I mean only is_less can trigger panic_on_ord_violation under assumption that the implementation itself isn't buggy. is_less is used indirectly via partition_lomuto_branchless_cyclic, which is non-trivial and could be buggy itself, so there's never going to be a case that is 100% certain. But it could be helpful in practice.

The additional checks piggyback on returning None, because that's the simplest implementation. Distinguishing between checks in partition would require a Result with a custom Error type and/or moving checks around. That's more code, potentially more branches, and I doubt it's worth it. I could add more debug_asserts?

I still think it's an improvement over the current version of partition, where if len == 0 case happened, it could make partition_at_index_loop just return without reporting any issue, while failures most likely caused by is_less just abort() without explaining why.