From f4f2759df2e4578c7acfab1331251ae1b1a9e0a9 Mon Sep 17 00:00:00 2001 From: Alexandre Bury Date: Wed, 28 Sep 2016 15:06:16 -0700 Subject: [PATCH] Make SelectView::on_submit callback take a Borrow Instead of a `T` directly. --- src/views/select_view.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/views/select_view.rs b/src/views/select_view.rs index d3a24ee..024faaf 100644 --- a/src/views/select_view.rs +++ b/src/views/select_view.rs @@ -1,6 +1,7 @@ use std::cmp::min; use std::rc::Rc; use std::cell::Cell; +use std::borrow::Borrow; use Cursive; use With; @@ -132,7 +133,6 @@ impl SelectView { /// Sets a callback to be used when an item is selected. /// - /// /// Chainable variant. pub fn on_select(self, cb: F) -> Self where F: Fn(&mut Cursive, &T) + 'static @@ -143,10 +143,13 @@ impl SelectView { /// Sets a callback to be used when `` is pressed. /// /// The item currently selected will be given to the callback. - pub fn set_on_submit(&mut self, cb: F) - where F: Fn(&mut Cursive, &T) + 'static + /// + /// Here, `V` can be `T` itself, or a type that can be borrowed from `T`. + pub fn set_on_submit(&mut self, cb: F) + where F: Fn(&mut Cursive, &V) + 'static, + T: Borrow { - self.on_submit = Some(Rc::new(cb)); + self.on_submit = Some(Rc::new(move |s, t| cb(s, t.borrow()))); } /// Sets a callback to be used when `` is pressed. @@ -154,8 +157,9 @@ impl SelectView { /// The item currently selected will be given to the callback. /// /// Chainable variant. - pub fn on_submit(self, cb: F) -> Self - where F: Fn(&mut Cursive, &T) + 'static + pub fn on_submit(self, cb: F) -> Self + where F: Fn(&mut Cursive, &V) + 'static, + T: Borrow { self.with(|s| s.set_on_submit(cb)) }