Prior to 1.0
, Rust had traits for the numeric primitive types to permit code
to generalize over which specific type it accepted. This was never stabilized,
and eventually removed.
This library reïnstates these traits.
Each minor version of this library raises the MSRV to the current stable at the time of release.
The numeric primitives implement the following trait hierarchy:
IsNumber
exports all the trait implementations and methods found on all numeric primitives.IsInteger
exports the trait implementations and methods found on all integers.IsSigned
unifies all signed integersiN
.IsUnsigned
unifies all unsigned integersuN
.
IsFloat
unifies both floating-point numbers.
There are three trait families for type width. For Width
values of 8
, 16
,
32
, 64
, and 128
:
IsWidth
is implemented by the numbers that are exactly this width.AtLeastWidth
is implemented by all numbers that are this width or wider.AtMostWidth
is implemented by all numbers that are this width or narrower.
Type use funty::*;
, then declare the traits you need as generic bounds.
Perform bit arithmetic on some unsigned integer:
use funty::IsUnsigned;
fn invert_middle_bits<T: IsUnsigned>(num: T) -> T {
let mask = (!T::ZERO).wrapping_shl(2).wrapping_shr(4).wrapping_shl(2);
num ^ mask
}
The floating-point numbers offer many functions which are implemented in the
target system’s libm
. This library is present only in std
-targets. If you
are compiling to a #![no_std]
target, depend on this library with
[dependencies.funty]
version = "1"
default-features = false