Numbers for Rust
LinksPlatform's platform-num crate — numeric traits and types
for the Links platform.
Crates.io package: platform-num
This crate provides a set of numeric traits used throughout the LinksPlatform ecosystem:
Num— A base trait combiningPrimInt,Default,Debug,AsPrimitive<usize>, andToPrimitive. Implemented for all primitive integer types.SignNum— ExtendsNumwith signed number operations (Signed,FromPrimitive). Implemented for signed integer types.ToSigned— Converts unsigned types to their signed counterparts (e.g.u32→i32).MaxValue— Provides aMAXassociated constant for all primitive integer types.LinkType— A composite trait for types that can be used as link identifiers:Num + Unsigned + ToSigned + MaxValue + FromPrimitive + Debug + Display + Hash + Send + Sync + 'static. Implemented foru8,u16,u32,u64, andusize.
Add to your Cargo.toml:
[dependencies]
platform-num = "0.2"use platform_num::LinkType;
fn create_link<T: LinkType>(source: T, target: T) -> (T, T) {
(source, target)
}
let link = create_link(1u64, 2u64);
assert_eq!(link, (1u64, 2u64));use platform_num::ToSigned;
let unsigned_val: u32 = 42;
let signed_val: i32 = unsigned_val.to_signed();
assert_eq!(signed_val, 42i32);use platform_num::MaxValue;
fn get_max<T: MaxValue>() -> T {
T::MAX
}
assert_eq!(get_max::<u64>(), u64::MAX);use platform_num::Num;
use num_traits::AsPrimitive;
fn to_usize<T: Num>(val: T) -> usize {
val.as_()
}
assert_eq!(to_usize(42u32), 42usize);This crate is released to the public domain under the Unlicense.
The Unlicense is the most permissive license available — it places no restrictions whatsoever on users. You are free to copy, modify, publish, use, compile, sell, or distribute this software for any purpose, commercial or non-commercial, in any way you choose, with no conditions attached.
Unlike LGPL, which forces users to redistribute modifications under the same license and comply with specific obligations (linking restrictions, source disclosure for modifications), the Unlicense imposes no obligations at all. It is truly free as in freedom.