Semigroup

Trait Semigroup 

Source
pub trait Semigroup: Sized {
    // Required method
    fn combine(&self, other: &Self) -> Self;

    // Provided method
    fn combine_assign(&mut self, other: &Self) { ... }
}
Expand description

A semigroup: a type with an associative binary operation.

Laws (not enforced by type system):

  • Associative: a.combine(b).combine(c) == a.combine(b.combine(c))

§Example

use algebra_core::Semigroup;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Max(i32);

impl Semigroup for Max {
    fn combine(&self, other: &Self) -> Self {
        Max(self.0.max(other.0))
    }
}

let x = Max(3);
let y = Max(5);
let z = Max(2);
assert_eq!(x.combine(&y).combine(&z), x.combine(&y.combine(&z)));

Required Methods§

Source

fn combine(&self, other: &Self) -> Self

Combine two elements associatively.

Provided Methods§

Source

fn combine_assign(&mut self, other: &Self)

In-place combine.

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.

Implementations on Foreign Types§

Source§

impl Semigroup for ()

Source§

fn combine(&self, _other: &Self) -> Self

Source§

impl Semigroup for String

Source§

fn combine(&self, other: &Self) -> Self

Source§

impl<A> Semigroup for (A,)
where A: Semigroup,

Source§

fn combine(&self, other: &Self) -> Self

Source§

impl<A, B> Semigroup for (A, B)
where A: Semigroup, B: Semigroup,

Source§

fn combine(&self, other: &Self) -> Self

Source§

impl<A, B, C> Semigroup for (A, B, C)
where A: Semigroup, B: Semigroup, C: Semigroup,

Source§

fn combine(&self, other: &Self) -> Self

Source§

impl<A, B, C, D> Semigroup for (A, B, C, D)
where A: Semigroup, B: Semigroup, C: Semigroup, D: Semigroup,

Source§

fn combine(&self, other: &Self) -> Self

Source§

impl<M: Semigroup + Clone> Semigroup for Option<M>

Source§

fn combine(&self, other: &Self) -> Self

Source§

impl<T: Eq + Hash + Clone> Semigroup for HashSet<T>

Source§

fn combine(&self, other: &Self) -> Self

Source§

impl<T: Ord + Clone> Semigroup for BTreeSet<T>

Source§

fn combine(&self, other: &Self) -> Self

Implementors§

Source§

impl<T: Add<Output = T> + Clone> Semigroup for Sum<T>

Source§

impl<T: Mul<Output = T> + Clone> Semigroup for Product<T>