From 9bc723ab4ad921de8634db2a08f605cc15f3cbc2 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 16 Mar 2018 15:50:56 -0700 Subject: [PATCH] Do not accept any return value for callbacks It got added so we could keep using `s.pop_layer()` in single-line callbacks. It was confusing, and the return value was ignored anyway. --- examples/mines/main.rs | 2 +- examples/text_area.rs | 6 ++++-- src/event.rs | 8 ++++---- src/view/any.rs | 1 - src/view/mod.rs | 2 +- src/view/view.rs | 2 -- src/views/button.rs | 8 ++++---- src/views/dialog.rs | 12 +++++++----- src/views/menu_popup.rs | 4 +++- src/views/on_event_view.rs | 16 ++++++++-------- 10 files changed, 32 insertions(+), 29 deletions(-) diff --git a/examples/mines/main.rs b/examples/mines/main.rs index 950f387..4d2ba70 100644 --- a/examples/mines/main.rs +++ b/examples/mines/main.rs @@ -63,7 +63,7 @@ fn show_options(siv: &mut Cursive) { new_game(s, *option); }), ) - .button("Back", |s| s.pop_layer()), + .dismiss_button("Back"), ); } diff --git a/examples/text_area.rs b/examples/text_area.rs index ce21f0c..c351121 100644 --- a/examples/text_area.rs +++ b/examples/text_area.rs @@ -41,8 +41,10 @@ fn main() { ).unwrap(); find(s, &text); }) - .button("Cancel", |s| s.pop_layer()), - ).on_event(Event::Key(Key::Esc), |s| s.pop_layer()), + .dismiss_button("Cancel"), + ).on_event(Event::Key(Key::Esc), |s| { + s.pop_layer(); + }), ) }); diff --git a/src/event.rs b/src/event.rs index 4d8c5c0..353ad88 100644 --- a/src/event.rs +++ b/src/event.rs @@ -26,9 +26,9 @@ pub struct Callback(Rc>); impl Callback { /// Wraps the given function into a `Callback` object. - pub fn from_fn(f: F) -> Self + pub fn from_fn(f: F) -> Self where - F: 'static + Fn(&mut Cursive) -> R, + F: 'static + Fn(&mut Cursive), { Callback(Rc::new(Box::new(move |siv| { f(siv); @@ -77,9 +77,9 @@ pub enum EventResult { impl EventResult { /// Convenient method to create `Consumed(Some(f))` - pub fn with_cb(f: F) -> Self + pub fn with_cb(f: F) -> Self where - F: 'static + Fn(&mut Cursive) -> R, + F: 'static + Fn(&mut Cursive), { EventResult::Consumed(Some(Callback::from_fn(f))) } diff --git a/src/view/any.rs b/src/view/any.rs index a6be913..8252088 100644 --- a/src/view/any.rs +++ b/src/view/any.rs @@ -43,4 +43,3 @@ impl AnyView for T { self } } - diff --git a/src/view/mod.rs b/src/view/mod.rs index 75d167e..16f8bf1 100644 --- a/src/view/mod.rs +++ b/src/view/mod.rs @@ -63,6 +63,6 @@ pub use self::position::{Offset, Position}; pub use self::scroll::{ScrollBase, ScrollStrategy}; pub use self::size_cache::SizeCache; pub use self::size_constraint::SizeConstraint; +pub use self::view::View; pub use self::view_path::ViewPath; pub use self::view_wrapper::ViewWrapper; -pub use self::view::View; diff --git a/src/view/view.rs b/src/view/view.rs index c72e825..8d03c06 100644 --- a/src/view/view.rs +++ b/src/view/view.rs @@ -5,7 +5,6 @@ use std::any::Any; use vec::Vec2; use view::{AnyView, Selector}; - /// Main trait defining a view behaviour. /// /// This is what you should implement to define a custom View. @@ -86,4 +85,3 @@ pub trait View: Any + AnyView { false } } - diff --git a/src/views/button.rs b/src/views/button.rs index a0ec0fc..4e5afaf 100644 --- a/src/views/button.rs +++ b/src/views/button.rs @@ -25,9 +25,9 @@ pub struct Button { impl Button { /// Creates a new button with the given content and callback. - pub fn new(label: S, cb: F) -> Self + pub fn new(label: S, cb: F) -> Self where - F: 'static + Fn(&mut Cursive) -> R, + F: 'static + Fn(&mut Cursive), S: Into, { let label = label.into(); @@ -35,9 +35,9 @@ impl Button { } /// Creates a new button without angle brackets. - pub fn new_raw>(label: S, cb: F) -> Self + pub fn new_raw>(label: S, cb: F) -> Self where - F: 'static + Fn(&mut Cursive) -> R, + F: 'static + Fn(&mut Cursive), { Button { label: label.into(), diff --git a/src/views/dialog.rs b/src/views/dialog.rs index 529004e..076b3c6 100644 --- a/src/views/dialog.rs +++ b/src/views/dialog.rs @@ -30,9 +30,9 @@ struct ChildButton { } impl ChildButton { - pub fn new>(label: S, cb: F) -> Self + pub fn new>(label: S, cb: F) -> Self where - F: 'static + Fn(&mut Cursive) -> R, + F: 'static + Fn(&mut Cursive), { ChildButton { button: SizedView::new(Button::new(label, cb)), @@ -151,9 +151,9 @@ impl Dialog { /// Adds a button to the dialog with the given label and callback. /// /// Consumes and returns self for easy chaining. - pub fn button>(mut self, label: S, cb: F) -> Self + pub fn button>(mut self, label: S, cb: F) -> Self where - F: 'static + Fn(&mut Cursive) -> R, + F: 'static + Fn(&mut Cursive), { self.buttons.push(ChildButton::new(label, cb)); @@ -184,7 +184,9 @@ impl Dialog { /// Shortcut method to add a button that will dismiss the dialog. pub fn dismiss_button>(self, label: S) -> Self { - self.button(label, |s| s.pop_layer()) + self.button(label, |s| { + s.pop_layer(); + }) } /// Sets the title of the dialog. diff --git a/src/views/menu_popup.rs b/src/views/menu_popup.rs index 365ec13..f583a3e 100644 --- a/src/views/menu_popup.rs +++ b/src/views/menu_popup.rs @@ -161,7 +161,9 @@ impl MenuPopup { action_cb.clone()(s); } }, - )).on_event(Key::Left, |s| s.pop_layer()), + )).on_event(Key::Left, |s| { + s.pop_layer(); + }), ); }) } diff --git a/src/views/on_event_view.rs b/src/views/on_event_view.rs index 9d45166..e7a7e03 100644 --- a/src/views/on_event_view.rs +++ b/src/views/on_event_view.rs @@ -80,10 +80,10 @@ impl OnEventView { /// Registers a callback when the given event is ignored by the child. /// /// Chainable variant. - pub fn on_event(self, event: E, cb: F) -> Self + pub fn on_event(self, event: E, cb: F) -> Self where E: Into, - F: 'static + Fn(&mut Cursive) -> R, + F: 'static + Fn(&mut Cursive), { self.with(|s| s.set_on_event(event, cb)) } @@ -93,10 +93,10 @@ impl OnEventView { /// The child will never receive this event. /// /// Chainable variant. - pub fn on_pre_event(self, event: E, cb: F) -> Self + pub fn on_pre_event(self, event: E, cb: F) -> Self where E: Into, - F: 'static + Fn(&mut Cursive) -> R, + F: 'static + Fn(&mut Cursive), { self.with(|s| s.set_on_pre_event(event, cb)) } @@ -135,10 +135,10 @@ impl OnEventView { } /// Registers a callback when the given event is ignored by the child. - pub fn set_on_event(&mut self, event: E, cb: F) + pub fn set_on_event(&mut self, event: E, cb: F) where E: Into, - F: Fn(&mut Cursive) -> R + 'static, + F: Fn(&mut Cursive) + 'static, { let cb = Callback::from_fn(cb); let action = @@ -150,10 +150,10 @@ impl OnEventView { /// Registers a callback when the given event is received. /// /// The child will never receive this event. - pub fn set_on_pre_event(&mut self, event: E, cb: F) + pub fn set_on_pre_event(&mut self, event: E, cb: F) where E: Into, - F: 'static + Fn(&mut Cursive) -> R, + F: 'static + Fn(&mut Cursive), { let cb = Callback::from_fn(cb); // We want to clone the Callback every time we call the closure