#numerics #primitives #traits

no-std funty

Trait generalization over the primitive types

5 stable releases

new 2.0.0 Nov 28, 2021
1.2.0 Feb 13, 2021
1.1.0 Dec 23, 2020
1.0.1 Mar 4, 2020
1.0.0 Feb 24, 2020

#187 in Rust patterns

Download history 147332/week @ 2021-08-13 182756/week @ 2021-08-20 161824/week @ 2021-08-27 148359/week @ 2021-09-03 179406/week @ 2021-09-10 185540/week @ 2021-09-17 180512/week @ 2021-09-24 175543/week @ 2021-10-01 185291/week @ 2021-10-08 186141/week @ 2021-10-15 160727/week @ 2021-10-22 155655/week @ 2021-10-29 149215/week @ 2021-11-05 150988/week @ 2021-11-12 146912/week @ 2021-11-19 136033/week @ 2021-11-26

612,374 downloads per month
Used in 1,122 crates (38 directly)

MIT license

56KB
863 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 provides a set of traits that abstract over common API surfaces of the primitive types, so that properties such as numeric behavior, register width, or signedness can be represented in the trait system.

MSRV

The current MSRV is 1.53, which added the ::BITS constant. This version may raise in minor versions. You should use "^X.Y" constraints when depending on this crate.

Functionality Traits

All primitive types (bool, char, {i,u}{8,16,32,64,128,size}, and f{32,64}) implement the Fundamental trait. This trait defines the basic concepts available to primitives: they are plain-old-data values, and can be as-casted to each other. Fundamental has no functionality other than providing the basic set of traits and allowing conversion.

The numeric primitives (everything except bool and char) implement the following trait hierarchy:

  • Numeric exports all the trait implementations and methods found on all numeric primitives.
    • Integral exports the trait implementations and methods found on all integers.
      • Signed unifies all signed integers iN.
      • Unsigned unifies all unsigned integers uN.
    • Floating 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::Unsigned;
fn invert_middle_bits<T: Unsigned>(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