Skip to main content

Mergeable

Trait Mergeable 

Source
pub trait Mergeable {
    // Required methods
    fn can_merge(&self, other: &Self) -> bool;
    fn merge(self, other: Self) -> Self;
}
Expand description

Trait for operations that can be merged (coalesced)

Implement this trait to enable automatic merging of consecutive similar operations (e.g., multiple character insertions into one).

§Example

impl Mergeable for TextOp {
    fn can_merge(&self, other: &Self) -> bool {
        match (self, other) {
            (TextOp::Insert { pos: p1, .. }, TextOp::Insert { pos: p2, .. }) => {
                // Can merge if inserting at consecutive positions
                *p2 == *p1 + 1
            }
            _ => false,
        }
    }

    fn merge(self, other: Self) -> Self {
        match (self, other) {
            (TextOp::Insert { pos, text: mut t1 }, TextOp::Insert { text: t2, .. }) => {
                t1.push_str(&t2);
                TextOp::Insert { pos, text: t1 }
            }
            _ => self,
        }
    }
}

Required Methods§

Source

fn can_merge(&self, other: &Self) -> bool

Check if this operation can be merged with other

self is the older operation, other is the newer one.

Source

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

Merge two operations into one

self is the older operation, other is the newer one. Returns a single operation that represents both.

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§