platform_num/lib.rs
1//! # platform_num
2//!
3//! Numeric traits for the [Links Platform](https://github.com/linksplatform).
4//!
5//! This crate provides a set of marker and conversion traits that abstract
6//! over Rust's primitive integer types. They are used throughout the Links
7//! Platform ecosystem to write generic code that works with any integer
8//! type while preserving type safety.
9//!
10//! ## Traits
11//!
12//! | Trait | Description |
13//! |-------|-------------|
14//! | [`Number`] | Base numeric trait — any `PrimInt + Default + Debug + AsPrimitive<usize> + ToPrimitive` |
15//! | [`SignedNumber`] | Extends [`Number`] with signed operations (`Signed + FromPrimitive`) |
16//! | [`ToSigned`] | Converts an unsigned type to its signed counterpart (e.g. `u32` → `i32`) |
17//! | [`MaxValue`] | Provides a `MAX` associated constant for every primitive integer type |
18//! | [`WrappingArithmetic`] | Composite trait for wrapping arithmetic — bundles `WrappingAdd`, `WrappingSub`, `WrappingMul`, `WrappingNeg`, `WrappingShl`, `WrappingShr` |
19//! | [`LinkReference`] | Composite trait for link identifiers — unsigned, hashable, displayable, thread-safe, with wrapping arithmetic and `TryFrom`/`TryInto` for all integer types |
20//!
21//! ## Re-exported `num-traits`
22//!
23//! All `num-traits` traits that appear in this crate's supertraits are
24//! re-exported so that downstream crates can use them without adding
25//! `num-traits` as a direct dependency:
26//!
27//! | Re-export | Used in |
28//! |-----------|---------|
29//! | [`PrimInt`] | [`Number`] |
30//! | [`AsPrimitive`] | [`Number`] |
31//! | [`ToPrimitive`] | [`Number`] |
32//! | [`FromPrimitive`] | [`SignedNumber`], [`LinkReference`] |
33//! | [`Signed`] | [`SignedNumber`] |
34//! | [`Unsigned`] | [`LinkReference`] |
35//! | [`WrappingAdd`], [`WrappingSub`], [`WrappingMul`], [`WrappingNeg`], [`WrappingShl`], [`WrappingShr`] | [`WrappingArithmetic`] |
36//!
37//! ## Example
38//!
39//! ```
40//! use platform_num::{LinkReference, MaxValue, Number, SignedNumber, ToSigned};
41//!
42//! fn max_link<T: LinkReference>() -> T {
43//! T::MAX
44//! }
45//!
46//! assert_eq!(max_link::<u32>(), u32::MAX);
47//! ```
48
49mod imp;
50
51pub use imp::{LinkReference, MaxValue, Number, SignedNumber, ToSigned, WrappingArithmetic};
52
53pub use num_traits::ops::wrapping::{
54 WrappingAdd, WrappingMul, WrappingNeg, WrappingShl, WrappingShr, WrappingSub,
55};
56pub use num_traits::{AsPrimitive, FromPrimitive, PrimInt, Signed, ToPrimitive, Unsigned};