#min-max #min #max #traits

no-std min_max_traits

Traits for MIN and MAX associated constants

1 unstable release

0.1.0 Jan 16, 2022

#1588 in Math

28 downloads per month
Used in 2 crates

MIT/Apache

7KB

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:

Type Minimum Maximum
u8 0 28-1
u16 0 216-1
u32 0 232-1
u64 0 264-1
u128 0 2128-1

The signed two's complement integer types consist of:

Type Minimum Maximum
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.

No runtime deps