From d676f665046d9ccb4dafe17e75b7acc3bdfd06d3 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Thu, 23 May 2024 12:03:02 +0200 Subject: [PATCH] fix: must_use the structs --- benches/bench.rs | 7 ++++--- examples/example.rs | 1 + src/flatten.rs | 1 + src/lib.rs | 12 +++--------- src/tree_item.rs | 1 + src/tree_state.rs | 1 + 6 files changed, 11 insertions(+), 12 deletions(-) diff --git a/benches/bench.rs b/benches/bench.rs index 7b04039..b477777 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -6,6 +6,7 @@ use ratatui::layout::Rect; use ratatui::widgets::StatefulWidget; use tui_tree_widget::{Tree, TreeItem, TreeState}; +#[must_use] fn example_items() -> Vec> { vec![ TreeItem::new_leaf("a", "Alfa"), @@ -73,15 +74,15 @@ fn init(criterion: &mut Criterion) { group.bench_function("empty", |bencher| { bencher.iter(|| { - let items: Vec> = vec![]; - black_box(Tree::new(black_box(&items))).unwrap(); + let items = vec![]; + let _: Tree = black_box(Tree::new(black_box(&items))).unwrap(); }); }); group.bench_function("example-items", |bencher| { bencher.iter(|| { let items = example_items(); - black_box(Tree::new(black_box(&items))).unwrap(); + let _: Tree<_> = black_box(Tree::new(black_box(&items))).unwrap(); }); }); diff --git a/examples/example.rs b/examples/example.rs index 050cf9d..0239be3 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -9,6 +9,7 @@ use ratatui::widgets::{Block, Scrollbar, ScrollbarOrientation}; use ratatui::{Frame, Terminal}; use tui_tree_widget::{Tree, TreeItem, TreeState}; +#[must_use] struct App { state: TreeState<&'static str>, items: Vec>, diff --git a/src/flatten.rs b/src/flatten.rs index 8f4a7b9..fffb167 100644 --- a/src/flatten.rs +++ b/src/flatten.rs @@ -5,6 +5,7 @@ use crate::tree_item::TreeItem; /// A flattened item of all visible [`TreeItem`]s. /// /// Generated via [`TreeState::flatten`](crate::TreeState::flatten). +#[must_use] pub struct Flattened<'text, Identifier> { pub identifier: Vec, pub item: &'text TreeItem<'text, Identifier>, diff --git a/src/lib.rs b/src/lib.rs index 025ec16..931fa17 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,6 +50,7 @@ mod tree_state; /// })?; /// # Ok::<(), std::io::Error>(()) /// ``` +#[must_use] #[derive(Debug, Clone)] pub struct Tree<'a, Identifier> { items: &'a [TreeItem<'a, Identifier>], @@ -107,7 +108,6 @@ where } #[allow(clippy::missing_const_for_fn)] - #[must_use] pub fn block(mut self, block: Block<'a>) -> Self { self.block = Some(block); self @@ -118,43 +118,36 @@ where /// Experimental: Can change on any release without any additional notice. /// Its there to test and experiment with whats possible with scrolling widgets. /// Also see - #[must_use] pub const fn experimental_scrollbar(mut self, scrollbar: Option>) -> Self { self.scrollbar = scrollbar; self } - #[must_use] pub const fn style(mut self, style: Style) -> Self { self.style = style; self } - #[must_use] pub const fn highlight_style(mut self, style: Style) -> Self { self.highlight_style = style; self } - #[must_use] pub const fn highlight_symbol(mut self, highlight_symbol: &'a str) -> Self { self.highlight_symbol = highlight_symbol; self } - #[must_use] pub const fn node_closed_symbol(mut self, symbol: &'a str) -> Self { self.node_closed_symbol = symbol; self } - #[must_use] pub const fn node_open_symbol(mut self, symbol: &'a str) -> Self { self.node_open_symbol = symbol; self } - #[must_use] pub const fn node_no_children_symbol(mut self, symbol: &'a str) -> Self { self.node_no_children_symbol = symbol; self @@ -166,7 +159,8 @@ where fn tree_new_errors_with_duplicate_identifiers() { let item = TreeItem::new_leaf("same", "text"); let another = item.clone(); - Tree::new(&[item, another]).unwrap(); + let items = [item, another]; + let _: Tree<_> = Tree::new(&items).unwrap(); } impl StatefulWidget for Tree<'_, Identifier> diff --git a/src/tree_item.rs b/src/tree_item.rs index ee64f1c..37d706d 100644 --- a/src/tree_item.rs +++ b/src/tree_item.rs @@ -139,6 +139,7 @@ where impl TreeItem<'static, &'static str> { #[cfg(test)] + #[must_use] pub(crate) fn example() -> Vec { vec![ Self::new_leaf("a", "Alfa"), diff --git a/src/tree_state.rs b/src/tree_state.rs index 538c61b..e314641 100644 --- a/src/tree_state.rs +++ b/src/tree_state.rs @@ -18,6 +18,7 @@ use crate::tree_item::TreeItem; /// /// let mut state = TreeState::::default(); /// ``` +#[must_use] #[derive(Debug, Default)] pub struct TreeState { pub(super) offset: usize,