use crate::api::*;
use std::ops::{Range};
#[derive(Clone)]
pub struct PushBeforeRope<BaseRope, PushFn>
where
BaseRope: RopeMut,
PushFn: Fn(&RopeAction<BaseRope::Cell, BaseRope::Attribute>) -> () {
rope: BaseRope,
push_fn: PushFn
}
impl<BaseRope, PushFn> Rope for PushBeforeRope<BaseRope, PushFn>
where
BaseRope: RopeMut,
PushFn: Fn(&RopeAction<BaseRope::Cell, BaseRope::Attribute>) -> () {
type Cell = BaseRope::Cell;
type Attribute = BaseRope::Attribute;
#[inline]
fn len(&self) -> usize {
self.rope.len()
}
#[inline]
fn read_cells<'a>(&'a self, range: Range<usize>) -> Box<dyn 'a+Iterator<Item=&Self::Cell>> {
self.rope.read_cells(range)
}
#[inline]
fn read_attributes<'a>(&'a self, pos: usize) -> (&'a Self::Attribute, Range<usize>) {
self.rope.read_attributes(pos)
}
}
impl<BaseRope, PushFn> RopeMut for PushBeforeRope<BaseRope, PushFn>
where
BaseRope: RopeMut,
PushFn: Fn(&RopeAction<BaseRope::Cell, BaseRope::Attribute>) -> () {
#[inline]
fn edit(&mut self, action: RopeAction<Self::Cell, Self::Attribute>) {
(self.push_fn)(&action);
self.rope.edit(action);
}
}
#[derive(Clone)]
pub struct PushAfterRope<BaseRope, PushFn>
where
BaseRope: RopeMut,
PushFn: Fn(RopeAction<BaseRope::Cell, BaseRope::Attribute>) -> () {
rope: BaseRope,
push_fn: PushFn
}
impl<BaseRope, PushFn> Rope for PushAfterRope<BaseRope, PushFn>
where
BaseRope: RopeMut,
PushFn: Fn(RopeAction<BaseRope::Cell, BaseRope::Attribute>) -> () {
type Cell = BaseRope::Cell;
type Attribute = BaseRope::Attribute;
#[inline]
fn len(&self) -> usize {
self.rope.len()
}
#[inline]
fn read_cells<'a>(&'a self, range: Range<usize>) -> Box<dyn 'a+Iterator<Item=&Self::Cell>> {
self.rope.read_cells(range)
}
#[inline]
fn read_attributes<'a>(&'a self, pos: usize) -> (&'a Self::Attribute, Range<usize>) {
self.rope.read_attributes(pos)
}
}
impl<BaseRope, PushFn> RopeMut for PushAfterRope<BaseRope, PushFn>
where
BaseRope: RopeMut,
PushFn: Fn(RopeAction<BaseRope::Cell, BaseRope::Attribute>) -> () {
#[inline]
fn edit(&mut self, action: RopeAction<Self::Cell, Self::Attribute>) {
self.rope.edit(action.clone());
(self.push_fn)(action);
}
fn replace<NewCells: IntoIterator<Item=Self::Cell>>(&mut self, range: Range<usize>, new_cells: NewCells) {
let new_cells = new_cells.into_iter().collect::<Vec<_>>();
self.rope.replace(range.clone(), new_cells.clone());
(self.push_fn)(RopeAction::Replace(range, new_cells));
}
fn set_attributes(&mut self, range: Range<usize>, new_attributes: Self::Attribute) {
self.rope.set_attributes(range.clone(), new_attributes.clone());
(self.push_fn)(RopeAction::SetAttributes(range, new_attributes));
}
fn replace_attributes<NewCells: IntoIterator<Item=Self::Cell>>(&mut self, range: Range<usize>, new_cells: NewCells, new_attributes: Self::Attribute) {
let new_cells = new_cells.into_iter().collect::<Vec<_>>();
self.rope.replace_attributes(range.clone(), new_cells.clone(), new_attributes.clone());
(self.push_fn)(RopeAction::ReplaceAttributes(range, new_cells, new_attributes));
}
}
impl<BaseRope, PushFn> PushBeforeRope<BaseRope, PushFn>
where
BaseRope: RopeMut,
PushFn: Fn(&RopeAction<BaseRope::Cell, BaseRope::Attribute>) -> () {
pub fn from(rope: BaseRope, update_fn: PushFn) -> PushBeforeRope<BaseRope, PushFn> {
PushBeforeRope {
rope: rope,
push_fn: update_fn
}
}
}
impl<BaseRope, PushFn> PushAfterRope<BaseRope, PushFn>
where
BaseRope: RopeMut,
PushFn: Fn(RopeAction<BaseRope::Cell, BaseRope::Attribute>) -> () {
pub fn from(rope: BaseRope, update_fn: PushFn) -> PushAfterRope<BaseRope, PushFn> {
PushAfterRope {
rope: rope,
push_fn: update_fn
}
}
}