Permalink
Browse files

Add IntoPair trait to allow returning Inhibit when a pair is needed

  • Loading branch information...
antoyo committed May 20, 2017
1 parent f4c4a48 commit 8068548a6dd04206e2eb76fa1a2b0a103c9ea8e6
Showing with 24 additions and 4 deletions.
  1. +20 −0 src/{option.rs → into.rs}
  2. +2 −2 src/lib.rs
  3. +2 −2 src/macros.rs
@@ -19,6 +19,8 @@
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
use gtk::Inhibit;
#[doc(hidden)]
pub trait IntoOption<T> {
#[doc(hidden)]
@@ -36,3 +38,21 @@ impl<T> IntoOption<T> for () {
None
}
}
#[doc(hidden)]
pub trait IntoPair<A, B> {
#[doc(hidden)]
fn into_pair(self) -> (A, B);
}
impl<A> IntoPair<Option<A>, Inhibit> for Inhibit {
fn into_pair(self) -> (Option<A>, Inhibit) {
(None, self)
}
}
impl<A, B> IntoPair<A, B> for (A, B) {
fn into_pair(self) -> (A, B) {
self
}
}
View
@@ -84,8 +84,8 @@ extern crate relm_core;
mod component;
mod container;
mod into;
mod macros;
mod option;
mod stream;
mod widget;
@@ -110,7 +110,7 @@ pub use relm_core::EventStream;
pub use container::{Container, ContainerWidget, RelmContainer};
pub use component::Component;
pub use option::IntoOption;
pub use into::{IntoOption, IntoPair};
use stream::ToStream;
pub use widget::Widget;
View
@@ -74,7 +74,7 @@ macro_rules! connect {
($relm:expr, $widget:expr, $event:ident($($args:pat),*), return $msg:expr) => {{
let stream = $relm.stream().clone();
$widget.$event(move |$($args),*| {
let (msg, return_value) = $msg;
let (msg, return_value) = ::relm::IntoPair::into_pair($msg);
let msg: Option<_> = ::relm::IntoOption::into_option(msg);
if let Some(msg) = msg {
stream.emit(msg);
@@ -96,7 +96,7 @@ macro_rules! connect {
let $widget_clone = $widget_clone.upgrade().expect("upgrade should always work");
check_recursion!($widget_clone);
let mut $widget_clone = $widget_clone.borrow_mut();
let (msg, return_value) = $msg;
let (msg, return_value) = ::relm::IntoPair::into_pair($msg);
let msg: Option<_> = ::relm::IntoOption::into_option(msg);
if let Some(msg) = msg {
stream.emit(msg);

0 comments on commit 8068548

Please sign in to comment.