Skip to content

Commit f7cd7c9

Browse files
committed
feat(mpz-common): scoped! macro
1 parent 9ec66f2 commit f7cd7c9

File tree

4 files changed

+83
-57
lines changed

4 files changed

+83
-57
lines changed

crates/mpz-common/src/executor/dummy.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ impl Context for DummyExecutor {
125125
#[cfg(test)]
126126
mod tests {
127127
use futures::executor::block_on;
128-
use scoped_futures::ScopedFutureExt;
128+
129+
use crate::scoped;
129130

130131
use super::*;
131132

@@ -142,18 +143,8 @@ mod tests {
142143
let a = &mut self.a;
143144
let b = &mut self.b;
144145
ctx.join(
145-
|ctx| {
146-
async move {
147-
*a = ctx.id().clone();
148-
}
149-
.scope_boxed()
150-
},
151-
|ctx| {
152-
async move {
153-
*b = ctx.id().clone();
154-
}
155-
.scope_boxed()
156-
},
146+
scoped!(|ctx| *a = ctx.id().clone()),
147+
scoped!(|ctx| *b = ctx.id().clone()),
157148
)
158149
.await
159150
.unwrap();

crates/mpz-common/src/executor/mt.rs

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -298,10 +298,9 @@ where
298298

299299
#[cfg(test)]
300300
mod tests {
301-
use scoped_futures::ScopedFutureExt;
302301
use serio::{stream::IoStreamExt, SinkExt};
303302

304-
use crate::{executor::test_mt_executor, join};
303+
use crate::{executor::test_mt_executor, scoped};
305304

306305
use super::*;
307306

@@ -318,15 +317,11 @@ mod tests {
318317
let a = &mut self.a;
319318
let b = &mut self.b;
320319

321-
join! {
322-
ctx,
323-
async {
324-
*a = ctx.id().clone();
325-
},
326-
async {
327-
*b = ctx.id().clone();
328-
}
329-
}
320+
ctx.join(
321+
scoped!(|ctx| *a = ctx.id().clone()),
322+
scoped!(|ctx| *b = ctx.id().clone()),
323+
)
324+
.await
330325
.unwrap();
331326

332327
// Make sure we can mutate the fields after borrowing them in the async closures.
@@ -355,22 +350,13 @@ mod tests {
355350
let (mut ctx_a, mut ctx_b) =
356351
futures::try_join!(exec_a.new_thread(), exec_b.new_thread()).unwrap();
357352

358-
let (_, received) = futures::join!(
359-
async {
360-
ctx_a
361-
.blocking(|ctx| async { ctx.io_mut().send(1u8).await.unwrap() }.scope_boxed())
362-
.await
363-
.unwrap()
364-
},
365-
async {
366-
ctx_b
367-
.blocking(|ctx| {
368-
async { ctx.io_mut().expect_next::<u8>().await.unwrap() }.scope_boxed()
369-
})
370-
.await
371-
.unwrap()
372-
}
373-
);
353+
let (_, received) = futures::try_join!(
354+
ctx_a.blocking(scoped!(|ctx| ctx.io_mut().send(1u8).await.unwrap())),
355+
ctx_b.blocking(scoped!(|ctx| async move {
356+
ctx.io_mut().expect_next::<u8>().await.unwrap()
357+
}))
358+
)
359+
.unwrap();
374360

375361
assert_eq!(received, 1u8);
376362
assert!(ctx_a.inner.is_some());

crates/mpz-common/src/executor/st.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,10 @@ where
125125
#[cfg(test)]
126126
mod tests {
127127
use futures::executor::block_on;
128-
use scoped_futures::ScopedFutureExt;
129128
use serio::channel::duplex;
130129

130+
use crate::scoped;
131+
131132
use super::*;
132133

133134
#[derive(Debug, Default)]
@@ -143,18 +144,8 @@ mod tests {
143144
let a = &mut self.a;
144145
let b = &mut self.b;
145146
ctx.join(
146-
|ctx| {
147-
async move {
148-
*a = ctx.id().clone();
149-
}
150-
.scope_boxed()
151-
},
152-
|ctx| {
153-
async move {
154-
*b = ctx.id().clone();
155-
}
156-
.scope_boxed()
157-
},
147+
scoped!(|ctx| *a = ctx.id().clone()),
148+
scoped!(|ctx| *b = ctx.id().clone()),
158149
)
159150
.await
160151
.unwrap();
@@ -180,10 +171,7 @@ mod tests {
180171
let mut ctx = STExecutor::new(io);
181172

182173
block_on(async {
183-
let id = ctx
184-
.blocking(|ctx| async move { ctx.id().clone() }.scope_boxed())
185-
.await
186-
.unwrap();
174+
let id = ctx.blocking(scoped!(|ctx| ctx.id().clone())).await.unwrap();
187175

188176
assert_eq!(&id, ctx.id());
189177
assert!(ctx.inner.is_some());

crates/mpz-common/src/lib.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,64 @@ pub use id::{Counter, ThreadId};
2828

2929
// Re-export scoped-futures for use with the callback-like API in `Context`.
3030
pub use scoped_futures;
31+
32+
/// A convenience macro for creating a closure which returns a scoped future.
33+
///
34+
/// # Example
35+
///
36+
/// ```
37+
/// # use mpz_common::scoped;
38+
///
39+
/// let closure = scoped!(|a: u8, b: u16| a as u16 + b);
40+
/// let fut = closure(1, 2);
41+
///
42+
/// fn is_future<T: futures::Future<Output = u16>>(_: T) {}
43+
///
44+
/// is_future(fut);
45+
/// ```
46+
#[macro_export]
47+
macro_rules! scoped {
48+
// Async move block.
49+
(| $($arg:ident $(: $typ:ty)?),* | async move $body:block) => {{
50+
#[allow(unused_imports)]
51+
use $crate::scoped_futures::ScopedFutureExt;
52+
| $($arg $( : $typ )?),* | async move $body.scope_boxed()
53+
}};
54+
// No async block.
55+
(| $($arg:ident $(: $typ:ty)?),* | $body:expr) => {{
56+
#[allow(unused_imports)]
57+
use $crate::scoped_futures::ScopedFutureExt;
58+
| $($arg $( : $typ )?),* | async move { $body }.scope_boxed()
59+
}};
60+
}
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use futures::Future;
65+
66+
#[test]
67+
fn test_scoped_macro() {
68+
fn assert_signature<T, Fut>(_: T)
69+
where
70+
T: Fn(u8, u16) -> Fut,
71+
Fut: Future<Output = u8>,
72+
{
73+
}
74+
75+
assert_signature(scoped! {
76+
|a: u8, _b: u16| async move { a }
77+
});
78+
79+
assert_signature(scoped! {
80+
|a, _b| a
81+
});
82+
83+
assert_signature(scoped! {
84+
|a: u8, _b| a
85+
});
86+
87+
assert_signature(scoped! {
88+
|a, _b: u16| a
89+
});
90+
}
91+
}

0 commit comments

Comments
 (0)