From 74266edf29c99780b33b5501fd1aae9d4251231c Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Fri, 5 Jul 2019 09:16:26 +0200 Subject: [PATCH] replace CbFunc with Box (#351) Since Rust 1.35, Box compiles, we can remove the workaround. --- examples/vpv.rs | 2 +- src/cursive.rs | 28 +++++----------------------- src/lib.rs | 2 +- 3 files changed, 7 insertions(+), 25 deletions(-) diff --git a/examples/vpv.rs b/examples/vpv.rs index 7f63bbb..ce5f8d9 100644 --- a/examples/vpv.rs +++ b/examples/vpv.rs @@ -137,7 +137,7 @@ cargo run --example vpv /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); } diff --git a/src/cursive.rs b/src/cursive.rs index 495da61..970bd41 100644 --- a/src/cursive.rs +++ b/src/cursive.rs @@ -43,8 +43,8 @@ pub struct Cursive { backend: Box, - cb_source: Receiver>, - cb_sink: Sender>, + cb_source: Receiver>, + cb_sink: Sender>, // User-provided data. user_data: Box, @@ -57,25 +57,7 @@ pub struct Cursive { pub type ScreenId = usize; /// Convenient alias to the result of `Cursive::cb_sink`. -pub type CbSink = Sender>; - -/// Asynchronous callback function trait. -/// -/// Every `FnOnce(&mut Cursive) -> () + Send` automatically -/// implements this. -/// -/// This is a workaround only because `Box` is not -/// working and `FnBox` is unstable. -pub trait CbFunc: Send { - /// Calls the function. - fn call_box(self: Box, _: &mut Cursive); -} - -impl () + Send> CbFunc for F { - fn call_box(self: Box, siv: &mut Cursive) { - (*self)(siv) - } -} +pub type CbSink = Sender>; 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; diff --git a/src/lib.rs b/src/lib.rs index e75f110..4326cad 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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;