Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions src/empty_zipper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,20 @@ impl ZipperAbsolutePath for EmptyZipper {
fn root_prefix_path(&self) -> &[u8] { &self.path[..self.path_start_idx] }
}

impl EmptyZipper {
/// Internal. Iteration that finds nothing: back to the root, as every zipper's iteration ends
fn rewind<Obs: PathObserver>(&mut self, obs: &mut Obs) {
let depth = self.depth();
self.reset();
obs.ascend(depth);
}
}

impl ZipperIteration for EmptyZipper {
fn to_next_val_observed<Obs: PathObserver>(&mut self, _obs: &mut Obs) -> bool { false }
fn to_next_val_observed<Obs: PathObserver>(&mut self, obs: &mut Obs) -> bool {
self.rewind(obs);
false
}
fn descend_last_path_observed<Obs: PathObserver>(&mut self, _obs: &mut Obs) -> bool { false }
fn descend_first_k_path_observed<Obs: PathObserver>(&mut self, _k: usize, _obs: &mut Obs) -> bool { false }
fn to_next_k_path_observed<Obs: PathObserver>(&mut self, k: usize, obs: &mut Obs) -> bool {
Expand Down Expand Up @@ -117,11 +129,17 @@ impl<'a, V: Clone + Send + Sync> ZipperReadOnlyConditionalValues<'a, V> for Empt
}

impl<'a, V: Clone + Send + Sync> ZipperReadOnlyIteration<'a, V> for EmptyZipper {
fn to_next_get_val_observed<Obs: PathObserver>(&mut self, _obs: &mut Obs) -> Option<&'a V> { None }
fn to_next_get_val_observed<Obs: PathObserver>(&mut self, obs: &mut Obs) -> Option<&'a V> {
self.rewind(obs);
None
}
}

impl<'a, V: Clone + Send + Sync> ZipperReadOnlyConditionalIteration<'a, V> for EmptyZipper {
fn to_next_get_val_with_witness_observed<'w, Obs: PathObserver>(&mut self, _witness: &'w Self::WitnessT, _obs: &mut Obs) -> Option<&'w V> where 'a: 'w { None }
fn to_next_get_val_with_witness_observed<'w, Obs: PathObserver>(&mut self, _witness: &'w Self::WitnessT, obs: &mut Obs) -> Option<&'w V> where 'a: 'w {
self.rewind(obs);
None
}
}

impl ZipperPathBuffer for EmptyZipper {
Expand Down Expand Up @@ -160,4 +178,34 @@ mod tests {
assert_eq!(z.path(), b"");
assert_eq!(observed, z.path());
}

/// `to_next_val` / `to_next_get_val` / `to_next_get_val_with_witness` find nothing and end back at
/// the root, reporting the ascent, like every other zipper's iteration. They used to stay put
#[test]
fn to_next_val_rewinds_and_reports_movement() {
let new = || {
let mut z = EmptyZipper::new_at_path(b"root.");
z.descend_to(b"abc");
z
};
let mut z = new();
let mut observed = z.path().to_vec();
assert!(!z.to_next_val_observed(&mut observed));
assert_eq!(z.path(), b"");
assert_eq!(observed, z.path());
assert_eq!(z.root_prefix_path(), b"root.");

let mut z = new();
let mut observed = z.path().to_vec();
assert_eq!(ZipperReadOnlyIteration::<u64>::to_next_get_val_observed(&mut z, &mut observed), None);
assert_eq!(z.path(), b"");
assert_eq!(observed, z.path());

let mut z = new();
let mut observed = z.path().to_vec();
let w = ZipperReadOnlyConditionalValues::<u64>::witness(&z);
assert_eq!(ZipperReadOnlyConditionalIteration::<u64>::to_next_get_val_with_witness_observed(&mut z, &w, &mut observed), None);
assert_eq!(z.path(), b"");
assert_eq!(observed, z.path());
}
}