MultiType
MultiType is Rust a crate for generalising fundamental types via traits.
MultiType provideds traits such as Uint and Float traits 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:
IntIntLeast16IntLeast32IntLeast64
UintUintLeast16UintLeast32UintLeast64
FloatFloatLeast32FloatLeast64StdFloat
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 defined 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:
Intfori8,i16,i32,i64,i128, andisizeUintforu8,u16,u32,u64,u128, andusizeFloatforf16,f32,f64, andf128
Furthermore, StdFloat extends the Float trait with functionality that is typically only available in std's floating-point types.
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:
i16and wider implementIntLeast16i32and wider implementIntLeast32- 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
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 Array;
pub unsafe
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 Uint;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
Generic array indexing:
use f32;
use ;
assert_eq!;
assert_eq!;
assert_eq!;
Feature flags
Default features:
allocstd
Dependency features:
alloc: Enables compatibility withallocfacilitiesstd: Enables compatibility withstdfacilities
Unstable features:
clone_to_uninit: EnablesCloneToUninitrequirementsconst_param_ty: EnablesConstParamTy_requirementsf16: Enables support forf16f128: Enables support forf128freeze: EnablesFreezerequirementsstep: EnablesSteprequirementsstructural_partial_eq: EnablesStructuralPartialEqrequirementstrusted_step: EnablesTrustedSteprequirementsunstable-docs: Enables unstable documentation featuresuse_cloned: EnablesUseClonedrequirements
Unstable features can be expected to be removed as their facilities stabilise.
MSRV policy
The goal of MultiType is to provide generic traits that bind as much of the standard interfaces as possible. We will attempt to backport all trivial interfaces as much as possible, but if any given interface is deemed to complicated, we will bump the MSRV to leverage it from the standard implementation.
When const-compatible traits land, MultiType will implement the feature as quickly as possible.
Copyright & Licence.
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.