Trait hacspec_lib::prelude::Mul

1.0.0 · source · []
pub trait Mul<Rhs = Self> {
    type Output;

    fn mul(self, rhs: Rhs) -> Self::Output;
}
Expand description

The multiplication operator *.

Note that Rhs is Self by default, but this is not mandatory.

Examples

Multipliable rational numbers

use std::ops::Mul;

// By the fundamental theorem of arithmetic, rational numbers in lowest
// terms are unique. So, by keeping `Rational`s in reduced form, we can
// derive `Eq` and `PartialEq`.
#[derive(Debug, Eq, PartialEq)]
struct Rational {
    numerator: usize,
    denominator: usize,
}

impl Rational {
    fn new(numerator: usize, denominator: usize) -> Self {
        if denominator == 0 {
            panic!("Zero is an invalid denominator!");
        }

        // Reduce to lowest terms by dividing by the greatest common
        // divisor.
        let gcd = gcd(numerator, denominator);
        Self {
            numerator: numerator / gcd,
            denominator: denominator / gcd,
        }
    }
}

impl Mul for Rational {
    // The multiplication of rational numbers is a closed operation.
    type Output = Self;

    fn mul(self, rhs: Self) -> Self {
        let numerator = self.numerator * rhs.numerator;
        let denominator = self.denominator * rhs.denominator;
        Self::new(numerator, denominator)
    }
}

// Euclid's two-thousand-year-old algorithm for finding the greatest common
// divisor.
fn gcd(x: usize, y: usize) -> usize {
    let mut x = x;
    let mut y = y;
    while y != 0 {
        let t = y;
        y = x % y;
        x = t;
    }
    x
}

assert_eq!(Rational::new(1, 2), Rational::new(2, 4));
assert_eq!(Rational::new(2, 3) * Rational::new(3, 4),
           Rational::new(1, 2));

Multiplying vectors by scalars as in linear algebra

use std::ops::Mul;

struct Scalar { value: usize }

#[derive(Debug, PartialEq)]
struct Vector { value: Vec<usize> }

impl Mul<Scalar> for Vector {
    type Output = Self;

    fn mul(self, rhs: Scalar) -> Self::Output {
        Self { value: self.value.iter().map(|v| v * rhs.value).collect() }
    }
}

let vector = Vector { value: vec![2, 4, 6] };
let scalar = Scalar { value: 3 };
assert_eq!(vector * scalar, Vector { value: vec![6, 12, 18] });

Required Associated Types

The resulting type after applying the * operator.

Required Methods

Performs the * operation.

Example
assert_eq!(12 * 2, 24);

Implementors

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: has wrapping semantics.

Warning: wraps on overflow.

Warning: has wrapping semantics.

Warning: wraps on overflow.

Warning: has wrapping semantics.

Warning: wraps on overflow.

Warning: has wrapping semantics.

Warning: wraps on overflow.

Warning: wraps on overflow.

Warning: wraps on overflow.

Warning: wraps on overflow.

Warning: wraps on overflow.

Polynomial multiplication on ℤ[x]

Polynomial multiplication on ℤ[x]

impl Mul<State> for State

impl Mul<Block> for Block

impl Mul<PolyKey> for PolyKey

impl Mul<State> for State

impl Mul<Row> for Row

impl<I: Integer> Mul<I> for Z0

impl<U: Unsigned + NonZero> Mul<Z0> for PInt<U>

impl<U: Unsigned + NonZero> Mul<Z0> for NInt<U>

impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero> Mul<PInt<Ur>> for PInt<Ul> where
    Ul: Mul<Ur>,
    <Ul as Mul<Ur>>::Output: Unsigned + NonZero

impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero> Mul<NInt<Ur>> for NInt<Ul> where
    Ul: Mul<Ur>,
    <Ul as Mul<Ur>>::Output: Unsigned + NonZero

impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero> Mul<NInt<Ur>> for PInt<Ul> where
    Ul: Mul<Ur>,
    <Ul as Mul<Ur>>::Output: Unsigned + NonZero

impl<Ul: Unsigned + NonZero, Ur: Unsigned + NonZero> Mul<PInt<Ur>> for NInt<Ul> where
    Ul: Mul<Ur>,
    <Ul as Mul<Ur>>::Output: Unsigned + NonZero

impl<U: Unsigned, B: Bit> Mul<B0> for UInt<U, B>

impl Mul<B0> for UTerm

impl Mul<B1> for UTerm

impl<U: Unsigned, B: Bit> Mul<B1> for UInt<U, B>

impl<U: Unsigned, B: Bit> Mul<UTerm> for UInt<U, B>

impl<U: Unsigned> Mul<U> for UTerm

impl<Ul: Unsigned, B: Bit, Ur: Unsigned> Mul<UInt<Ur, B>> for UInt<Ul, B0> where
    Ul: Mul<UInt<Ur, B>>, 

impl<Ul: Unsigned, B: Bit, Ur: Unsigned> Mul<UInt<Ur, B>> for UInt<Ul, B1> where
    Ul: Mul<UInt<Ur, B>>,
    UInt<Prod<Ul, UInt<Ur, B>>, B0>: Add<UInt<Ur, B>>, 

impl<Rhs> Mul<Rhs> for ATerm

impl<V, A, Rhs> Mul<Rhs> for TArr<V, A> where
    V: Mul<Rhs>,
    A: Mul<Rhs>,
    Rhs: Copy

impl Mul<ATerm> for Z0

impl<U> Mul<ATerm> for PInt<U> where
    U: Unsigned + NonZero

impl<U> Mul<ATerm> for NInt<U> where
    U: Unsigned + NonZero

impl<V, A> Mul<TArr<V, A>> for Z0 where
    Z0: Mul<A>, 

impl<V, A, U> Mul<TArr<V, A>> for PInt<U> where
    U: Unsigned + NonZero,
    PInt<U>: Mul<A> + Mul<V>, 

impl<V, A, U> Mul<TArr<V, A>> for NInt<U> where
    U: Unsigned + NonZero,
    NInt<U>: Mul<A> + Mul<V>, 

impl Mul<Block> for Block

impl Mul<Word> for Word

impl Mul<Nonce> for Nonce

impl Mul<SBox> for SBox

impl Mul<RCon> for RCon

impl Mul<Key128> for Key128

impl Mul<Key256> for Key256

impl Mul<Block> for Block

impl Mul<Key> for Key

impl Mul<Tag> for Tag

impl Mul<DigestB> for DigestB

impl Mul<Scalar> for Scalar

impl Mul<Scalar> for Scalar

impl Mul<Scalar> for Scalar

impl Mul<Block> for Block

impl Mul<Digest> for Digest

impl Mul<Hash> for Hash