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.

§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", so this trait is not object safe.

Implementors§