Skip to main content

Crate platform_num

Crate platform_num 

Source
Expand description

§platform_num

Numeric traits for the Links Platform.

This crate provides a set of marker and conversion traits that abstract over Rust’s primitive integer types. They are used throughout the Links Platform ecosystem to write generic code that works with any integer type while preserving type safety.

§Traits

TraitDescription
NumberBase numeric trait — any PrimInt + Default + Debug + AsPrimitive<usize> + ToPrimitive
SignedNumberExtends Number with signed operations (Signed + FromPrimitive)
ToSignedConverts an unsigned type to its signed counterpart (e.g. u32i32)
MaxValueProvides a MAX associated constant for every primitive integer type
WrappingArithmeticComposite trait for wrapping arithmetic — bundles WrappingAdd, WrappingSub, WrappingMul, WrappingNeg, WrappingShl, WrappingShr
LinkReferenceComposite trait for link identifiers — unsigned, hashable, displayable, thread-safe, with wrapping arithmetic and TryFrom/TryInto for all integer types

§Re-exported num-traits

All num-traits traits that appear in this crate’s supertraits are re-exported so that downstream crates can use them without adding num-traits as a direct dependency:

§Example

use platform_num::{LinkReference, MaxValue, Number, SignedNumber, ToSigned};

fn max_link<T: LinkReference>() -> T {
    T::MAX
}

assert_eq!(max_link::<u32>(), u32::MAX);

Traits§

AsPrimitive
A generic interface for casting between machine scalars with the as operator, which admits narrowing and precision loss. Implementers of this trait AsPrimitive should behave like a primitive numeric type (e.g. a newtype around another primitive), and the intended conversion must never fail.
FromPrimitive
A generic trait for converting a number to a value.
LinkReference
A composite trait for types that can be used as link identifiers.
MaxValue
Provides the maximum value for a numeric type as an associated constant.
Number
A base numeric trait combining PrimInt, Default, Debug, AsPrimitive<usize>, and ToPrimitive.
PrimInt
Generic trait for primitive integers.
Signed
Useful functions for signed numbers (i.e. numbers that can be negative).
SignedNumber
A signed numeric trait extending Number with signed operations.
ToPrimitive
A generic trait for converting a value to a number.
ToSigned
Converts a numeric type to its signed counterpart.
Unsigned
A trait for values which cannot be negative
WrappingAdd
Performs addition that wraps around on overflow.
WrappingArithmetic
A composite trait for wrapping arithmetic operations.
WrappingMul
Performs multiplication that wraps around on overflow.
WrappingNeg
Performs a negation that does not panic.
WrappingShl
Performs a left shift that does not panic.
WrappingShr
Performs a right shift that does not panic.
WrappingSub
Performs subtraction that wraps around on overflow.