pub mod cursor_list;
pub use self::cursor_list::CursorList;
use std::borrow::Borrow;
use std::cmp::Ordering;
pub trait MyTrait<'a> : Ord {
type Owned;
fn into_owned(self) -> Self::Owned;
fn clone_onto(&self, other: &mut Self::Owned);
fn compare(&self, other: &Self::Owned) -> Ordering;
fn less_equals(&self, other: &Self::Owned) -> bool {
self.compare(other) != Ordering::Greater
}
fn equals(&self, other: &Self::Owned) -> bool {
self.compare(other) == Ordering::Equal
}
fn less_than(&self, other: &Self::Owned) -> bool {
self.compare(other) == Ordering::Less
}
fn borrow_as(other: &'a Self::Owned) -> Self;
}
impl<'a, T: Ord+ToOwned+?Sized> MyTrait<'a> for &'a T {
type Owned = T::Owned;
fn into_owned(self) -> Self::Owned { self.to_owned() }
fn clone_onto(&self, other: &mut Self::Owned) { <T as ToOwned>::clone_into(self, other) }
fn compare(&self, other: &Self::Owned) -> Ordering { self.cmp(&other.borrow()) }
fn borrow_as(other: &'a Self::Owned) -> Self {
other.borrow()
}
}
pub trait Cursor {
type Key<'a>: Copy + Clone + MyTrait<'a, Owned = Self::KeyOwned>;
type KeyOwned: Ord + Clone;
type Val<'a>: Copy + Clone + MyTrait<'a, Owned = Self::ValOwned> + for<'b> PartialOrd<Self::Val<'b>>;
type ValOwned: Ord + Clone;
type Time;
type Diff: ?Sized;
type Storage;
fn key_valid(&self, storage: &Self::Storage) -> bool;
fn val_valid(&self, storage: &Self::Storage) -> bool;
fn key<'a>(&self, storage: &'a Self::Storage) -> Self::Key<'a>;
fn val<'a>(&self, storage: &'a Self::Storage) -> Self::Val<'a>;
fn get_key<'a>(&self, storage: &'a Self::Storage) -> Option<Self::Key<'a>> {
if self.key_valid(storage) { Some(self.key(storage)) } else { None }
}
fn get_val<'a>(&self, storage: &'a Self::Storage) -> Option<Self::Val<'a>> {
if self.val_valid(storage) { Some(self.val(storage)) } else { None }
}
fn map_times<L: FnMut(&Self::Time, &Self::Diff)>(&mut self, storage: &Self::Storage, logic: L);
fn step_key(&mut self, storage: &Self::Storage);
fn seek_key(&mut self, storage: &Self::Storage, key: Self::Key<'_>);
fn seek_key_owned(&mut self, storage: &Self::Storage, key: &Self::KeyOwned) {
self.seek_key(storage, MyTrait::borrow_as(key));
}
fn step_val(&mut self, storage: &Self::Storage);
fn seek_val(&mut self, storage: &Self::Storage, val: Self::Val<'_>);
fn rewind_keys(&mut self, storage: &Self::Storage);
fn rewind_vals(&mut self, storage: &Self::Storage);
fn to_vec(&mut self, storage: &Self::Storage) -> Vec<((Self::KeyOwned, Self::ValOwned), Vec<(Self::Time, Self::Diff)>)>
where
Self::Time: Clone,
Self::Diff: Clone,
{
let mut out = Vec::new();
self.rewind_keys(storage);
self.rewind_vals(storage);
while self.key_valid(storage) {
while self.val_valid(storage) {
let mut kv_out = Vec::new();
self.map_times(storage, |ts, r| {
kv_out.push((ts.clone(), r.clone()));
});
out.push(((self.key(storage).into_owned(), self.val(storage).into_owned()), kv_out));
self.step_val(storage);
}
self.step_key(storage);
}
out
}
}