use std::marker;
use crate::core::pmmr::{round_up_to_leaf_pos, Backend, ReadonlyPMMR};
use crate::ser::PMMRable;
pub struct RewindablePMMR<'a, T, B>
where
T: PMMRable,
B: Backend<T>,
{
last_pos: u64,
backend: &'a B,
_marker: marker::PhantomData<T>,
}
impl<'a, T, B> RewindablePMMR<'a, T, B>
where
T: PMMRable,
B: 'a + Backend<T>,
{
pub fn new(backend: &'a B) -> RewindablePMMR<'a, T, B> {
RewindablePMMR {
backend,
last_pos: 0,
_marker: marker::PhantomData,
}
}
pub fn at(backend: &'a B, last_pos: u64) -> RewindablePMMR<'a, T, B> {
RewindablePMMR {
backend,
last_pos,
_marker: marker::PhantomData,
}
}
pub fn rewind(&mut self, position: u64) -> Result<(), String> {
self.last_pos = round_up_to_leaf_pos(position);
Ok(())
}
pub fn as_readonly(&self) -> ReadonlyPMMR<'a, T, B> {
ReadonlyPMMR::at(&self.backend, self.last_pos)
}
}