Make SelectView::on_submit callback take a Borrow<T>

Instead of a `T` directly.
This commit is contained in:
Alexandre Bury 2016-09-28 15:06:16 -07:00
parent aae9368961
commit f4f2759df2

View File

@ -1,6 +1,7 @@
use std::cmp::min; use std::cmp::min;
use std::rc::Rc; use std::rc::Rc;
use std::cell::Cell; use std::cell::Cell;
use std::borrow::Borrow;
use Cursive; use Cursive;
use With; use With;
@ -132,7 +133,6 @@ impl<T: 'static> SelectView<T> {
/// Sets a callback to be used when an item is selected. /// Sets a callback to be used when an item is selected.
/// ///
///
/// Chainable variant. /// Chainable variant.
pub fn on_select<F>(self, cb: F) -> Self pub fn on_select<F>(self, cb: F) -> Self
where F: Fn(&mut Cursive, &T) + 'static where F: Fn(&mut Cursive, &T) + 'static
@ -143,10 +143,13 @@ impl<T: 'static> SelectView<T> {
/// Sets a callback to be used when `<Enter>` is pressed. /// Sets a callback to be used when `<Enter>` is pressed.
/// ///
/// The item currently selected will be given to the callback. /// The item currently selected will be given to the callback.
pub fn set_on_submit<F>(&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<F, V: ?Sized>(&mut self, cb: F)
where F: Fn(&mut Cursive, &V) + 'static,
T: Borrow<V>
{ {
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 `<Enter>` is pressed. /// Sets a callback to be used when `<Enter>` is pressed.
@ -154,8 +157,9 @@ impl<T: 'static> SelectView<T> {
/// The item currently selected will be given to the callback. /// The item currently selected will be given to the callback.
/// ///
/// Chainable variant. /// Chainable variant.
pub fn on_submit<F>(self, cb: F) -> Self pub fn on_submit<F, V: ?Sized>(self, cb: F) -> Self
where F: Fn(&mut Cursive, &T) + 'static where F: Fn(&mut Cursive, &V) + 'static,
T: Borrow<V>
{ {
self.with(|s| s.set_on_submit(cb)) self.with(|s| s.set_on_submit(cb))
} }