editer 0.2.0

In-place, simultaneous iteration and mutation of collections
Documentation
editer-0.2.0 has been yanked.

Editer

Editer allows mutating a collection in place while iterating over it.

Quick links

Usage

The edit function iterates over a given List. For each item in the list, it calls a given function with a Slot. The Slot can be used to access the current item and/or mutate the list at the current position. You can:

  • Insert zero or more new items before or after the current item using Slot::insert_before or Slot::insert_after.

    use editer::edit;
    
    let mut items = vec![1, 2, 3, 4, 5];
    
    edit(&mut items, |item| {
        if item == 2 {
            item.insert_after([6, 7]);
        } else if item == 3 {
            item.insert_before([8, 9]);
        }
    });
    
    assert_eq!(items, vec![1, 2, 6, 7, 8, 9, 3, 4, 5]);
    
  • Replace the current item with zero or more new items using DerefMut::deref_mut or Slot::replace.

    let mut items = vec![1, 2, 3, 4, 5];
    
    edit(&mut items, |mut item| {
        if item == 2 {
            *item = 6;
        }
    
        if item == 3 {
            item.replace([7, 8, 9]);
        }
    });
    
    assert_eq!(items, vec![1, 6, 7, 8, 9, 4, 5]);
    
  • Remove the current item using Slot::remove.

    let mut items = vec![1, 2, 3, 4, 5];
    
    edit(&mut items, |item| {
        if item == 3 {
            item.remove();
        }
    });
    
    assert_eq!(items, vec![1, 2, 4, 5]);
    

try_edit is the fallible version of edit. It applies the given editor function to each item in the given list, like edit. It stops at the first error and returns it.

use editer::try_edit;

let mut items = vec![1, 2, 3, 4, 5];

let result = try_edit(&mut items, |item| {
    if item == 4 {
        Err("Whoops!")
    } else {
        item.remove();
        Ok(())
    }
});

assert_eq!(result, Err("Whoops!"));
assert_eq!(items, vec![4, 5]);

Requirements

Editer requires Rust 1.57 or newer.

License

Editer is distributed under the terms of the MIT License. See LICENSE for details.