Fundamental type traits. https://crates.io/crates/multitype/
Find a file
2025-10-19 15:35:00 +02:00
src Clean up code; Update lints; Update docs; 2025-10-19 15:35:00 +02:00
.gitignore Add changelog; Add gitignore; Add Cargo manifest; License under MPL 2.0; Set MSRV to 1.87 and edition to 2024; Configure lints; Add 'std' and 'alloc' features; Add 'num' module; Add 'Num', 'Int', 'Uint', 'Float', and 'StdFloat' traits to 'num'; Add 'f16' and 'f128' features; Add 'cast_signed' method to 'Uint'; Add 'cast_unsigned' method to 'Int'; Add 'sqrt', 'cbrt', 'sin', 'cos', 'tan', 'asin', 'acos', 'atan', 'atan2', 'sinh', 'cosh', 'tanh', 'asinh', 'acosh', 'atanh', 'exp', 'exp2', 'log', 'ln', 'log2', 'log10', 'round', 'fract', 'ceil', 'floor', and 'trunc' methods to 'StdFloat'; Add 'mul_add' method to 'Float'; Add 'Bits' type to 'Float'; Add 'from_bits' constructor and 'to_bits' destructor to 'Float'; Add 'Signed' type to 'Uint'; Add 'Unsigned' type to 'Int'; 2025-08-13 11:40:41 +02:00
Cargo.toml Clean up code; Update lints; Update docs; 2025-10-19 15:35:00 +02:00
CHANGELOG.md Clean up code; Update lints; Update docs; 2025-10-19 15:35:00 +02:00
clippy.toml Add 'ptr' module; Add 'ConstPtr' and 'MutPtr' traits to 'ptr'; Add 'Target' type to 'ConstPtr' and 'MutPtr'; Add 'null' constructor to 'ConstPtr' and 'MutPtr'; Add 'is_null' method to 'ConstPtr' and 'MutPtr'; Relicense under MIT or Apache 2.0; Add 'ConstPtr' type to 'MutPtr'; Add 'MutPtr' type to 'ConstPtr'; Add 'cast_const' method to 'MutPtr'; Add 'cast_mut' method to 'ConstPtr'; Require bitwise operations for traits; Add 'from_i8' constructor to 'Int'; Add 'from_u8' constructor to 'Uint'; Add 'from_f16', 'from_f64_lossy', and 'from_f128_lossy' constructors to 'Float'; Add 'unstable-docs' feature; Require comparison operations for traits; Remove 'Num' trait; Simplify operator requirements; Add 'addr' method to 'ConstPtr' and 'MutPtr'; Add mathematical constants to 'Float'; Require 'Neg' for traits; Require 'Default' and 'Debug' for all traits; Require 'Display' for traits; Require 'Sum' and 'Product' for traits; Require 'Pointer', 'Binary', 'LowerExp', 'LowerHex', 'Octal', 'UpperExp', and 'UpperHex' for traits; 2025-08-14 13:01:50 +02:00
doc-icon.svg Update docs logo; 2025-09-18 21:03:35 +02:00
LICENCE-APACHE Add 'ptr' module; Add 'ConstPtr' and 'MutPtr' traits to 'ptr'; Add 'Target' type to 'ConstPtr' and 'MutPtr'; Add 'null' constructor to 'ConstPtr' and 'MutPtr'; Add 'is_null' method to 'ConstPtr' and 'MutPtr'; Relicense under MIT or Apache 2.0; Add 'ConstPtr' type to 'MutPtr'; Add 'MutPtr' type to 'ConstPtr'; Add 'cast_const' method to 'MutPtr'; Add 'cast_mut' method to 'ConstPtr'; Require bitwise operations for traits; Add 'from_i8' constructor to 'Int'; Add 'from_u8' constructor to 'Uint'; Add 'from_f16', 'from_f64_lossy', and 'from_f128_lossy' constructors to 'Float'; Add 'unstable-docs' feature; Require comparison operations for traits; Remove 'Num' trait; Simplify operator requirements; Add 'addr' method to 'ConstPtr' and 'MutPtr'; Add mathematical constants to 'Float'; Require 'Neg' for traits; Require 'Default' and 'Debug' for all traits; Require 'Display' for traits; Require 'Sum' and 'Product' for traits; Require 'Pointer', 'Binary', 'LowerExp', 'LowerHex', 'Octal', 'UpperExp', and 'UpperHex' for traits; 2025-08-14 13:01:50 +02:00
LICENCE-MIT Add 'ptr' module; Add 'ConstPtr' and 'MutPtr' traits to 'ptr'; Add 'Target' type to 'ConstPtr' and 'MutPtr'; Add 'null' constructor to 'ConstPtr' and 'MutPtr'; Add 'is_null' method to 'ConstPtr' and 'MutPtr'; Relicense under MIT or Apache 2.0; Add 'ConstPtr' type to 'MutPtr'; Add 'MutPtr' type to 'ConstPtr'; Add 'cast_const' method to 'MutPtr'; Add 'cast_mut' method to 'ConstPtr'; Require bitwise operations for traits; Add 'from_i8' constructor to 'Int'; Add 'from_u8' constructor to 'Uint'; Add 'from_f16', 'from_f64_lossy', and 'from_f128_lossy' constructors to 'Float'; Add 'unstable-docs' feature; Require comparison operations for traits; Remove 'Num' trait; Simplify operator requirements; Add 'addr' method to 'ConstPtr' and 'MutPtr'; Add mathematical constants to 'Float'; Require 'Neg' for traits; Require 'Default' and 'Debug' for all traits; Require 'Display' for traits; Require 'Sum' and 'Product' for traits; Require 'Pointer', 'Binary', 'LowerExp', 'LowerHex', 'Octal', 'UpperExp', and 'UpperHex' for traits; 2025-08-14 13:01:50 +02:00
LOGO.svg Update readme; Remove slice methods from 'Array'; Require 'From<u8>' for 'Uint'; Require 'From<u16>' for 'UintLeast16'; Require 'From<u32>' for 'UintLeast32'; Require 'From<u64>' for 'UintLeast64'; Require 'From<i8>' for 'Int'; Require 'From<u8>' and 'From<i16>' for 'IntLeast16'; Require 'From<u16>' and 'From<i32>' for 'IntLeast32'; Require 'From<u32>' and 'From<i64>' for 'IntLeast64'; Remove 'UintLeast128', 'IntLeast128', and 'FloatLeast128'; Lock 'FloatLeast64' behind 'f128'; Require 'From<f32>' for 'FloatLeast32'; Require 'From<f64>' for 'FloatLeast64'; Require 'From<u16>' and 'From<i16>' for 'FloatLeast32'; Require 'From<u32>' and 'From<i32>' for 'FloatLeast64'; Update logo; Require 'From<u8>' and 'From<i8>' for 'Float'; Require 'UnwindSafe', 'Unpin', 'Send', 'Sync' for 'Uint', 'Int', and 'Float'; 2025-08-30 18:51:38 +02:00
README.md Merge 'StdFloat' into 'Float'; 2025-10-08 16:07:09 +02:00

