Skip to content

Introduce trait for (forced) total ordering of numbers - #381

Open
fogti wants to merge 1 commit into
rust-num:mainfrom
fogti:total-ord
Open

Introduce trait for (forced) total ordering of numbers#381
fogti wants to merge 1 commit into
rust-num:mainfrom
fogti:total-ord

Conversation

@fogti

@fogti fogti commented Aug 15, 2026

Copy link
Copy Markdown

@cuviper

cuviper commented Aug 15, 2026

Copy link
Copy Markdown
Member

We already have this, but for floats only:
https://docs.rs/num-traits/latest/num_traits/float/trait.TotalOrder.html

Can you say more about why you want the same trait for integers?

Comment thread src/ops/total_ord.rs Outdated
@fogti

fogti commented Aug 16, 2026

Copy link
Copy Markdown
Author

Mostly to be able to treat float numbers and integers uniformly in containers that don't really care about special float values and just need an arbitrary, but deterministic ordering for them, optimally to shift the burden away from the user of having to wrap float values in a wrapper that forwards Ord::cmp to f_::total_cmp.

In particularly, this applies to containers like priority queues or R-trees.

Comment thread src/ops/total_order.rs
@fogti
fogti force-pushed the total-ord branch 2 times, most recently from 099ecae to 09e60fc Compare August 17, 2026 23:29
Comment thread src/ops/total_order.rs
macro_rules! totalorder_impl_zeroable_via_ord_core {
($($t:ty),* $(,)?) => {
$(
impl TotalOrder for NonZero<$t> {

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, only remaining problem afaik is that NonZero requires a bump of MSRV to 1.79

@fogti
fogti force-pushed the total-ord branch 3 times, most recently from a195803 to 9e5bd02 Compare August 17, 2026 23:41
@fogti

fogti commented Aug 17, 2026

Copy link
Copy Markdown
Author

I refactored this to minimize the amount of code generated by macros, by using generic trait impls instead.

Comment thread src/ops/total_order.rs
}

totalorder_impl_zeroable_via_ord_core!(
char, i8, u8, i16, u16, i32, u32, i64, u64, i128, u128, isize, usize,

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Weirdly enough char: ZeroablePrimitive, but NonZeroChar doesn't exist...

Comment thread src/float.rs Outdated
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