Skip to main content

Combinable

Trait Combinable 

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

Types that support associative combination.

Implementors must ensure that combination is associative: (a.combine(b)).combine(c) must equal a.combine(b.combine(c)) for all values.

This trait is used to enable pattern combination for Pattern<V> where V: Combinable.

§Laws

Associativity: For all values a, b, c of type Self:

(a.combine(b)).combine(c) == a.combine(b.combine(c))

§Examples

use pattern_core::Combinable;

let s1 = String::from("hello");
let s2 = String::from(" world");
let result = s1.combine(s2);
assert_eq!(result, "hello world");

Required Methods§

Source

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

Combines two values associatively.

§Parameters
  • self - The first value (consumed)
  • other - The second value to combine with (consumed)
§Returns

A new value representing the combination of self and other.

§Laws

Must be associative: (a.combine(b)).combine(c) == a.combine(b.combine(c))

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 Combinable for ()

Combines two unit values (trivial).

Unit combination is trivially associative.

§Examples

use pattern_core::Combinable;

let u1 = ();
let u2 = ();
let result = u1.combine(u2);
assert_eq!(result, ());
Source§

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

Source§

impl Combinable for String

Combines two strings by concatenation.

String concatenation is associative: (a + b) + c = a + (b + c)

§Examples

use pattern_core::Combinable;

let s1 = String::from("hello");
let s2 = String::from(" world");
let result = s1.combine(s2);
assert_eq!(result, "hello world");
Source§

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

Source§

impl<T> Combinable for Vec<T>

Combines two vectors by concatenation.

Vector concatenation is associative: (a ++ b) ++ c = a ++ (b ++ c)

§Examples

use pattern_core::Combinable;

let v1 = vec![1, 2, 3];
let v2 = vec![4, 5];
let result = v1.combine(v2);
assert_eq!(result, vec![1, 2, 3, 4, 5]);
Source§

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

Implementors§

Source§

impl Combinable for EmptySubject

Source§

impl Combinable for FirstSubject

Source§

impl Combinable for LastSubject

Source§

impl Combinable for Subject

Combination strategy for Subject that merges labels and properties.

This strategy combines two subjects by:

  • Using the identity from the first subject
  • Taking the union of labels from both subjects
  • Merging properties (values from the second subject overwrite the first)

§Semigroup Laws

This implementation satisfies associativity:

  • Identity choice is associative (always picks leftmost)
  • Label union is associative (set union is associative)
  • Property merge is associative with right-bias (latter values win)

§Examples

use pattern_core::{Subject, Symbol, Combinable};
use std::collections::{HashMap, HashSet};

let s1 = Subject {
    identity: Symbol("n1".to_string()),
    labels: {
        let mut s = HashSet::new();
        s.insert("Person".to_string());
        s
    },
    properties: HashMap::new(),
};

let s2 = Subject {
    identity: Symbol("n2".to_string()),
    labels: {
        let mut s = HashSet::new();
        s.insert("Employee".to_string());
        s
    },
    properties: HashMap::new(),
};

// Merge combines labels and uses first identity
let merged = s1.combine(s2);
assert_eq!(merged.identity.0, "n1");
assert!(merged.labels.contains("Person"));
assert!(merged.labels.contains("Employee"));