// Merges the given item into a mutable layer.
Merges the given item into a mutable layer.
Merges the given item into a mutable layer .
pub(super) async fn merge_into<K: Debug + OrdLowerBound, V: Debug>(
    mut_iter: Box<dyn LayerIteratorMut<K, V> + '_>,
    item: Item<K, V>,
    merge_fn: MergeFn<K, V>,
) -> Result<(), Error> {
    let merge_item = if mut_iter.get().is_some() { MergeItem::Iter } else { MergeItem::None };
    let mut mut_merge_iter = MergeLayerIterator {
        layer: None,
        iter: RawIterator::Mut(mut_iter),
        layer_index: 1,
        item: merge_item,
    };
    let mut item_merge_iter = MergeLayerIterator::new_with_item(0, MergeItem::Item(item));
    while mut_merge_iter.is_some() && item_merge_iter.is_some() {
        if mut_merge_iter > item_merge_iter {
            // In this branch the mutable layer is left and the item we're merging-in is right.
            let merge_result = merge_fn(&mut_merge_iter, &item_merge_iter);
            log::debug!(
                "(1) merge for {:?} {:?} -> {:?}",
                mut_merge_iter.key(),
                item_merge_iter.key(),
                merge_result
            );
            match merge_result {
                MergeResult::EmitLeft => {
                    if let Some(item) = mut_merge_iter.take_item() {
                        mut_merge_iter.insert(item);
                        mut_merge_iter.set_item_from_iter();
                    } else {
                        mut_merge_iter.advance().await?;
                    }
                }
                MergeResult::Other { emit, left, right } => {
                    if let Some(emit) = emit {
                        mut_merge_iter.insert(emit);
                    }
                    match left {
                        ItemOp::Keep => {}
                        ItemOp::Discard => {
                            if let MergeItem::Iter = mut_merge_iter.item {
                                mut_merge_iter.erase();
                            }
                            mut_merge_iter.set_item_from_iter();
                        }
                        ItemOp::Replace(item) => {
                            if let MergeItem::Iter = mut_merge_iter.item {
                                mut_merge_iter.erase();
                            }
                            mut_merge_iter.item = MergeItem::Item(item)
                        }
                    }
                    match right {
                        ItemOp::Keep => {}
                        ItemOp::Discard => item_merge_iter.item = MergeItem::None,
                        ItemOp::Replace(item) => item_merge_iter.item = MergeItem::Item(item),
                    }
                }
            }
        } else {
            // In this branch, the item we're merging-in is left and the mutable layer is right.
            let merge_result = merge_fn(&item_merge_iter, &mut_merge_iter);
            log::debug!(
                "(2) merge for {:?} {:?} -> {:?}",
                item_merge_iter.key(),
                mut_merge_iter.key(),
                merge_result
            );
            match merge_result {
                MergeResult::EmitLeft => break, // Item is inserted outside the loop
                MergeResult::Other { emit, left, right } => {
                    if let Some(emit) = emit {
                        mut_merge_iter.insert(emit);
                    }
                    match left {
                        ItemOp::Keep => {}
                        ItemOp::Discard => item_merge_iter.item = MergeItem::None,
                        ItemOp::Replace(item) => item_merge_iter.item = MergeItem::Item(item),
                    }
                    match right {
                        ItemOp::Keep => {}
                        ItemOp::Discard => {
                            if let MergeItem::Iter = mut_merge_iter.item {
                                mut_merge_iter.erase();
                            }
                            mut_merge_iter.set_item_from_iter();
                        }
                        ItemOp::Replace(item) => {
                            if let MergeItem::Iter = mut_merge_iter.item {
                                mut_merge_iter.erase();
                            }
                            mut_merge_iter.item = MergeItem::Item(item)
                        }
                    }
                }
            }
        }
    } // while ...
      // The only way we could get here with both items is via the break above, so we know the
      // correct order required here.
    if let MergeItem::Item(item) = item_merge_iter.item {
        mut_merge_iter.insert(item);
    }
    if let Some(item) = mut_merge_iter.take_item() {
        mut_merge_iter.insert(item);
    }
    mut_merge_iter.commit().await;
    Ok(())
}
pub ( super ) async fn merge_into < K : Debug + OrdLowerBound , V : Debug > ( mut_iter : Box < dyn LayerIteratorMut < K , V > + ' _ > , item : Item < K , V > , merge_fn : MergeFn < K , V > , ) -> Result < ( ) , Error > { let merge_item = if mut_iter . get ( ) . is_some ( ) { MergeItem :: Iter } else { MergeItem :: None } ; let mut mut_merge_iter = MergeLayerIterator { layer : None , iter : RawIterator :: Mut ( mut_iter ) , layer_index : 1 , item : merge_item , } ; let mut item_merge_iter = MergeLayerIterator :: new_with_item ( 0 , MergeItem :: Item ( item ) ) ; while mut_merge_iter . is_some ( ) && item_merge_iter . is_some ( ) { if mut_merge_iter > item_merge_iter { // In this branch the mutable layer is left and the item we're merging-in is right. let merge_result = merge_fn ( & mut_merge_iter , & item_merge_iter ) ; log :: debug ! ( " " mut_merge_iter key ( ) item_merge_iter key ( ) merge_result ) ; match merge_result { MergeResult :: EmitLeft => { if let Some ( item ) = mut_merge_iter . take_item ( ) { mut_merge_iter . insert ( item ) ; mut_merge_iter . set_item_from_iter ( ) ; } else { mut_merge_iter . advance ( ) . await ? ; } } MergeResult :: Other { emit , left , right } => { if let Some ( emit ) = emit { mut_merge_iter . insert ( emit ) ; } match left { ItemOp :: Keep => { } ItemOp :: Discard => { if let MergeItem :: Iter = mut_merge_iter . item { mut_merge_iter . erase ( ) ; } mut_merge_iter . set_item_from_iter ( ) ; } ItemOp :: Replace ( item ) => { if let MergeItem :: Iter = mut_merge_iter . item { mut_merge_iter . erase ( ) ; } mut_merge_iter . item = MergeItem :: Item ( item ) } } match right { ItemOp :: Keep => { } ItemOp :: Discard => item_merge_iter . item = MergeItem :: None , ItemOp :: Replace ( item ) => item_merge_iter . item = MergeItem :: Item ( item ) , } } } } else { // In this branch, the item we're merging-in is left and the mutable layer is right. let merge_result = merge_fn ( & item_merge_iter , & mut_merge_iter ) ; log :: debug ! ( " " item_merge_iter key ( ) mut_merge_iter key ( ) merge_result ) ; match merge_result { MergeResult :: EmitLeft => break , // Item is inserted outside the loop MergeResult :: Other { emit , left , right } => { if let Some ( emit ) = emit { mut_merge_iter . insert ( emit ) ; } match left { ItemOp :: Keep => { } ItemOp :: Discard => item_merge_iter . item = MergeItem :: None , ItemOp :: Replace ( item ) => item_merge_iter . item = MergeItem :: Item ( item ) , } match right { ItemOp :: Keep => { } ItemOp :: Discard => { if let MergeItem :: Iter = mut_merge_iter . item { mut_merge_iter . erase ( ) ; } mut_merge_iter . set_item_from_iter ( ) ; } ItemOp :: Replace ( item ) => { if let MergeItem :: Iter = mut_merge_iter . item { mut_merge_iter . erase ( ) ; } mut_merge_iter . item = MergeItem :: Item ( item ) } } } } } } // while ... // The only way we could get here with both items is via the break above, so we know the // correct order required here. if let MergeItem :: Item ( item ) = item_merge_iter . item { mut_merge_iter . insert ( item ) ; } if let Some ( item ) = mut_merge_iter . take_item ( ) { mut_merge_iter . insert ( item ) ; } mut_merge_iter . commit ( ) . await ; Ok ( ( ) ) }
Merges the given item into a mutable layer.
Merges the given item into a mutable layer .
mut_iter
Box<dyn LayerIteratorMut<K, V> + '_>
item
Item<K, V>
merge_fn
MergeFn<K, V>