Skip to content

Commit 8068548

Browse files
committed
Add IntoPair trait to allow returning Inhibit when a pair is needed
1 parent f4c4a48 commit 8068548

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/option.rs renamed to src/into.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2020
*/
2121

22+
use gtk::Inhibit;
23+
2224
#[doc(hidden)]
2325
pub trait IntoOption<T> {
2426
#[doc(hidden)]
@@ -36,3 +38,21 @@ impl<T> IntoOption<T> for () {
3638
None
3739
}
3840
}
41+
42+
#[doc(hidden)]
43+
pub trait IntoPair<A, B> {
44+
#[doc(hidden)]
45+
fn into_pair(self) -> (A, B);
46+
}
47+
48+
impl<A> IntoPair<Option<A>, Inhibit> for Inhibit {
49+
fn into_pair(self) -> (Option<A>, Inhibit) {
50+
(None, self)
51+
}
52+
}
53+
54+
impl<A, B> IntoPair<A, B> for (A, B) {
55+
fn into_pair(self) -> (A, B) {
56+
self
57+
}
58+
}

src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ extern crate relm_core;
8484

8585
mod component;
8686
mod container;
87+
mod into;
8788
mod macros;
88-
mod option;
8989
mod stream;
9090
mod widget;
9191

@@ -110,7 +110,7 @@ pub use relm_core::EventStream;
110110

111111
pub use container::{Container, ContainerWidget, RelmContainer};
112112
pub use component::Component;
113-
pub use option::IntoOption;
113+
pub use into::{IntoOption, IntoPair};
114114
use stream::ToStream;
115115
pub use widget::Widget;
116116

src/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ macro_rules! connect {
7474
($relm:expr, $widget:expr, $event:ident($($args:pat),*), return $msg:expr) => {{
7575
let stream = $relm.stream().clone();
7676
$widget.$event(move |$($args),*| {
77-
let (msg, return_value) = $msg;
77+
let (msg, return_value) = ::relm::IntoPair::into_pair($msg);
7878
let msg: Option<_> = ::relm::IntoOption::into_option(msg);
7979
if let Some(msg) = msg {
8080
stream.emit(msg);
@@ -96,7 +96,7 @@ macro_rules! connect {
9696
let $widget_clone = $widget_clone.upgrade().expect("upgrade should always work");
9797
check_recursion!($widget_clone);
9898
let mut $widget_clone = $widget_clone.borrow_mut();
99-
let (msg, return_value) = $msg;
99+
let (msg, return_value) = ::relm::IntoPair::into_pair($msg);
100100
let msg: Option<_> = ::relm::IntoOption::into_option(msg);
101101
if let Some(msg) = msg {
102102
stream.emit(msg);

0 commit comments

Comments
 (0)