pub trait Semigroup: Sized {
// Required method
fn combine(self, other: Self) -> Self;
}Expand description
A type that supports an associative binary operation
§Laws
Implementations must satisfy the associativity law:
a.combine(b).combine(c) == a.combine(b.combine(c))§Note on Ownership
The combine method takes self by value, not by reference. If you need to
preserve the original values, you must clone them before combining.
Required Methods§
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<K, V> Semigroup for BTreeMap<K, V>
Semigroup for BTreeMap that merges maps, combining values with the same key.
impl<K, V> Semigroup for BTreeMap<K, V>
Semigroup for BTreeMap that merges maps, combining values with the same key.
Same semantics as HashMap but maintains ordered keys.
§Example
use std::collections::BTreeMap;
use stillwater::Semigroup;
let mut map1 = BTreeMap::new();
map1.insert("a", vec![1, 2]);
let mut map2 = BTreeMap::new();
map2.insert("a", vec![3, 4]);
map2.insert("b", vec![5]);
let combined = map1.combine(map2);
assert_eq!(combined.get("a"), Some(&vec![1, 2, 3, 4]));
assert_eq!(combined.get("b"), Some(&vec![5]));Source§impl<K, V> Semigroup for HashMap<K, V>
Semigroup for HashMap that merges maps, combining values with the same key.
impl<K, V> Semigroup for HashMap<K, V>
Semigroup for HashMap that merges maps, combining values with the same key.
When keys conflict, values are combined using their Semigroup instance.
§Example
use std::collections::HashMap;
use stillwater::Semigroup;
let mut map1 = HashMap::new();
map1.insert("errors", vec!["error1"]);
map1.insert("warnings", vec!["warn1"]);
let mut map2 = HashMap::new();
map2.insert("errors", vec!["error2"]);
map2.insert("info", vec!["info1"]);
let combined = map1.combine(map2);
// Result:
// {
// "errors": ["error1", "error2"], // Combined
// "warnings": ["warn1"], // From map1
// "info": ["info1"] // From map2
// }Source§impl<T1, T2, T3> Semigroup for (T1, T2, T3)
impl<T1, T2, T3> Semigroup for (T1, T2, T3)
fn combine(self, other: (T1, T2, T3)) -> (T1, T2, T3)
Source§impl<T1, T2, T3, T4> Semigroup for (T1, T2, T3, T4)
impl<T1, T2, T3, T4> Semigroup for (T1, T2, T3, T4)
fn combine(self, other: (T1, T2, T3, T4)) -> (T1, T2, T3, T4)
Source§impl<T1, T2, T3, T4, T5> Semigroup for (T1, T2, T3, T4, T5)
impl<T1, T2, T3, T4, T5> Semigroup for (T1, T2, T3, T4, T5)
fn combine(self, other: (T1, T2, T3, T4, T5)) -> (T1, T2, T3, T4, T5)
Source§impl<T1, T2, T3, T4, T5, T6> Semigroup for (T1, T2, T3, T4, T5, T6)
impl<T1, T2, T3, T4, T5, T6> Semigroup for (T1, T2, T3, T4, T5, T6)
fn combine(self, other: (T1, T2, T3, T4, T5, T6)) -> (T1, T2, T3, T4, T5, T6)
Source§impl<T1, T2, T3, T4, T5, T6, T7> Semigroup for (T1, T2, T3, T4, T5, T6, T7)
impl<T1, T2, T3, T4, T5, T6, T7> Semigroup for (T1, T2, T3, T4, T5, T6, T7)
fn combine( self, other: (T1, T2, T3, T4, T5, T6, T7), ) -> (T1, T2, T3, T4, T5, T6, T7)
Source§impl<T1, T2, T3, T4, T5, T6, T7, T8> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8)
impl<T1, T2, T3, T4, T5, T6, T7, T8> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8)
fn combine( self, other: (T1, T2, T3, T4, T5, T6, T7, T8), ) -> (T1, T2, T3, T4, T5, T6, T7, T8)
Source§impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8, T9)
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8, T9)
fn combine( self, other: (T1, T2, T3, T4, T5, T6, T7, T8, T9), ) -> (T1, T2, T3, T4, T5, T6, T7, T8, T9)
Source§impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
fn combine( self, other: (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10), ) -> (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10)
Source§impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11)
Source§impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
impl<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12> Semigroup for (T1, T2, T3, T4, T5, T6, T7, T8, T9, T10, T11, T12)
Source§impl<T> Semigroup for Option<T>where
T: Semigroup,
Semigroup for Option that lifts the inner Semigroup.
impl<T> Semigroup for Option<T>where
T: Semigroup,
Semigroup for Option that lifts the inner Semigroup.
Some(a).combine(Some(b))=Some(a.combine(b))Some(a).combine(None)=Some(a)None.combine(Some(b))=Some(b)None.combine(None)=None
§Example
use stillwater::Semigroup;
let opt1 = Some(vec![1, 2]);
let opt2 = Some(vec![3, 4]);
let result = opt1.combine(opt2);
assert_eq!(result, Some(vec![1, 2, 3, 4]));
let none: Option<Vec<i32>> = None;
let some = Some(vec![1, 2]);
assert_eq!(none.combine(some.clone()), some);Source§impl<T> Semigroup for BTreeSet<T>where
T: Ord,
Semigroup for BTreeSet using union.
impl<T> Semigroup for BTreeSet<T>where
T: Ord,
Semigroup for BTreeSet using union.
Same semantics as HashSet but maintains ordered elements.
§Example
use std::collections::BTreeSet;
use stillwater::Semigroup;
let set1: BTreeSet<_> = [1, 2, 3].iter().cloned().collect();
let set2: BTreeSet<_> = [3, 4, 5].iter().cloned().collect();
let combined = set1.combine(set2);
assert_eq!(combined.len(), 5); // {1, 2, 3, 4, 5}Source§impl<T> Semigroup for HashSet<T>
Semigroup for HashSet using union.
impl<T> Semigroup for HashSet<T>
Semigroup for HashSet using union.
§Example
use std::collections::HashSet;
use stillwater::Semigroup;
let set1: HashSet<_> = [1, 2, 3].iter().cloned().collect();
let set2: HashSet<_> = [3, 4, 5].iter().cloned().collect();
let combined = set1.combine(set2);
assert_eq!(combined.len(), 5); // {1, 2, 3, 4, 5}