[−][src]Crate modtype
This crate provides:
- Macros that implement modular arithmetic integer types
- Preset types
Usage
#[modtype::use_modtype] type F = modtype::u64::F<1_000_000_007u64>; assert_eq!((F(1_000_000_006) + F(2)).to_string(), "1");
To use a customized type, copy the following code via clipboard and edit it.
#[allow(non_snake_case)] 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", 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
- The inner value is
u8
,u16
,u32
,u64
,u128
, orusize
. - The inner value and the modulus are of a same type.
- The modulus is immutable.
- The inner value is always smaller than the modulus.
- If the modular arithmetic type implements
One
, The modulus is larger than1
.
- If the modular arithmetic type implements
- If the modular arithmetic type implements
Div
, the modulus is a prime.
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 |
Re-exports
pub use modtype_derive::use_modtype; |
pub use modtype_derive::ConstValue; |
pub use modtype_derive::new; |
pub use modtype_derive::new_unchecked; |
pub use modtype_derive::get; |
pub use modtype_derive::From; |
pub use modtype_derive::Into; |
pub use modtype_derive::Display; |
pub use modtype_derive::ModtypeDebug as Debug; |
pub use modtype_derive::FromStr; |
pub use modtype_derive::Deref; |
pub use modtype_derive::Neg; |
pub use modtype_derive::Add; |
pub use modtype_derive::AddAssign; |
pub use modtype_derive::Sub; |
pub use modtype_derive::SubAssign; |
pub use modtype_derive::Mul; |
pub use modtype_derive::MulAssign; |
pub use modtype_derive::Div; |
pub use modtype_derive::DivAssign; |
pub use modtype_derive::Rem; |
pub use modtype_derive::RemAssign; |
pub use modtype_derive::Num; |
pub use modtype_derive::Unsigned; |
pub use modtype_derive::Bounded; |
pub use modtype_derive::Zero; |
pub use modtype_derive::One; |
pub use modtype_derive::FromPrimitive; |
pub use modtype_derive::Inv; |
pub use modtype_derive::CheckedNeg; |
pub use modtype_derive::CheckedAdd; |
pub use modtype_derive::CheckedSub; |
pub use modtype_derive::CheckedMul; |
pub use modtype_derive::CheckedDiv; |
pub use modtype_derive::CheckedRem; |
pub use modtype_derive::Pow; |
pub use modtype_derive::Integer; |
Modules
u64 | Preset types that the inner types are |
Traits
ConstValue | A trait that has one associated constant value. |