From ea377db4ee81cf9fc2a4dae1906e5dbf85aa1bcd Mon Sep 17 00:00:00 2001 From: rhysd Date: Sat, 9 Jul 2022 12:38:05 +0900 Subject: [PATCH] prefer `Into::into` to `From::from` --- README.md | 10 +++++----- examples/editor.rs | 4 ++-- examples/minimal.rs | 2 +- examples/single_line.rs | 2 +- examples/split.rs | 2 +- examples/variable.rs | 2 +- src/input.rs | 7 ++++++- 7 files changed, 17 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 3aea05b..642ad64 100644 --- a/README.md +++ b/README.md @@ -366,7 +366,7 @@ loop { // Using `Input` is not mandatory, but it's useful for pattern match // Ignore Ctrl+m and Enter. Otherwise handle keys as usual - match Input::from(read()?) { + match read()?.into() { Input { key: Key::Char('m'), ctrl: true, alt: false } | Input { key: Key::Enter, .. } => continue, input => { @@ -436,7 +436,7 @@ loop { // ... match mode { - Mode::Normal => match Input::from(read()?) { + Mode::Normal => match read()?.into() { Input { key: Key::Char('h'), .. } => textarea.move_cursor(CursorMove::Back), Input { key: Key::Char('j'), .. } => textarea.move_cursor(CursorMove::Down), Input { key: Key::Char('k'), .. } => textarea.move_cursor(CursorMove::Up), @@ -460,7 +460,7 @@ loop { } _ => {}, }, - Mode::Insert => match Input::from(read()?) { + Mode::Insert => match read()?.into() { Input { key: Key::Esc, .. } => { mode = Mode::Normal; } @@ -477,7 +477,7 @@ If you don't want to use default key mappings, `TextArea::input_without_shortcut newlines. ```rust -match Input::from(read()?) { +match read()?.into() { // Handle your own key mappings here // ... input => textarea.input_without_shortcuts(input), @@ -583,7 +583,7 @@ loop { } })?; - match Input::from(read()?) { + match read()?.into() { // Switch focused textarea by Ctrl+S Input { key: Key::Char('s'), ctrl: true, .. } => focused = (focused + 1) % 2; // Handle input by the focused editor diff --git a/examples/editor.rs b/examples/editor.rs index 7c7dac4..67a80e6 100644 --- a/examples/editor.rs +++ b/examples/editor.rs @@ -260,7 +260,7 @@ impl<'a> Editor<'a> { if search_height > 0 { let textarea = &mut self.buffers[self.current].textarea; - match Input::from(crossterm::event::read()?) { + match crossterm::event::read()?.into() { Input { key: Key::Char('g' | 'n'), ctrl: true, @@ -307,7 +307,7 @@ impl<'a> Editor<'a> { } } } else { - match Input::from(crossterm::event::read()?) { + match crossterm::event::read()?.into() { Input { key: Key::Char('q'), ctrl: true, diff --git a/examples/minimal.rs b/examples/minimal.rs index 7146548..5d26a20 100644 --- a/examples/minimal.rs +++ b/examples/minimal.rs @@ -28,7 +28,7 @@ fn main() -> io::Result<()> { term.draw(|f| { f.render_widget(textarea.widget(), f.size()); })?; - match Input::from(crossterm::event::read()?) { + match crossterm::event::read()?.into() { Input { key: Key::Esc, .. } => break, input => { textarea.input(input); diff --git a/examples/single_line.rs b/examples/single_line.rs index ed43950..4e7a374 100644 --- a/examples/single_line.rs +++ b/examples/single_line.rs @@ -48,7 +48,7 @@ fn main() -> io::Result<()> { f.render_widget(widget, chunks[0]); })?; - match Input::from(crossterm::event::read()?) { + match crossterm::event::read()?.into() { Input { key: Key::Esc, .. } => break, Input { key: Key::Enter, .. diff --git a/examples/split.rs b/examples/split.rs index 0400b13..e470649 100644 --- a/examples/split.rs +++ b/examples/split.rs @@ -59,7 +59,7 @@ fn main() -> io::Result<()> { f.render_widget(widget, chunk); } })?; - match Input::from(crossterm::event::read()?) { + match crossterm::event::read()?.into() { Input { key: Key::Esc, .. } => break, Input { key: Key::Char('x'), diff --git a/examples/variable.rs b/examples/variable.rs index 5d20602..91637c9 100644 --- a/examples/variable.rs +++ b/examples/variable.rs @@ -36,7 +36,7 @@ fn main() -> io::Result<()> { .split(f.size()); f.render_widget(textarea.widget(), chunks[0]); })?; - match Input::from(crossterm::event::read()?) { + match crossterm::event::read()?.into() { Input { key: Key::Esc, .. } => break, input => { textarea.input(input); diff --git a/src/input.rs b/src/input.rs index 5131031..3fc3875 100644 --- a/src/input.rs +++ b/src/input.rs @@ -36,9 +36,14 @@ pub enum Key { /// use crossterm::event::{Event, read}; /// /// let event = read().unwrap(); +/// +/// // `Input::from` can convert backend-native event into `Input` /// let input = Input::from(event); +/// // or `Into::into` +/// let input: Input = event.into(); +/// // Conversion from `KeyEvent` value is also available /// if let Event::Key(key) = event { -/// let input = Input::from(key); // Conversion from `KeyEvent` value is also available +/// let input = Input::from(key); /// } /// ``` ///