MultiType

MultiType is Rust a crate for generalising fundamental types via traits.

MultiType provideds traits such as Uint and Float to abstract over a set of equivalent primitive types. These traits are intended to provide one-to-one copies of the interfaces that the primitive types define.

Overview

The complete list of abstraction traits is:

  • Int
    • IntLeast16
    • IntLeast32
    • IntLeast64
  • Uint
    • UintLeast16
    • UintLeast32
    • UintLeast64
  • Float
    • FloatLeast32
    • FloatLeast64
  • Array

Any given type may implement at most one of the aforementioned trait groups; thus, for example, any type that implemets Float can be assumed to possibly implement FloatLeast32 but never Uint.

For the sake of compatibility with {f16, f32, f64, f128}::to_int_unchecked, we also define our own FloatToInt trait.

Note that all traits provided by this crate are sealed and cannot be implemented by third-party crates (at least currently).

Arithmetic types

MultiType defines different traits for generalising arithmetic types:

  • Int for i8, i16, i32, i64, i128, and isize
  • Uint for u8, u16, u32, u64, u128, and usize
  • Float for f16, f32, f64, and f128

Sized, arithmetic types

The basic, arithmetic traits guarantee a minimum size that is equivalent to the smallest member of its group, e.g. Uint is at least u8 and Float is at least f16.

