use super::chunk_storage::ChunkStorage;
use super::id::Id;
use crate::traits::record::Record;
use crate::traits::valid_key::{BorrowedKey, ValidKey};
pub struct Editor<'a, ChunkKey: ?Sized, ItemKey: ?Sized, Element>
where
ChunkKey: BorrowedKey,
ChunkKey::Owned: ValidKey,
ItemKey: BorrowedKey,
ItemKey::Owned: ValidKey,
{
id: Id<&'a ChunkKey, &'a ItemKey>,
idx: usize,
storage: &'a mut ChunkStorage<ChunkKey, ItemKey, Element>,
}
impl<'a, ChunkKey, ItemKey, Element> Editor<'a, ChunkKey, ItemKey, Element>
where
ChunkKey: BorrowedKey + ?Sized,
ChunkKey::Owned: ValidKey,
ItemKey: BorrowedKey + ?Sized,
ItemKey::Owned: ValidKey,
Element: Record<ChunkKey, ItemKey>,
{
pub(super) fn new<'x>(
id: Id<&'a ChunkKey, &'a ItemKey>,
idx: usize,
storage: &'x mut ChunkStorage<ChunkKey, ItemKey, Element>,
) -> Self
where
'x: 'a,
{
Editor { id, idx, storage }
}
pub fn id(&self) -> &Id<&'a ChunkKey, &'a ItemKey> {
&self.id
}
pub fn modify<F>(self, f: F) -> Self
where
F: FnOnce(&mut Element),
{
let element = self.storage.get_idx_mut(self.idx);
f(element);
self
}
pub fn get(&self) -> &Element {
self.storage.get_idx(self.idx)
}
pub fn get_mut(&mut self) -> &mut Element {
self.storage.get_idx_mut(self.idx)
}
}