From cf31797e86d3acd7b2f82380c56a5145743d8f95 Mon Sep 17 00:00:00 2001 From: EdJoPaTo Date: Thu, 1 Apr 2021 09:05:05 +0200 Subject: [PATCH] refactor: impl method returns Self --- examples/example.rs | 4 ++-- examples/util/event.rs | 12 ++++++------ examples/util/mod.rs | 8 ++++---- src/lib.rs | 28 ++++++++++++++-------------- 4 files changed, 26 insertions(+), 26 deletions(-) diff --git a/examples/example.rs b/examples/example.rs index e2927fb..891fffe 100644 --- a/examples/example.rs +++ b/examples/example.rs @@ -20,8 +20,8 @@ struct App<'a> { } impl<'a> App<'a> { - fn new() -> App<'a> { - App { + fn new() -> Self { + Self { tree: StatefulTree::with_items(vec![ TreeItem::new_leaf("a"), TreeItem::new( diff --git a/examples/util/event.rs b/examples/util/event.rs index 639abf2..08ed793 100644 --- a/examples/util/event.rs +++ b/examples/util/event.rs @@ -25,8 +25,8 @@ pub struct Config { } impl Default for Config { - fn default() -> Config { - Config { + fn default() -> Self { + Self { exit_key: Key::Char('q'), tick_rate: Duration::from_millis(250), } @@ -34,11 +34,11 @@ impl Default for Config { } impl Events { - pub fn new() -> Events { - Events::with_config(Config::default()) + pub fn new() -> Self { + Self::with_config(Config::default()) } - pub fn with_config(config: Config) -> Events { + pub fn with_config(config: Config) -> Self { let (tx, rx) = mpsc::channel(); { let tx = tx.clone(); @@ -63,7 +63,7 @@ impl Events { } thread::sleep(config.tick_rate); }); - Events { rx } + Self { rx } } pub fn next(&self) -> Result, mpsc::RecvError> { diff --git a/examples/util/mod.rs b/examples/util/mod.rs index 473d6ec..6d908e4 100644 --- a/examples/util/mod.rs +++ b/examples/util/mod.rs @@ -9,15 +9,15 @@ pub struct StatefulTree<'a> { impl<'a> StatefulTree<'a> { #[allow(dead_code)] - pub fn new() -> StatefulTree<'a> { - StatefulTree { + pub fn new() -> Self { + Self { state: TreeState::default(), items: Vec::new(), } } - pub fn with_items(items: Vec>) -> StatefulTree<'a> { - StatefulTree { + pub fn with_items(items: Vec>) -> Self { + Self { state: TreeState::default(), items, } diff --git a/src/lib.rs b/src/lib.rs index 6d98cb3..c8e86d5 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,8 +23,8 @@ pub struct TreeState { } impl Default for TreeState { - fn default() -> TreeState { - TreeState { + fn default() -> Self { + Self { offset: 0, selected: Vec::new(), opened: HashSet::new(), @@ -82,23 +82,23 @@ pub struct TreeItem<'a> { } impl<'a> TreeItem<'a> { - pub fn new_leaf(text: T) -> TreeItem<'a> + pub fn new_leaf(text: T) -> Self where T: Into>, { - TreeItem { + Self { text: text.into(), style: Style::default(), children: Vec::new(), } } - pub fn new(text: T, children: Children) -> TreeItem<'a> + pub fn new(text: T, children: Children) -> Self where T: Into>, Children: Into>>, { - TreeItem { + Self { text: text.into(), style: Style::default(), children: children.into(), @@ -109,7 +109,7 @@ impl<'a> TreeItem<'a> { self.text.height() } - pub fn style(mut self, style: Style) -> TreeItem<'a> { + pub fn style(mut self, style: Style) -> Self { self.style = style; self } @@ -133,11 +133,11 @@ pub struct Tree<'a> { } impl<'a> Tree<'a> { - pub fn new(items: T) -> Tree<'a> + pub fn new(items: T) -> Self where T: Into>>, { - Tree { + Self { block: None, style: Style::default(), items: items.into(), @@ -147,27 +147,27 @@ impl<'a> Tree<'a> { } } - pub fn block(mut self, block: Block<'a>) -> Tree<'a> { + pub fn block(mut self, block: Block<'a>) -> Self { self.block = Some(block); self } - pub fn style(mut self, style: Style) -> Tree<'a> { + pub fn style(mut self, style: Style) -> Self { self.style = style; self } - pub fn highlight_symbol(mut self, highlight_symbol: &'a str) -> Tree<'a> { + pub fn highlight_symbol(mut self, highlight_symbol: &'a str) -> Self { self.highlight_symbol = Some(highlight_symbol); self } - pub fn highlight_style(mut self, style: Style) -> Tree<'a> { + pub fn highlight_style(mut self, style: Style) -> Self { self.highlight_style = style; self } - pub fn start_corner(mut self, corner: Corner) -> Tree<'a> { + pub fn start_corner(mut self, corner: Corner) -> Self { self.start_corner = corner; self }