Additionally, these three arithmetic traits have subtraits that guarantee wider types, for example:

  • i16 and wider implement IntLeast16
  • i32 and wider implement IntLeast32
  • Etc.

Extremely-wide traits, e.g. IntLeast8 or IntLeast128, are considered redundant and are thus not provided.

Array types

MultiType also provides the Array trait for generalising array types most often over their length.

An example of this trait's usecase is actually in this crate: Take, for instance, the definition of Uint: It has a Bytes associated type that is used by the bytewise constructors and destructors:

pub unsafe trait Uint: /* .. */ {
    type Bytes;

    fn from_ne_bytes(bytes: Self::Bytes) -> Self;

    fn to_ne_bytes(self) -> Self::Bytes;

    // ..
}

Now, anyone that would want to use the output of to_ne_bytes:to_ne_bytes) wouldn't really have that many choices with regard to what to do with it. So, MultiType defines the Array trait:

use multitype::Array;

pub unsafe trait Uint: /* .. */ {
    type Bytes: Array<Scalar = u8> /* + .. */;

    // ..
}

With it, it's possible for users to generically use Uint::to_ne_bytes as an array type through the trait methods.

Examples

A generic Fibonacci sequence:

use multitype::Uint;

fn f<T: Uint>(x: T) -> T {
    let mut y    = T::from(0u8);
    let mut y_m1 = T::from(0u8);
    let mut y_m2 = T::from(1u8);

    let mut i = T::from(0u8);
    while i < x {
        y = y_m1 + y_m2;

        y_m2 = y_m1;
        y_m1 = y;

        i += T::from(1u8);
    }

    y
}

assert_eq!(f(0u8),   0);
assert_eq!(f(1u8),   1);

assert_eq!(f(2u16),  1);
assert_eq!(f(3u16),  2);

assert_eq!(f(4u32),  3);
assert_eq!(f(5u32),  5);

assert_eq!(f(6u64),  8);
assert_eq!(f(7u64), 13);

Generic array indexing:

use core::f32;
use multitype::{Array, Float};

fn complicated_neg<T: Float>(value: T) -> T {
    let mut bytes = value.to_le_bytes();

    // Invert the sign bit -- which is always the most
    // significant bit of the most significant byte.
    *bytes.as_mut_slice().last_mut().unwrap() ^= 0b10000000;

    T::from_le_bytes(bytes)
}

assert_eq!(complicated_neg( 1.0f64), -1.0f64);
assert_eq!(complicated_neg(-1.0f64),  1.0f64);

assert_eq!(complicated_neg(f32::NEG_INFINITY), f32::INFINITY);

Feature gates

Default features:

  • alloc
  • std

Dependency features:

  • alloc: Enables compatibility with alloc facilities
  • std: Enables compatibility with std facilities

Unstable features:

  • clone_to_uninit: Enables CloneToUninit requirements
  • const_param_ty: Enables ConstParamTy_ requirements
  • f16: Enables support for f16
  • f128: Enables support for f128
  • freeze: Enables Freeze requirements
  • nightly_backport: Enables backports for nightly items
  • num_buffer_trait: Enables NumBufferTrait requirements
  • step: Enables Step requirements
  • structural_partial_eq: Enables StructuralPartialEq requirements
  • trusted_step: Enables TrustedStep requirements
  • unstable_docs: Enables unstable documentation features
  • use_cloned: Enables UseCloned requirements

Unstable features can be expected to be removed as their facilities stabilise.

SemVer policy

Nightly backports will match the current, nightly interfaces provided by rustc. A change in rustc will thus be reflected here along with a bump in the minor version.

Conversely, when a nightly item is stabilised in rustc, the corresponding nightly backport will be released from the feature gate in a minor version bump.

MSRV policy

The goal of MultiType is to provide generic traits that bind as much of the standard interfaces as possible. Items that are added after the MSRV will be backported.

Copyright © 2025 Gabriel Bjørnager Jensen.

MultiType is distributed under either an MIT licence (see LICENCE-MIT) or version 2.0 of the Apache License (see LICENCE-APACHE), at your option.