Skip to main content

Additive

Trait Additive 

Source
pub trait Additive:
    Object
    + for<'a> AddAssign<&'a Self>
    + for<'a> Add<&'a Self, Output = Self>
    + for<'a> SubAssign<&'a Self>
    + for<'a> Sub<&'a Self, Output = Self>
    + Neg<Output = Self> {
    // Required method
    fn zero() -> Self;

    // Provided methods
    fn double(&mut self) { ... }
    fn scale(&self, bits_le: &[u64]) -> Self { ... }
}
Expand description

A type that supports addition, subtraction, and negation.

For some type T implementing this trait, the following operations must be supported:

  1. &mut T += &T,
  2. T + &T,
  3. &mut T -= &T,
  4. T - &T,
  5. -T.

There are other combinations of borrowing that could be chosen, but these should be efficiently implementable, even for a “heavier” struct, e.g. a vector of values.

§Properties

Implementations are expected to form a commutative group under addition:

  • += and -= agree with + and -,
  • addition satisfies a + b = b + a and (a + b) + c = a + (b + c),
  • Additive::zero satisfies 0 + a = a,
  • negation satisfies a + (-a) = 0 and a - b = a + (-b),
  • Additive::scale satisfies a.scale(&[]) = 0, a.scale(&[1]) = a, and a.scale(m + n) = a.scale(m) + a.scale(n).

§Usage


// We use .clone() whenever ownership is needed.
fn example<T: Additive>(mut x: T, y: T) {
    x += &y;
    x.clone() + &y;
    x -= &y;
    x.clone() - &y;
    -x.clone();
    T::zero();
}

Required Methods§

Source

fn zero() -> Self

The neutral element for addition.

Provided Methods§

Source

fn double(&mut self)

Add an element to itself.

This has a default implementation involving a clone.

This can be overriden if a more efficient implementation is available.

Source

fn scale(&self, bits_le: &[u64]) -> Self

Scale this number by a positive integer.

To support arbitrary positive integers, we expect to see 64 bit limbs in little endian order.

For example, for a 256 bit integer, we expect a slice of 4 elements, starting with the lowest 64 bits.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Additive for F

Source§

impl<F: Additive, G: Space<F>> Additive for Synthetic<F, G>

Source§

impl<K: Additive> Additive for Poly<K>