pub mod list;
pub mod list_base;
pub mod map_base;
pub use list::IList;
use crate::index::store::Filterable;
use std::marker::PhantomData;
pub trait Editable<I> {
type Index;
fn update<U>(&mut self, index: Self::Index, update: U) -> Option<&I>
where
U: FnMut(&mut I);
fn remove(&mut self, index: Self::Index) -> Option<I>;
}
pub struct Editor<'a, I, E> {
editor: &'a mut E,
_items: PhantomData<I>,
}
impl<'a, I, E> Editor<'a, I, E>
where
E: Editable<I, Index = usize> + Filterable<Index = usize>,
{
pub fn new(editor: &'a mut E) -> Self {
Self {
editor,
_items: PhantomData,
}
}
pub fn update_by_key<U>(&mut self, key: &E::Key, mut update: U)
where
U: FnMut(&mut I),
{
#[allow(clippy::unnecessary_to_owned)]
for idx in self.editor.get(key).to_vec() {
self.editor.update(idx, &mut update);
}
}
pub fn update_by_key_with_cb<U, C>(&mut self, key: &E::Key, mut update: U, mut callback: C)
where
U: FnMut(&mut I),
C: FnMut(&I),
{
#[allow(clippy::unnecessary_to_owned)]
for idx in self.editor.get(key).to_vec() {
if let Some(item) = self.editor.update(idx, &mut update) {
callback(item);
}
}
}
pub fn remove_by_key(&mut self, key: &E::Key) {
while let Some(idx) = self.editor.get(key).iter().next() {
self.editor.remove(*idx);
}
}
pub fn remove_by_key_with_cb<C>(&mut self, key: &E::Key, mut callback: C)
where
C: FnMut(I),
{
while let Some(idx) = self.editor.get(key).iter().next() {
if let Some(item) = self.editor.remove(*idx) {
callback(item);
}
}
}
}