Crate min_max_traits

Source
Expand description

§Traits for MIN and MAX associated constants

At the time of writing, all primitive numeric types in Rust provide MIN and MAX associated constants, which nonetheless do not belong to any trait.

One commonly used crate, num-traits, offers many useful traits for numeric types. However, the closest analogue of min_max_traits::Min and min_max_traits::Max offered by num-traits at the time of writing is num_traits::Bounded, which requires implementation of min_value() and max_value() functions. Since const_fn_trait_bound feature is in the works, num_traits::Bounded cannot be used in generic implementations of constant functions relying on MIN and MAX associated constants, at least on stable Rust.

These traits can be useful, for example, to generically implement associated constants storing the greatest length of primitive integers when converted to strings.

Excerpt from Rust’s reference:

§Numeric types

§Integer types

The unsigned integer types consist of:

TypeMinimumMaximum
u8028-1
u160216-1
u320232-1
u640264-1
u12802128-1

The signed two’s complement integer types consist of:

TypeMinimumMaximum
i8-(27)27-1
i16-(215)215-1
i32-(231)231-1
i64-(263)263-1
i128-(2127)2127-1

§Floating-point types

The IEEE 754-2008 “binary32” and “binary64” floating-point types are f32 and f64, respectively.

§License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Traits§

Max
Trait ensuring that there is a maximal value that is also constant
Min
Trait ensuring that there is a minimal value that is also constant