Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Unlicense Crates.io Rust CI/CD Pipeline Docs.rs Codecov

Numbers for Rust

LinksPlatform's platform-num crate — numeric traits and types for the Links platform.

Crates.io package: platform-num

Overview

This crate provides a set of numeric traits used throughout the LinksPlatform ecosystem:

  • Num — A base trait combining PrimInt, Default, Debug, AsPrimitive<usize>, and ToPrimitive. Implemented for all primitive integer types.
  • SignNum — Extends Num with signed number operations (Signed, FromPrimitive). Implemented for signed integer types.
  • ToSigned — Converts unsigned types to their signed counterparts (e.g. u32i32).
  • MaxValue — Provides a MAX associated 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 for u8, u16, u32, u64, and usize.

Installation

Add to your Cargo.toml:

[dependencies]
platform-num = "0.2"

Usage

Using LinkType as a generic constraint

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));

Using ToSigned for type conversion

use platform_num::ToSigned;

let unsigned_val: u32 = 42;
let signed_val: i32 = unsigned_val.to_signed();
assert_eq!(signed_val, 42i32);

Using MaxValue

use platform_num::MaxValue;

fn get_max<T: MaxValue>() -> T {
    T::MAX
}

assert_eq!(get_max::<u64>(), u64::MAX);

Using Num for generic numeric operations

use platform_num::Num;
use num_traits::AsPrimitive;

fn to_usize<T: Num>(val: T) -> usize {
    val.as_()
}

assert_eq!(to_usize(42u32), 42usize);

Depend on

Dependent libraries

License

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.