From af4c5792b3d573f4be7583c8f14f77cb7f63abfa Mon Sep 17 00:00:00 2001 From: Manos Pitsidianakis Date: Sat, 1 Feb 2020 14:31:22 +0200 Subject: [PATCH] ui: remove unnecessary unreachable panics in set_and_join_box --- ui/src/components.rs | 51 ++++++++++++--------------- ui/src/components/indexer.rs | 2 +- ui/src/components/mail/status.rs | 2 +- ui/src/components/mail/view/thread.rs | 44 +++++++++++++---------- 4 files changed, 51 insertions(+), 48 deletions(-) diff --git a/ui/src/components.rs b/ui/src/components.rs index efc03da91..3ee8cd984 100644 --- a/ui/src/components.rs +++ b/ui/src/components.rs @@ -107,22 +107,6 @@ pub trait Component: Display + Debug + Send { } } -/* -pub(crate) fn is_box_char(ch: char) -> bool { - match ch { - HORZ_BOUNDARY | VERT_BOUNDARY => true, - _ => false, - } -} - - * pub(crate) fn is_box_char(ch: char) -> bool { - * match ch { - * '└' | '─' | '┘' | '┴' | '┌' | '│' | '├' | '┐' | '┬' | '┤' | '┼' | '╷' | '╵' | '╴' | '╶' => true, - * _ => false, - * } - * } - */ - fn bin_to_ch(b: u32) -> char { match b { 0b0001 => '╶', @@ -140,7 +124,7 @@ fn bin_to_ch(b: u32) -> char { 0b1101 => '┬', 0b1110 => '┤', 0b1111 => '┼', - x => unreachable!(format!("unreachable bin_to_ch(x), x = {:b}", x)), + _ => unsafe { std::hint::unreachable_unchecked() }, } } @@ -363,7 +347,12 @@ fn set_and_join_horz(grid: &mut CellBuffer, idx: Pos) -> u32 { bin_set } -pub(crate) fn set_and_join_box(grid: &mut CellBuffer, idx: Pos, ch: char) { +pub enum BoxBoundary { + Horizontal, + Vertical, +} + +pub(crate) fn set_and_join_box(grid: &mut CellBuffer, idx: Pos, ch: BoxBoundary) { /* Connected sides: * * 1 @@ -376,9 +365,8 @@ pub(crate) fn set_and_join_box(grid: &mut CellBuffer, idx: Pos, ch: char) { if grid.ascii_drawing { grid[idx].set_ch(match ch { - '│' => '|', - '─' => '-', - _ => unreachable!(), + BoxBoundary::Vertical => '|', + BoxBoundary::Horizontal => '-', }); grid[idx].set_fg(Color::Byte(240)); @@ -386,9 +374,8 @@ pub(crate) fn set_and_join_box(grid: &mut CellBuffer, idx: Pos, ch: char) { } let bin_set = match ch { - '│' => set_and_join_vert(grid, idx), - '─' => set_and_join_horz(grid, idx), - _ => unreachable!(), + BoxBoundary::Vertical => set_and_join_vert(grid, idx), + BoxBoundary::Horizontal => set_and_join_horz(grid, idx), }; grid[idx].set_ch(bin_to_ch(bin_set)); @@ -414,9 +401,17 @@ pub fn create_box(grid: &mut CellBuffer, area: Area) { grid[(get_x(bottom_right), y)].set_ch(VERT_BOUNDARY); grid[(get_x(bottom_right), y)].set_fg(Color::Byte(240)); } - set_and_join_box(grid, upper_left, HORZ_BOUNDARY); - set_and_join_box(grid, set_x(upper_left, get_x(bottom_right)), HORZ_BOUNDARY); - set_and_join_box(grid, set_y(upper_left, get_y(bottom_right)), VERT_BOUNDARY); - set_and_join_box(grid, bottom_right, VERT_BOUNDARY); + set_and_join_box(grid, upper_left, BoxBoundary::Horizontal); + set_and_join_box( + grid, + set_x(upper_left, get_x(bottom_right)), + BoxBoundary::Horizontal, + ); + set_and_join_box( + grid, + set_y(upper_left, get_y(bottom_right)), + BoxBoundary::Vertical, + ); + set_and_join_box(grid, bottom_right, BoxBoundary::Vertical); } } diff --git a/ui/src/components/indexer.rs b/ui/src/components/indexer.rs index c18835a34..e545fc2f0 100644 --- a/ui/src/components/indexer.rs +++ b/ui/src/components/indexer.rs @@ -77,7 +77,7 @@ impl Component for Indexer { let index_entity_width = (30 * total_cols) / 100; let mid = get_x(bottom_right) - index_entity_width; for i in get_y(upper_left)..=get_y(bottom_right) { - set_and_join_box(grid, (mid, i), VERT_BOUNDARY); + set_and_join_box(grid, (mid, i), BoxBoundary::Vertical); } let left_menu_area = (upper_left, (set_x(bottom_right, mid - 1))); diff --git a/ui/src/components/mail/status.rs b/ui/src/components/mail/status.rs index aca38b978..182e664e2 100644 --- a/ui/src/components/mail/status.rs +++ b/ui/src/components/mail/status.rs @@ -252,7 +252,7 @@ impl StatusPanel { for (i, a) in context.accounts.iter().enumerate() { for x in 2..(120 - 1) { - set_and_join_box(&mut self.content, (x, 12 + i * 10), HORZ_BOUNDARY); + set_and_join_box(&mut self.content, (x, 12 + i * 10), BoxBoundary::Horizontal); } //create_box(&mut self.content, ((2, 5 + i * 10), (120 - 1, 15 + i * 10))); let (x, y) = write_string_to_grid( diff --git a/ui/src/components/mail/view/thread.rs b/ui/src/components/mail/view/thread.rs index 3275d98a2..bfdad2de7 100644 --- a/ui/src/components/mail/view/thread.rs +++ b/ui/src/components/mail/view/thread.rs @@ -245,11 +245,11 @@ impl ThreadView { set_and_join_box( &mut content, (e.index.0 * 4 + ctr, 2 * y - 1), - HORZ_BOUNDARY, + BoxBoundary::Horizontal, ); ctr += 1; } - set_and_join_box(&mut content, index, HORZ_BOUNDARY); + set_and_join_box(&mut content, index, BoxBoundary::Horizontal); } } write_string_to_grid( @@ -288,13 +288,17 @@ impl ThreadView { let bg_color = Color::Default; change_colors(&mut content, area, fg_color, bg_color); } - set_and_join_box(&mut content, (e.index.0 * 4, 2 * y), VERT_BOUNDARY); - set_and_join_box(&mut content, (e.index.0 * 4, 2 * y + 1), VERT_BOUNDARY); + set_and_join_box(&mut content, (e.index.0 * 4, 2 * y), BoxBoundary::Vertical); + set_and_join_box( + &mut content, + (e.index.0 * 4, 2 * y + 1), + BoxBoundary::Vertical, + ); for i in ((e.index.0 * 4) + 1)..width - 1 { - set_and_join_box(&mut content, (i, 2 * y + 1), HORZ_BOUNDARY); + set_and_join_box(&mut content, (i, 2 * y + 1), BoxBoundary::Horizontal); } - set_and_join_box(&mut content, (width - 1, 2 * y), VERT_BOUNDARY); - set_and_join_box(&mut content, (width - 1, 2 * y + 1), VERT_BOUNDARY); + set_and_join_box(&mut content, (width - 1, 2 * y), BoxBoundary::Vertical); + set_and_join_box(&mut content, (width - 1, 2 * y + 1), BoxBoundary::Vertical); } } else { for (y, e) in self.entries.iter().enumerate() { @@ -323,11 +327,11 @@ impl ThreadView { set_and_join_box( &mut content, (e.index.0 * 4 + ctr, 2 * y - 1), - HORZ_BOUNDARY, + BoxBoundary::Horizontal, ); ctr += 1; } - set_and_join_box(&mut content, index, HORZ_BOUNDARY); + set_and_join_box(&mut content, index, BoxBoundary::Horizontal); } } write_string_to_grid( @@ -366,17 +370,21 @@ impl ThreadView { let bg_color = Color::Default; change_colors(&mut content, area, fg_color, bg_color); } - set_and_join_box(&mut content, (e.index.0 * 4, 2 * y), VERT_BOUNDARY); - set_and_join_box(&mut content, (e.index.0 * 4, 2 * y + 1), VERT_BOUNDARY); + set_and_join_box(&mut content, (e.index.0 * 4, 2 * y), BoxBoundary::Vertical); + set_and_join_box( + &mut content, + (e.index.0 * 4, 2 * y + 1), + BoxBoundary::Vertical, + ); for i in ((e.index.0 * 4) + 1)..width - 1 { - set_and_join_box(&mut content, (i, 2 * y + 1), HORZ_BOUNDARY); + set_and_join_box(&mut content, (i, 2 * y + 1), BoxBoundary::Horizontal); } - set_and_join_box(&mut content, (width - 1, 2 * y), VERT_BOUNDARY); - set_and_join_box(&mut content, (width - 1, 2 * y + 1), VERT_BOUNDARY); + set_and_join_box(&mut content, (width - 1, 2 * y), BoxBoundary::Vertical); + set_and_join_box(&mut content, (width - 1, 2 * y + 1), BoxBoundary::Vertical); } for y in 0..height - 1 { - set_and_join_box(&mut content, (width - 1, y), VERT_BOUNDARY); + set_and_join_box(&mut content, (width - 1, y), BoxBoundary::Vertical); } } self.content = content; @@ -682,7 +690,7 @@ impl ThreadView { return; } for x in get_x(upper_left)..=get_x(bottom_right) { - set_and_join_box(grid, (x, y - 1), HORZ_BOUNDARY); + set_and_join_box(grid, (x, y - 1), BoxBoundary::Horizontal); grid[(x, y - 1)].set_fg(Color::Byte(33)); grid[(x, y - 1)].set_bg(Color::Default); } @@ -768,7 +776,7 @@ impl ThreadView { }; for x in get_x(upper_left)..=get_x(bottom_right) { - set_and_join_box(grid, (x, y - 1), HORZ_BOUNDARY); + set_and_join_box(grid, (x, y - 1), BoxBoundary::Horizontal); grid[(x, y - 1)].set_fg(Color::Default); grid[(x, y - 1)].set_bg(Color::Default); } @@ -832,7 +840,7 @@ impl ThreadView { let area = (set_y(upper_left, mid), set_y(bottom_right, mid)); context.dirty_areas.push_back(area); for x in get_x(upper_left)..=get_x(bottom_right) { - set_and_join_box(grid, (x, mid), HORZ_BOUNDARY); + set_and_join_box(grid, (x, mid), BoxBoundary::Horizontal); grid[(x, mid)].set_fg(Color::Default); grid[(x, mid)].set_bg(Color::Default); }