modtype 0.5.0

Macros that implement modular arithmetic integer types
Documentation

modtype

Build Status codecov Crates.io

This crate provides modular arithmetic integer types.

Usage

modtype::Z

#[modtype::use_modtype]
type F = modtype::u64::Z<1_000_000_007u64>;

assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");

modtype::thread_local::Z

#[allow(non_snake_case)]
modtype::thread_local::u32::Z::with(7, |F| {
    assert_eq!(F(6) + F(1), F(0));
});

modtype::field_param::Z

use modtype::field_param::u32::Z;
use num::CheckedDiv as _;

#[allow(non_snake_case)]
let Z = Z::factory(1000);

assert_eq!(Z(1).checked_div(&Z(777)), Some(Z(713))); // 777 × 713 ≡ 1 (mod 1000)

Customization

Zs can be customized via modtype::Impl.

#[modtype::use_modtype]
type F = modtype::Z<u64, Impl, 1_000_000_007u64>;

enum Impl {}

impl modtype::Impl for Impl {
    type Uint = u64;

    // your implementation here
}

Attributes for use_modtype

Name Format Optional
constant constant($Ident) Yes (default = concat!(_, $value, $type_uppercase))
constructor constructor($Ident) Yes (default = the type alias)