Skip to content

Commit

Permalink
Add IntoPair trait to allow returning Inhibit when a pair is needed
Browse files Browse the repository at this point in the history
  • Loading branch information
antoyo committed Aug 15, 2017
1 parent f4c4a48 commit 8068548
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
20 changes: 20 additions & 0 deletions src/option.rs → src/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ extern crate relm_core;

mod component;
mod container;
mod into;
mod macros;
mod option;
mod stream;
mod widget;

Expand All @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 8068548

Please sign in to comment.