From b553df1b1504e07187c0e1d4b5dd01ae65a5fcc1 Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Fri, 14 Oct 2022 11:37:23 +0200 Subject: Refactor some code to improve coverage --- src/util/slice.rs | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) (limited to 'src/util/slice.rs') diff --git a/src/util/slice.rs b/src/util/slice.rs index 4c942b9..869ecca 100644 --- a/src/util/slice.rs +++ b/src/util/slice.rs @@ -29,18 +29,14 @@ impl<'a> Position<'a> { debug_assert_eq!(exit.kind, Kind::Exit, "expected `exit` event"); let mut enter_index = index - 1; - loop { - let enter = &events[enter_index]; - if enter.kind == Kind::Enter && enter.name == exit.name { - let position = Position { - start: &enter.point, - end: &exit.point, - }; - return position; - } - + while events[enter_index].kind != Kind::Enter || events[enter_index].name != exit.name { enter_index -= 1; } + + Position { + start: &events[enter_index].point, + end: &exit.point, + } } /// Turn a position into indices. @@ -122,6 +118,10 @@ impl<'a> Slice<'a> { /// /// Supports virtual spaces. pub fn serialize(&self) -> String { + debug_assert_eq!(self.after, 0, "expected no trailing vs"); + // If the above ever starts erroring, handle the same as `self.before` + // above but with `self.after`. + // It’d currently be unused code. let mut string = String::with_capacity(self.len()); let mut index = self.before; while index > 0 { @@ -129,10 +129,6 @@ impl<'a> Slice<'a> { index -= 1; } string.push_str(self.as_str()); - debug_assert_eq!(self.after, 0, "expected no trailing vs"); - // If the above ever starts erroring, handle the same as `self.before` - // above but with `self.after`. - // It’d currently be unused code. string } } -- cgit