replace CbFunc with Box<FnOnce()> (#351)

Since Rust 1.35, Box<FnOnce()> compiles, we can remove the workaround.
This commit is contained in:
Gergely Nagy 2019-07-05 09:16:26 +02:00 committed by Alexandre Bury
parent babc038e36
commit 74266edf29
3 changed files with 7 additions and 25 deletions

View File

@ -137,7 +137,7 @@ cargo run --example vpv </dev/zero >/dev/null",
}
// When we're done, shut down the application
cb_sink.send(Box::new(|s: &mut Cursive| s.quit())).unwrap();
cb_sink.send(Box::new(Cursive::quit)).unwrap();
});
siv.set_autorefresh(true);
}

View File

@ -43,8 +43,8 @@ pub struct Cursive {
backend: Box<dyn backend::Backend>,
cb_source: Receiver<Box<dyn CbFunc>>,
cb_sink: Sender<Box<dyn CbFunc>>,
cb_source: Receiver<Box<dyn FnOnce(&mut Cursive) + Send>>,
cb_sink: Sender<Box<dyn FnOnce(&mut Cursive) + Send>>,
// User-provided data.
user_data: Box<dyn Any>,
@ -57,25 +57,7 @@ pub struct Cursive {
pub type ScreenId = usize;
/// Convenient alias to the result of `Cursive::cb_sink`.
pub type CbSink = Sender<Box<dyn CbFunc>>;
/// Asynchronous callback function trait.
///
/// Every `FnOnce(&mut Cursive) -> () + Send` automatically
/// implements this.
///
/// This is a workaround only because `Box<FnOnce()>` is not
/// working and `FnBox` is unstable.
pub trait CbFunc: Send {
/// Calls the function.
fn call_box(self: Box<Self>, _: &mut Cursive);
}
impl<F: FnOnce(&mut Cursive) -> () + Send> CbFunc for F {
fn call_box(self: Box<Self>, siv: &mut Cursive) {
(*self)(siv)
}
}
pub type CbSink = Sender<Box<dyn FnOnce(&mut Cursive) + Send>>;
cfg_if::cfg_if! {
if #[cfg(feature = "blt-backend")] {
@ -330,7 +312,7 @@ impl Cursive {
/// let mut siv = Cursive::dummy();
///
/// // quit() will be called during the next event cycle
/// siv.cb_sink().send(Box::new(|s: &mut Cursive| s.quit())).unwrap();
/// siv.cb_sink().send(Box::new(|s| s.quit())).unwrap();
/// # }
/// ```
pub fn cb_sink(&self) -> &CbSink {
@ -877,7 +859,7 @@ impl Cursive {
// Then, handle any available callback
while let Ok(cb) = self.cb_source.try_recv() {
boring = false;
cb.call_box(self);
cb(self);
if !self.running {
return true;

View File

@ -101,7 +101,7 @@ mod utf8;
pub mod backend;
pub use self::cursive::{CbFunc, CbSink, Cursive, ScreenId};
pub use self::cursive::{CbSink, Cursive, ScreenId};
pub use self::printer::Printer;
pub use self::rect::Rect;
pub use self::vec::Vec2;