summaryrefslogtreecommitdiffstats
path: root/tests-integration
diff options
context:
space:
mode:
authorCarl Lerche <me@carllerche.com>2020-01-22 18:59:22 -0800
committerGitHub <noreply@github.com>2020-01-22 18:59:22 -0800
commit8cf98d694663e8397bfc337e7a4676567287bfae (patch)
tree50b214c2e29a257633494a64eed35c5b6f568eba /tests-integration
parentf9ea576ccae5beffeaa2f2c48c2c0d2f9449673b (diff)
Provide `select!` macro (#2152)
Provides a `select!` macro for concurrently waiting on multiple async expressions. The macro has similar goals and syntax as the one provided by the `futures` crate, but differs significantly in implementation. First, this implementation does not require special traits to be implemented on futures or streams (i.e., no `FuseFuture`). A design goal is to be able to pass a "plain" async fn result into the select! macro. Even without `FuseFuture`, this `select!` implementation is able to handle all cases the `futures::select!` macro can handle. It does this by supporting pre-poll conditions on branches and result pattern matching. For pre-conditions, each branch is able to include a condition that disables the branch if it evaluates to false. This allows the user to guard futures that have already been polled, preventing double polling. Pattern matching can be used to disable streams that complete. A second big difference is the macro is implemented almost entirely as a declarative macro. The biggest advantage to using this strategy is that the user will not need to alter the rustc recursion limit except in the most extreme cases. The resulting future also tends to be smaller in many cases.
Diffstat (limited to 'tests-integration')
-rw-r--r--tests-integration/Cargo.toml9
-rw-r--r--tests-integration/tests/macros_select.rs33
2 files changed, 41 insertions, 1 deletions
diff --git a/tests-integration/Cargo.toml b/tests-integration/Cargo.toml
index 3cca17a7..6f84afd9 100644
--- a/tests-integration/Cargo.toml
+++ b/tests-integration/Cargo.toml
@@ -6,7 +6,14 @@ edition = "2018"
publish = false
[features]
-full = ["tokio/full", "tokio-test"]
+full = [
+ "macros",
+ "rt-core",
+ "rt-threaded",
+
+ "tokio/full",
+ "tokio-test"
+]
macros = ["tokio/macros"]
rt-core = ["tokio/rt-core"]
rt-threaded = ["rt-core", "tokio/rt-threaded"]
diff --git a/tests-integration/tests/macros_select.rs b/tests-integration/tests/macros_select.rs
new file mode 100644
index 00000000..4c4fef7c
--- /dev/null
+++ b/tests-integration/tests/macros_select.rs
@@ -0,0 +1,33 @@
+#![cfg(feature = "macros")]
+
+use futures::channel::oneshot;
+use futures::executor::block_on;
+use std::thread;
+
+#[test]
+fn join_with_select() {
+ block_on(async {
+ let (tx1, mut rx1) = oneshot::channel::<i32>();
+ let (tx2, mut rx2) = oneshot::channel::<i32>();
+
+ thread::spawn(move || {
+ tx1.send(123).unwrap();
+ tx2.send(456).unwrap();
+ });
+
+ let mut a = None;
+ let mut b = None;
+
+ while a.is_none() || b.is_none() {
+ tokio::select! {
+ v1 = (&mut rx1), if a.is_none() => a = Some(v1.unwrap()),
+ v2 = (&mut rx2), if b.is_none() => b = Some(v2.unwrap()),
+ }
+ }
+
+ let (a, b) = (a.unwrap(), b.unwrap());
+
+ assert_eq!(a, 123);
+ assert_eq!(b, 456);
+ });
+}