From 769ab8bf9bdd04080486dd1323cfbdeccb4a91c5 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Fri, 6 Sep 2019 16:02:20 -0700 Subject: [PATCH] Replace event::AnyCb with non-boxed &dyn FnMut --- CHANGELOG.md | 12 ++++++++++++ src/event.rs | 10 ++++++++-- src/view/finder.rs | 4 ++-- src/view/view_wrapper.rs | 3 +-- src/views/box_view.rs | 1 - src/views/hideable_view.rs | 5 ++--- src/views/id_view.rs | 7 ++----- src/views/linear_layout.rs | 6 ++---- src/views/list_view.rs | 4 ++-- src/views/stack_view.rs | 10 +++------- 10 files changed, 34 insertions(+), 28 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 251cf0d..8024ece 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## 0.14.0 (Next version) + +### Breaking changes + +- `cursive::event::AnyCb` changed from `Box<...>` to `&mut ...`, so users of + `View::call_on_any` no longer need to box their closures. +- Remove `BoxView::squishable` + +### Bugfixes + +- Fixed layout for `BoxView` with some size constraints. + ## 0.13.0 ### Breaking changes diff --git a/src/event.rs b/src/event.rs index 239934d..c021a8b 100644 --- a/src/event.rs +++ b/src/event.rs @@ -22,14 +22,20 @@ use std::rc::Rc; /// Callback is a function that can be triggered by an event. /// It has a mutable access to the cursive root. +/// +/// It is meant to be stored in views. #[derive(Clone)] pub struct Callback(Rc>); // TODO: remove the Box when Box -> Rc is possible -/// A boxed callback that can be run on `&mut Any`. -pub type AnyCb<'a> = Box; +/// A callback that can be run on `&mut Any`. +/// +/// It is meant to be used as parameter in `View::call_on_any`, and not much else. +pub type AnyCb<'a> = &'a mut dyn FnMut(&mut dyn Any); /// A trigger that only selects some types of events. +/// +/// It is meant to be stored in views. pub struct EventTrigger(Box bool>); impl EventTrigger { diff --git a/src/view/finder.rs b/src/view/finder.rs index ad7f570..8136d2b 100644 --- a/src/view/finder.rs +++ b/src/view/finder.rs @@ -60,7 +60,7 @@ impl Finder for T { let result_ref = &mut result; let mut callback = Some(callback); - let callback = |v: &mut dyn Any| { + let mut callback = |v: &mut dyn Any| { if let Some(callback) = callback.take() { if v.is::() { *result_ref = @@ -72,7 +72,7 @@ impl Finder for T { } } }; - self.call_on_any(sel, Box::new(callback)); + self.call_on_any(sel, &mut callback); } result } diff --git a/src/view/view_wrapper.rs b/src/view/view_wrapper.rs index ad49278..7e0ed42 100644 --- a/src/view/view_wrapper.rs +++ b/src/view/view_wrapper.rs @@ -4,7 +4,6 @@ use crate::rect::Rect; use crate::vec::Vec2; use crate::view::{Selector, View}; use crate::Printer; -use std::any::Any; /// Generic wrapper around a view. /// @@ -129,7 +128,7 @@ impl View for T { fn call_on_any<'a>( &mut self, selector: &Selector<'_>, - callback: Box, + callback: AnyCb<'a>, ) { self.wrap_call_on_any(selector, callback) } diff --git a/src/views/box_view.rs b/src/views/box_view.rs index 9f5f864..4ff76bd 100644 --- a/src/views/box_view.rs +++ b/src/views/box_view.rs @@ -1,7 +1,6 @@ use crate::printer::Printer; use crate::vec::Vec2; use crate::view::{SizeConstraint, View, ViewWrapper}; -use crate::With; use crate::XY; /// Wrapper around another view, with a controlled size. diff --git a/src/views/hideable_view.rs b/src/views/hideable_view.rs index bba943f..fa3c671 100644 --- a/src/views/hideable_view.rs +++ b/src/views/hideable_view.rs @@ -1,9 +1,8 @@ +use crate::event::AnyCb; use crate::vec::Vec2; use crate::view::{Selector, View, ViewWrapper}; use crate::With; -use std::any::Any; - /// Wrapper around another view that can be hidden at will. /// /// By default, it simply forwards all calls to the inner view. @@ -93,7 +92,7 @@ impl ViewWrapper for HideableView { fn wrap_call_on_any<'a>( &mut self, selector: &Selector<'_>, - callback: Box, + callback: AnyCb<'a>, ) { // We always run callbacks, even when invisible. self.view.call_on_any(selector, callback) diff --git a/src/views/id_view.rs b/src/views/id_view.rs index 0548dd1..43e2c52 100644 --- a/src/views/id_view.rs +++ b/src/views/id_view.rs @@ -1,6 +1,6 @@ +use crate::event::AnyCb; use crate::view::{Selector, View, ViewWrapper}; use owning_ref::{OwningHandle, RcRef}; -use std::any::Any; use std::cell::{RefCell, RefMut}; use std::ops::DerefMut; use std::rc::Rc; @@ -41,9 +41,6 @@ impl IdView { } } -// Shortcut for a boxed callback (for the wrap_call_on_any method). -type BoxedCallback<'a> = Box FnMut(&'b mut dyn Any) + 'a>; - impl ViewWrapper for IdView { type V = T; @@ -79,7 +76,7 @@ impl ViewWrapper for IdView { fn wrap_call_on_any<'a>( &mut self, selector: &Selector<'_>, - mut callback: BoxedCallback<'a>, + callback: AnyCb<'a>, ) { match selector { &Selector::Id(id) if id == self.id => callback(self), diff --git a/src/views/linear_layout.rs b/src/views/linear_layout.rs index 619fb61..afcb44b 100644 --- a/src/views/linear_layout.rs +++ b/src/views/linear_layout.rs @@ -639,12 +639,10 @@ impl View for LinearLayout { fn call_on_any<'a>( &mut self, selector: &Selector<'_>, - mut callback: AnyCb<'a>, + callback: AnyCb<'a>, ) { for child in &mut self.children { - child - .view - .call_on_any(selector, Box::new(|any| callback(any))); + child.view.call_on_any(selector, callback); } } diff --git a/src/views/list_view.rs b/src/views/list_view.rs index 676efde..465e4f7 100644 --- a/src/views/list_view.rs +++ b/src/views/list_view.rs @@ -396,10 +396,10 @@ impl View for ListView { fn call_on_any<'a>( &mut self, selector: &Selector<'_>, - mut callback: AnyCb<'a>, + callback: AnyCb<'a>, ) { for view in self.children.iter_mut().filter_map(ListChild::view) { - view.call_on_any(selector, Box::new(|any| callback(any))); + view.call_on_any(selector, callback); } } diff --git a/src/views/stack_view.rs b/src/views/stack_view.rs index 1770107..c1cf9e3 100644 --- a/src/views/stack_view.rs +++ b/src/views/stack_view.rs @@ -324,9 +324,7 @@ impl StackView { for (i, child) in self.layers.iter_mut().enumerate() { let mut found = false; - child - .view - .call_on_any(&selector, Box::new(|_| found = true)); + child.view.call_on_any(&selector, &mut |_| found = true); if found { return Some(LayerPosition::FromBack(i)); } @@ -668,12 +666,10 @@ impl View for StackView { fn call_on_any<'a>( &mut self, selector: &Selector<'_>, - mut callback: AnyCb<'a>, + callback: AnyCb<'a>, ) { for layer in &mut self.layers { - layer - .view - .call_on_any(selector, Box::new(|any| callback(any))); + layer.view.call_on_any(selector, callback); } }