[][src]Crate modtype

This crate provides:

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, or usize.
  • 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 than 1.
  • If the modular arithmetic type implements Div, the modulus is a prime.

Attributes

use_modtype

NameFormatOptional
constantconstant($Ident)Yes (default = concat!(_, $type_uppercase))
constructorconstructor($Ident)Yes (default = the type alias)

Derive Macros

Struct

NameFormatOptional
modulusmodulus = $Lit where $Lit is converted/parsed to an ExprNo
stdstd = $LitStr where $LitStr is parsed to a PathYes (default = ::std)
num_traitsnum_traits = $LitStr where $LitStr is parsed to a PathYes (default = ::num::traits)
num_integernum_integer = $LitStr where $LitStr is parsed to a PathYes (default = ::num::integer)
num_bigintnum_bigint = $LitStr where $LitStr is parsed to a PathYes (default = ::num::bigint)
fromfrom($Ident $(, $Idents) $(,)?) where all Idents ∈ {InnerValue, BigUint, BigInt}Yes (default = all)
debugdebug(SingleTuple) or debug(Transparent)Yes (default = SingleTuple)
negneg(for_ref = $LitBool)Yes (default = true)
addadd(for_ref = $LitBool)Yes (default = true)
add_assignadd_assign(for_ref = $LitBool)Yes (default = true)
subsub(for_ref = $LitBool)Yes (default = true)
sub_assignsub_assign(for_ref = $LitBool)Yes (default = true)
mulmul(for_ref = $LitBool)Yes (default = true)
mul_assignmul_assign(for_ref = $LitBool)Yes (default = true)
divdiv(for_ref = $LitBool)Yes (default = true)
div_assigndiv_assign(for_ref = $LitBool)Yes (default = true)
remrem(for_ref = $LitBool)Yes (default = true)
rem_assignrem_assign(for_ref = $LitBool)Yes (default = true)
invinv(for_ref = $LitBool)Yes (default = true)
powpow(for_ref = $LitBool)Yes (default = true)

Field

NameFormatOptional
valuevalueNo

ConstValue

Struct

NameFormatOptional
const_valueconst_value = $LitInt where $LitInt has a suffixNo

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 u64.

Traits

ConstValue

A trait that has one associated constant value.