modtype

Build Status codecov Crates.io

This crate provides: - Macros that implement modular arithmetic integer types - Preset types - [modtype::u64::F] - [modtype::u64::field::F] - [modtype::u64::thread_local::F]

Usage

```rust

[modtype::use_modtype]

type F = modtype::u64::F<1_000_000_007u64>;

asserteq!((F(1000000006) + F(2)).to_string(), "1"); ```

To use a customized type, copy the following code via clipboard and edit it.

```rust

[allow(nonsnakecase)]

fn F(value: u64) -> F { F::from(value) }

[derive(

modtype::new,
modtype::new_unchecked,
modtype::get,
Default,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
modtype::From,
modtype::Into,
modtype::Display,
modtype::Debug,
modtype::FromStr,
modtype::Deref,
modtype::Neg,
modtype::Add,
modtype::AddAssign,
modtype::Sub,
modtype::SubAssign,
modtype::Mul,
modtype::MulAssign,
modtype::Div,
modtype::DivAssign,
modtype::Rem,
modtype::RemAssign,
modtype::Num,
modtype::Unsigned,
modtype::Bounded,
modtype::Zero,
modtype::One,
modtype::FromPrimitive,
modtype::Inv,
modtype::CheckedNeg,
modtype::CheckedAdd,
modtype::CheckedSub,
modtype::CheckedMul,
modtype::CheckedDiv,
modtype::CheckedRem,
modtype::Pow,
modtype::Integer,

)]

[modtype(

modulus = "1_000_000_007",
std = "std",
num_traits = "num::traits",
num_integer = "num::integer",
num_bigint = "num::bigint",
from(InnerValue, BigUint, BigInt),
debug(SingleTuple),
neg(for_ref = true),
add(for_ref = true),
add_assign(for_ref = true),
sub(for_ref = true),
sub_assign(for_ref = true),
mul(for_ref = true),
mul_assign(for_ref = true),
div(for_ref = true),
div_assign(for_ref = true),
rem(for_ref = true),
rem_assign(for_ref = true),
inv(for_ref = true),
pow(for_ref = true)

)] struct F { #[modtype(value)] __value: u64, } ```

Requirements

Attributes

use_modtype

| Name | Format | Optional | | :------------ | :----------------------------- | :-------------------------------------------- | | constant | constant($[Ident]) | Yes (default = concat!(_, $type_uppercase)) | | constructor | constructor($[Ident]) | Yes (default = the type alias) |

Derive Macros

Struct

| Name | Format | Optional | | :------------------- | :------------------------------------------------------------------------------------------------------- | :------------------------------- | | modulus | modulus = $[Lit] where $[Lit] is converted/parsed to an [Expr] | No | | std | std = $[LitStr] where $[LitStr] is parsed to a [Path] | Yes (default = ::std) | | num_traits | num_traits = $[LitStr] where $[LitStr] is parsed to a [Path] | Yes (default = ::num::traits) | | num_integer | num_integer = $[LitStr] where $[LitStr] is parsed to a [Path] | Yes (default = ::num::integer) | | num_bigint | num_bigint = $[LitStr] where $[LitStr] is parsed to a [Path] | Yes (default = ::num::bigint) | | from | from($[Ident]$(, $[Ident]s) $(,)?) where all [Ident]s ∈ {InnerValue, BigUint, BigInt} | Yes (default = all) | | debug | debug(SingleTuple) or debug(Transparent) | Yes (default = SingleTuple) | | neg | neg(for_ref = $[LitBool]) | Yes (default = true) | | add | add(for_ref = $[LitBool]) | Yes (default = true) | | add_assign | add_assign(for_ref = $[LitBool]) | Yes (default = true) | | sub | sub(for_ref = $[LitBool]) | Yes (default = true) | | sub_assign | sub_assign(for_ref = $[LitBool]) | Yes (default = true) | | mul | mul(for_ref = $[LitBool]) | Yes (default = true) | | mul_assign | mul_assign(for_ref = $[LitBool]) | Yes (default = true) | | div | div(for_ref = $[LitBool]) | Yes (default = true) | | div_assign | div_assign(for_ref = $[LitBool]) | Yes (default = true) | | rem | rem(for_ref = $[LitBool]) | Yes (default = true) | | rem_assign | rem_assign(for_ref = $[LitBool]) | Yes (default = true) | | inv | inv(for_ref = $[LitBool]) | Yes (default = true) | | pow | pow(for_ref = $[LitBool]) | Yes (default = true) |

Field

| Name | Format | Optional | | :------------------- | :------ | :------- | | value | value | No |

[ConstValue]

Struct

| Name | Format | Optional | | :------------------- | :----------------------------------------------------------- | :-------- | | const_value | const_value = $[LitInt] where $[LitInt] has a suffix | No |