Trait Semigroup

Source
pub trait Semigroup {
    // Required method
    fn combine(self, rhs: Self) -> Self;
}
Expand description

The trait combines two types into another one.

The combining of several types must be associative, meaning that they can be evaluated in any order. first.combine(second.combine(third)) == first.combine(second).combine(third)

Required Methods§

Source

fn combine(self, rhs: Self) -> Self

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 Ordering

This combines two Ordering operations into one. ‘first.cmp(second)’ for example gives back an Ordering. If we have several types you wish to compare say name and age for example. We can first order by name and then age.

§Examples

use partial_functional::semigroup::Semigroup;

struct Person {
    name: String,
    age: u8,
}

let first = Person { name: String::from("Chris"), age: 43 };
let second = Person { name: String::from("Chris"), age: 23 };

let fst_compared_to_snd = first.name.cmp(&second.name).combine(first.age.cmp(&second.age));

assert_eq!(Ordering::Greater, fst_compared_to_snd);
Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for f32

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for f64

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for i8

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for i16

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for i32

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for i64

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for i128

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for isize

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for u8

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for u16

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for u32

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for u64

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for u128

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for usize

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl Semigroup for String

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl<T> Semigroup for Vec<T>

Source§

fn combine(self, rhs: Self) -> Self

Source§

impl<T> Semigroup for PhantomData<T>

Source§

fn combine(self, _rhs: Self) -> Self

Source§

impl<T, E> Semigroup for Result<T, E>

Returns the first Result if it’s an Ok variant, otherwise returns the second

§Examples

use partial_functional::semigroup::Semigroup;

let five: Result<u32, &'static str> = Ok(5);
let two_kb: Result<u32, &'static str> = Ok(2048);
let err: Result<u32, &'static str> = Err("An error occured");
let err_again: Result<u32, &'static str> = Err("Another error");

assert_eq!(Ok(5), five.combine(err.clone()));
assert_eq!(Ok(2048), two_kb.combine(five.clone()));
assert_eq!(Ok(2048), two_kb.combine(five));
assert_eq!(Err("Another error"), err.combine(err_again));
Source§

fn combine(self, rhs: Self) -> Self

Source§

impl<T: Semigroup> Semigroup for Option<T>

Returns the combination of both Some variants, if one is None then the other is returned.

use partial_functional::semigroup::Semigroup;

let five = Some(5);
let ten = Some(10);

assert_eq!(Some(15), five.combine(ten));
assert_eq!(Some(5), five.combine(None));
assert_eq!(Some(10), None.combine(ten));
Source§

fn combine(self, rhs: Self) -> Self

Implementors§

Source§

impl Semigroup for All

Source§

impl Semigroup for Any

Source§

impl<T> Semigroup for First<T>

Source§

impl<T> Semigroup for Last<T>

Source§

impl<T: Ord> Semigroup for Max<T>

Source§

impl<T: Ord> Semigroup for Min<T>

Source§

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

Source§

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