async-await: add current_thread::Runtime::block_on_async (#1072)

This function is used by the Tokio macros introduced by #1058  but was
omitted from the PR.
This commit is contained in:
Carl Lerche
2019-04-30 19:55:22 -07:00
committed by GitHub
parent 219f24cbf1
commit 4ef736b9d5
8 changed files with 73 additions and 15 deletions
+1 -1
View File
@@ -37,7 +37,7 @@ tokio-codec = { path = "../tokio-codec" }
tokio-current-thread = { path = "../tokio-current-thread" }
tokio-executor = { path = "../tokio-executor" }
tokio-fs = { path = "../tokio-fs" }
# tokio-futures = { path = "../" }
tokio-futures = { path = "../tokio-futures" }
tokio-io = { path = "../tokio-io" }
tokio-reactor = { path = "../tokio-reactor" }
tokio-signal = { path = "../tokio-signal" }
+22
View File
@@ -0,0 +1,22 @@
#![feature(await_macro, async_await)]
use tokio::await;
use tokio::timer::Delay;
use std::time::{Duration, Instant};
#[tokio::test]
async fn success_no_async() {
assert!(true);
}
#[tokio::test]
#[should_panic]
async fn fail_no_async() {
assert!(false);
}
#[tokio::test]
async fn use_timer() {
let when = Instant::now() + Duration::from_millis(10);
await!(Delay::new(when));
}
+5 -10
View File
@@ -71,17 +71,12 @@ jobs:
tokio-buf:
- util
# Check async / await
- template: ci/azure-cargo-check.yml
# Run async-await tests
- template: ci/azure-test-nightly.yml
parameters:
name: async_await
displayName: Async / Await
name: test_nightly
displayName: Test Async / Await
rust: nightly-2019-04-25
noDefaultFeatures: ''
benches: true
crates:
tokio:
- async-await-preview
# Try cross compiling
- template: ci/azure-cross-compile.yml
@@ -113,7 +108,7 @@ jobs:
- test_sub_cross
- test_linux
- features
- async_await
- test_nightly
- cross_32bit_linux
- minrust
- tsan
-4
View File
@@ -27,7 +27,3 @@ jobs:
- script: cargo check ${{ parameters.noDefaultFeatures }} --features ${{ feature }}
displayName: Check `${{ crate.key }}`, features = ${{ feature }}
workingDirectory: $(Build.SourcesDirectory)/${{ crate.key }}
- ${{ if parameters.benches }}:
- script: cargo check --benches --all
displayName: Check benchmarks
+23
View File
@@ -0,0 +1,23 @@
jobs:
- job: ${{ parameters.name }}
displayName: ${{ parameters.displayName }}
pool:
vmImage: ubuntu-16.04
steps:
- template: azure-install-rust.yml
parameters:
rust_version: ${{ parameters.rust }}
- template: azure-patch-crates.yml
- script: cargo test
displayName: cargo +nightly test -p async-await
workingDirectory: $(Build.SourcesDirectory)/async-await
- script: cargo check --all
displayName: cargo +nightly check --all
# Check benches
- script: cargo check --benches --all
displayName: Check benchmarks
+2
View File
@@ -58,6 +58,7 @@ pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream {
let ret = &input.decl.output;
let name = &input.ident;
let body = &input.block;
let attrs = &input.attrs;
if input.asyncness.is_none() {
let tokens = quote_spanned! { input.span() =>
@@ -69,6 +70,7 @@ pub fn test(_attr: TokenStream, item: TokenStream) -> TokenStream {
let result = quote! {
#[test]
#(#attrs)*
fn #name() #ret {
let mut rt = tokio::runtime::current_thread::Runtime::new().unwrap();
rt.block_on_async(async { #body })
@@ -0,0 +1,17 @@
use super::Runtime;
use std::future::Future;
impl Runtime {
/// Like `block_on`, but takes an `async` block
pub fn block_on_async<F>(&mut self, future: F) -> F::Output
where
F: Future,
{
use tokio_futures::compat;
match self.block_on(compat::infallible_into_01(future)) {
Ok(v) => v,
Err(_) => unreachable!(),
}
}
}
+3
View File
@@ -69,6 +69,9 @@
mod builder;
mod runtime;
#[cfg(feature = "async-await-preview")]
mod async_await;
pub use self::builder::Builder;
pub use self::runtime::{Runtime, Handle};
pub use tokio_current_thread::spawn;