#numerics #primitives #traits

no-std funty

Trait generalization over the primitive types

4 stable releases

1.2.0 Feb 13, 2021
1.1.0 Dec 23, 2020
1.0.1 Mar 4, 2020
1.0.0 Feb 24, 2020

#40 in Rust patterns

Download history 24864/week @ 2020-12-25 39359/week @ 2021-01-01 51241/week @ 2021-01-08 48140/week @ 2021-01-15 57153/week @ 2021-01-22 69593/week @ 2021-01-29 74245/week @ 2021-02-05 81254/week @ 2021-02-12 88882/week @ 2021-02-19 86521/week @ 2021-02-26 82742/week @ 2021-03-05 82239/week @ 2021-03-12 84935/week @ 2021-03-19 88626/week @ 2021-03-26 85540/week @ 2021-04-02 81764/week @ 2021-04-09

245,674 downloads per month
Used in 1,037 crates (56 directly)

MIT license

54KB
741 lines

funty

Fundamental Type Unification

Crate Documentation License

Crate Downloads Crate Size

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.

MSRV

Each minor version of this library raises the MSRV to the current stable at the time of release.

Functionality Traits

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 integers iN.
      • IsUnsigned unifies all unsigned integers uN.
    • IsFloat unifies both floating-point numbers.

Width Traits

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.

Usage

Type use funty::*;, then declare the traits you need as generic bounds.

Examples

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
}

#![no_std] Compatibility

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

No runtime deps