From 1b16bb54cb0435aa4c6ca27924bf865fa9d58ec4 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 10 Oct 2019 01:33:47 +0200 Subject: [PATCH 1/4] task::block_on -> thread::spawn_task Signed-off-by: Yoshua Wuyts --- CHANGELOG.md | 2 +- README.md | 4 +- benches/task_local.rs | 2 +- docs/src/concepts/tasks.md | 8 +-- docs/src/tutorial/accept_loop.md | 4 +- docs/src/tutorial/all_together.md | 2 +- docs/src/tutorial/handling_disconnection.md | 2 +- docs/src/tutorial/implementing_a_client.md | 2 +- examples/a-chat/client.rs | 2 +- examples/a-chat/server.rs | 2 +- examples/hello-world.rs | 2 +- examples/line-count.rs | 2 +- examples/list-dir.rs | 2 +- examples/logging.rs | 2 +- examples/print-file.rs | 2 +- examples/socket-timeouts.rs | 2 +- examples/stdin-echo.rs | 2 +- examples/stdin-timeout.rs | 2 +- examples/surf-web.rs | 2 +- examples/task-local.rs | 2 +- examples/task-name.rs | 2 +- examples/tcp-client.rs | 2 +- examples/tcp-echo.rs | 2 +- examples/udp-client.rs | 2 +- examples/udp-echo.rs | 2 +- src/fs/canonicalize.rs | 2 +- src/fs/copy.rs | 2 +- src/fs/create_dir.rs | 2 +- src/fs/create_dir_all.rs | 2 +- src/fs/dir_builder.rs | 2 +- src/fs/dir_entry.rs | 8 +-- src/fs/file.rs | 21 ++++--- src/fs/file_type.rs | 6 +- src/fs/hard_link.rs | 2 +- src/fs/metadata.rs | 18 +++--- src/fs/mod.rs | 2 +- src/fs/open_options.rs | 20 +++--- src/fs/permissions.rs | 4 +- src/fs/read.rs | 2 +- src/fs/read_dir.rs | 2 +- src/fs/read_link.rs | 2 +- src/fs/read_to_string.rs | 2 +- src/fs/remove_dir.rs | 2 +- src/fs/remove_dir_all.rs | 2 +- src/fs/remove_file.rs | 2 +- src/fs/rename.rs | 2 +- src/fs/set_permissions.rs | 2 +- src/fs/symlink_metadata.rs | 2 +- src/fs/write.rs | 2 +- src/future/pending.rs | 2 +- src/future/poll_fn.rs | 2 +- src/future/ready.rs | 2 +- src/future/timeout.rs | 2 +- src/io/buf_read/mod.rs | 8 +-- src/io/buf_reader.rs | 14 ++--- src/io/buf_writer.rs | 20 +++--- src/io/copy.rs | 2 +- src/io/cursor.rs | 2 +- src/io/empty.rs | 2 +- src/io/mod.rs | 2 +- src/io/read/bytes.rs | 2 +- src/io/read/chain.rs | 8 +-- src/io/read/mod.rs | 18 +++--- src/io/read/take.rs | 12 ++-- src/io/repeat.rs | 2 +- src/io/seek.rs | 2 +- src/io/sink.rs | 2 +- src/io/stderr.rs | 2 +- src/io/stdin.rs | 4 +- src/io/stdout.rs | 2 +- src/io/timeout.rs | 2 +- src/io/write/mod.rs | 6 +- src/lib.rs | 7 ++- src/net/addr.rs | 2 +- src/net/mod.rs | 2 +- src/net/tcp/listener.rs | 10 +-- src/net/tcp/stream.rs | 20 +++--- src/net/udp/mod.rs | 20 +++--- src/os/unix/fs.rs | 2 +- src/os/unix/net/datagram.rs | 24 +++---- src/os/unix/net/listener.rs | 10 +-- src/os/unix/net/stream.rs | 12 ++-- src/stream/empty.rs | 2 +- src/stream/extend.rs | 2 +- src/stream/from_stream.rs | 8 +-- src/stream/mod.rs | 2 +- src/stream/once.rs | 2 +- src/stream/repeat.rs | 2 +- src/stream/stream/mod.rs | 62 +++++++++---------- src/sync/barrier.rs | 12 ++-- src/sync/mod.rs | 2 +- src/sync/mutex.rs | 14 ++--- src/sync/rwlock.rs | 12 ++-- src/task/mod.rs | 18 +++--- src/task/pool.rs | 4 +- src/task/sleep.rs | 4 +- src/task/task.rs | 8 +-- src/task/task_local.rs | 12 ++-- src/task/worker.rs | 4 +- src/thread/mod.rs | 8 +++ .../block_on.rs => thread/spawn_task.rs} | 18 +++--- tests/addr.rs | 2 +- tests/block_on.rs | 4 +- tests/io_timeout.rs | 6 +- tests/mutex.rs | 4 +- tests/rwlock.rs | 8 +-- tests/task_local.rs | 2 +- tests/tcp.rs | 4 +- tests/udp.rs | 2 +- tests/uds.rs | 10 +-- 110 files changed, 324 insertions(+), 316 deletions(-) create mode 100644 src/thread/mod.rs rename src/{task/block_on.rs => thread/spawn_task.rs} (93%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 77ab025a1..6e658eaac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -135,7 +135,7 @@ task::blocking(async { - Refactored the scheduling algorithm of our executor to use work stealing - Refactored the network driver, removing 400 lines of code -- Removed the `Send` bound from `task::block_on` +- Removed the `Send` bound from `thread::spawn_task` - Removed `Unpin` bound from `impl Stream for T` # [0.99.5] - 2019-09-12 diff --git a/README.md b/README.md index ae129aa16..b1e3556c3 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ $ cargo add async-std use async_std::task; fn main() { - task::block_on(async { + thread::spawn_task(async { println!("Hello, world!"); }) } @@ -72,7 +72,7 @@ async fn get() -> io::Result> { } fn main() { - task::block_on(async { + thread::spawn_task(async { let raw_response = get().await.expect("request"); let response = String::from_utf8(raw_response) .expect("utf8 conversion"); diff --git a/benches/task_local.rs b/benches/task_local.rs index 3b6c85902..ce432c171 100644 --- a/benches/task_local.rs +++ b/benches/task_local.rs @@ -13,7 +13,7 @@ fn get(b: &mut Bencher) { } let mut sum = 0; - task::block_on(async { + thread::spawn_task(async { b.iter(|| VAL.with(|v| sum += v)); }); black_box(sum); diff --git a/docs/src/concepts/tasks.md b/docs/src/concepts/tasks.md index d4037a3bb..858b59470 100644 --- a/docs/src/concepts/tasks.md +++ b/docs/src/concepts/tasks.md @@ -24,7 +24,7 @@ fn main() { } }); println!("Started task!"); - task::block_on(reader_task); + thread::spawn_task(reader_task); println!("Stopped task!"); } ``` @@ -86,7 +86,7 @@ Tasks in `async_std` are one of the core abstractions. Much like Rust's `thread` # extern crate async_std; # use async_std::task; fn main() { - task::block_on(async { + thread::spawn_task(async { // this is std::fs, which blocks std::fs::read_to_string("test_file"); }) @@ -107,7 +107,7 @@ In practice, that means that `block_on` propagates panics to the blocking compon # extern crate async_std; # use async_std::task; fn main() { - task::block_on(async { + thread::spawn_task(async { panic!("test"); }); } @@ -128,7 +128,7 @@ task::spawn(async { panic!("test"); }); -task::block_on(async { +thread::spawn_task(async { task::sleep(Duration::from_millis(10000)).await; }) ``` diff --git a/docs/src/tutorial/accept_loop.md b/docs/src/tutorial/accept_loop.md index dc748bb4d..e1b6464dc 100644 --- a/docs/src/tutorial/accept_loop.md +++ b/docs/src/tutorial/accept_loop.md @@ -85,7 +85,7 @@ Finally, let's add main: // main fn run() -> Result<()> { let fut = accept_loop("127.0.0.1:8080"); - task::block_on(fut) + thread::spawn_task(fut) } ``` @@ -93,4 +93,4 @@ The crucial thing to realise that is in Rust, unlike other languages, calling an Async functions only construct futures, which are inert state machines. To start stepping through the future state-machine in an async function, you should use `.await`. In a non-async function, a way to execute a future is to hand it to the executor. -In this case, we use `task::block_on` to execute a future on the current thread and block until it's done. +In this case, we use `thread::spawn_task` to execute a future on the current thread and block until it's done. diff --git a/docs/src/tutorial/all_together.md b/docs/src/tutorial/all_together.md index dcc06616a..b030183a1 100644 --- a/docs/src/tutorial/all_together.md +++ b/docs/src/tutorial/all_together.md @@ -25,7 +25,7 @@ type Receiver = mpsc::UnboundedReceiver; // main fn run() -> Result<()> { - task::block_on(accept_loop("127.0.0.1:8080")) + thread::spawn_task(accept_loop("127.0.0.1:8080")) } fn spawn_and_log_error(fut: F) -> task::JoinHandle<()> diff --git a/docs/src/tutorial/handling_disconnection.md b/docs/src/tutorial/handling_disconnection.md index a1f51d134..c4a3f2f63 100644 --- a/docs/src/tutorial/handling_disconnection.md +++ b/docs/src/tutorial/handling_disconnection.md @@ -147,7 +147,7 @@ enum Void {} // main fn run() -> Result<()> { - task::block_on(accept_loop("127.0.0.1:8080")) + thread::spawn_task(accept_loop("127.0.0.1:8080")) } async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> { diff --git a/docs/src/tutorial/implementing_a_client.md b/docs/src/tutorial/implementing_a_client.md index 3aac67f35..a8cb34b8c 100644 --- a/docs/src/tutorial/implementing_a_client.md +++ b/docs/src/tutorial/implementing_a_client.md @@ -29,7 +29,7 @@ type Result = std::result::Result // main fn run() -> Result<()> { - task::block_on(try_run("127.0.0.1:8080")) + thread::spawn_task(try_run("127.0.0.1:8080")) } async fn try_run(addr: impl ToSocketAddrs) -> Result<()> { diff --git a/examples/a-chat/client.rs b/examples/a-chat/client.rs index 48634ba03..d103dc15d 100644 --- a/examples/a-chat/client.rs +++ b/examples/a-chat/client.rs @@ -11,7 +11,7 @@ use async_std::{ type Result = std::result::Result>; pub(crate) fn main() -> Result<()> { - task::block_on(try_main("127.0.0.1:8080")) + thread::spawn_task(try_main("127.0.0.1:8080")) } async fn try_main(addr: impl ToSocketAddrs) -> Result<()> { diff --git a/examples/a-chat/server.rs b/examples/a-chat/server.rs index 77ebfd1e3..b5cafe2de 100644 --- a/examples/a-chat/server.rs +++ b/examples/a-chat/server.rs @@ -20,7 +20,7 @@ type Receiver = mpsc::UnboundedReceiver; enum Void {} pub(crate) fn main() -> Result<()> { - task::block_on(accept_loop("127.0.0.1:8080")) + thread::spawn_task(accept_loop("127.0.0.1:8080")) } async fn accept_loop(addr: impl ToSocketAddrs) -> Result<()> { diff --git a/examples/hello-world.rs b/examples/hello-world.rs index 8d663339f..0f1e0df52 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -7,5 +7,5 @@ async fn say_hi() { } fn main() { - task::block_on(say_hi()) + thread::spawn_task(say_hi()) } diff --git a/examples/line-count.rs b/examples/line-count.rs index 847352d62..a33ea1520 100644 --- a/examples/line-count.rs +++ b/examples/line-count.rs @@ -10,7 +10,7 @@ use async_std::task; fn main() -> io::Result<()> { let path = args().nth(1).expect("missing path argument"); - task::block_on(async { + thread::spawn_task(async { let file = File::open(&path).await?; let mut lines = BufReader::new(file).lines(); let mut count = 0u64; diff --git a/examples/list-dir.rs b/examples/list-dir.rs index e4b62fc3a..056860d19 100644 --- a/examples/list-dir.rs +++ b/examples/list-dir.rs @@ -10,7 +10,7 @@ use async_std::task; fn main() -> io::Result<()> { let path = args().nth(1).expect("missing path argument"); - task::block_on(async { + thread::spawn_task(async { let mut dir = fs::read_dir(&path).await?; while let Some(res) = dir.next().await { diff --git a/examples/logging.rs b/examples/logging.rs index bd55aaa0c..dfd8c145d 100644 --- a/examples/logging.rs +++ b/examples/logging.rs @@ -5,7 +5,7 @@ use async_std::task; fn main() { femme::start(log::LevelFilter::Trace).unwrap(); - task::block_on(async { + thread::spawn_task(async { let handle = task::spawn(async { log::info!("Hello world!"); }); diff --git a/examples/print-file.rs b/examples/print-file.rs index e2cdde794..8eb121ec4 100644 --- a/examples/print-file.rs +++ b/examples/print-file.rs @@ -12,7 +12,7 @@ const LEN: usize = 16 * 1024; // 16 Kb fn main() -> io::Result<()> { let path = args().nth(1).expect("missing path argument"); - task::block_on(async { + thread::spawn_task(async { let mut file = File::open(&path).await?; let mut stdout = io::stdout(); let mut buf = vec![0u8; LEN]; diff --git a/examples/socket-timeouts.rs b/examples/socket-timeouts.rs index f4db5c7b2..8412c158a 100644 --- a/examples/socket-timeouts.rs +++ b/examples/socket-timeouts.rs @@ -20,7 +20,7 @@ async fn get() -> io::Result> { } fn main() { - task::block_on(async { + thread::spawn_task(async { let raw_response = get().await.expect("request"); let response = String::from_utf8(raw_response).expect("utf8 conversion"); println!("received: {}", response); diff --git a/examples/stdin-echo.rs b/examples/stdin-echo.rs index 9514219e8..c671c57cf 100644 --- a/examples/stdin-echo.rs +++ b/examples/stdin-echo.rs @@ -5,7 +5,7 @@ use async_std::prelude::*; use async_std::task; fn main() -> io::Result<()> { - task::block_on(async { + thread::spawn_task(async { let stdin = io::stdin(); let mut stdout = io::stdout(); let mut line = String::new(); diff --git a/examples/stdin-timeout.rs b/examples/stdin-timeout.rs index f13c38748..b061b667a 100644 --- a/examples/stdin-timeout.rs +++ b/examples/stdin-timeout.rs @@ -7,7 +7,7 @@ use async_std::task; fn main() -> io::Result<()> { // This async scope times out after 5 seconds. - task::block_on(io::timeout(Duration::from_secs(5), async { + thread::spawn_task(io::timeout(Duration::from_secs(5), async { let stdin = io::stdin(); // Read a line from the standard input and display it. diff --git a/examples/surf-web.rs b/examples/surf-web.rs index b3101d15c..b4666e862 100644 --- a/examples/surf-web.rs +++ b/examples/surf-web.rs @@ -4,7 +4,7 @@ use async_std::task; fn main() -> Result<(), surf::Exception> { - task::block_on(async { + thread::spawn_task(async { let url = "https://www.rust-lang.org"; let mut response = surf::get(url).await?; let body = response.body_string().await?; diff --git a/examples/task-local.rs b/examples/task-local.rs index 320b383e1..484751b39 100644 --- a/examples/task-local.rs +++ b/examples/task-local.rs @@ -10,7 +10,7 @@ task_local! { } fn main() { - task::block_on(async { + thread::spawn_task(async { println!("var = {}", VAR.with(|v| v.get())); VAR.with(|v| v.set(2)); println!("var = {}", VAR.with(|v| v.get())); diff --git a/examples/task-name.rs b/examples/task-name.rs index f0bfdff86..19a3618a1 100644 --- a/examples/task-name.rs +++ b/examples/task-name.rs @@ -7,7 +7,7 @@ async fn print_name() { } fn main() { - task::block_on(async { + thread::spawn_task(async { task::Builder::new() .name("my-task".to_string()) .spawn(print_name()) diff --git a/examples/tcp-client.rs b/examples/tcp-client.rs index 02250eec5..37291ec54 100644 --- a/examples/tcp-client.rs +++ b/examples/tcp-client.rs @@ -18,7 +18,7 @@ use async_std::prelude::*; use async_std::task; fn main() -> io::Result<()> { - task::block_on(async { + thread::spawn_task(async { let mut stream = TcpStream::connect("127.0.0.1:8080").await?; println!("Connected to {}", &stream.peer_addr()?); diff --git a/examples/tcp-echo.rs b/examples/tcp-echo.rs index 7c50be016..fe90fae1b 100644 --- a/examples/tcp-echo.rs +++ b/examples/tcp-echo.rs @@ -21,7 +21,7 @@ async fn process(stream: TcpStream) -> io::Result<()> { } fn main() -> io::Result<()> { - task::block_on(async { + thread::spawn_task(async { let listener = TcpListener::bind("127.0.0.1:8080").await?; println!("Listening on {}", listener.local_addr()?); diff --git a/examples/udp-client.rs b/examples/udp-client.rs index 180db0b83..f56ddbdf4 100644 --- a/examples/udp-client.rs +++ b/examples/udp-client.rs @@ -17,7 +17,7 @@ use async_std::net::UdpSocket; use async_std::task; fn main() -> io::Result<()> { - task::block_on(async { + thread::spawn_task(async { let socket = UdpSocket::bind("127.0.0.1:8081").await?; println!("Listening on {}", socket.local_addr()?); diff --git a/examples/udp-echo.rs b/examples/udp-echo.rs index f436d01bd..ae397caf7 100644 --- a/examples/udp-echo.rs +++ b/examples/udp-echo.rs @@ -11,7 +11,7 @@ use async_std::net::UdpSocket; use async_std::task; fn main() -> io::Result<()> { - task::block_on(async { + thread::spawn_task(async { let socket = UdpSocket::bind("127.0.0.1:8080").await?; let mut buf = vec![0u8; 1024]; diff --git a/src/fs/canonicalize.rs b/src/fs/canonicalize.rs index c484aeeb5..65a1bd825 100644 --- a/src/fs/canonicalize.rs +++ b/src/fs/canonicalize.rs @@ -23,7 +23,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/copy.rs b/src/fs/copy.rs index fc3fd3e69..0893ee820 100644 --- a/src/fs/copy.rs +++ b/src/fs/copy.rs @@ -31,7 +31,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/create_dir.rs b/src/fs/create_dir.rs index aa2d5724f..9d12ced9b 100644 --- a/src/fs/create_dir.rs +++ b/src/fs/create_dir.rs @@ -25,7 +25,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/create_dir_all.rs b/src/fs/create_dir_all.rs index 33176f75d..f6c25c5db 100644 --- a/src/fs/create_dir_all.rs +++ b/src/fs/create_dir_all.rs @@ -20,7 +20,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/dir_builder.rs b/src/fs/dir_builder.rs index 3064f7a30..39bb85cb0 100644 --- a/src/fs/dir_builder.rs +++ b/src/fs/dir_builder.rs @@ -86,7 +86,7 @@ impl DirBuilder { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::DirBuilder; /// diff --git a/src/fs/dir_entry.rs b/src/fs/dir_entry.rs index 66b3cb7c9..509a2beda 100644 --- a/src/fs/dir_entry.rs +++ b/src/fs/dir_entry.rs @@ -35,7 +35,7 @@ impl DirEntry { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// use async_std::prelude::*; @@ -73,7 +73,7 @@ impl DirEntry { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// use async_std::prelude::*; @@ -111,7 +111,7 @@ impl DirEntry { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// use async_std::prelude::*; @@ -135,7 +135,7 @@ impl DirEntry { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// use async_std::prelude::*; diff --git a/src/fs/file.rs b/src/fs/file.rs index bdf934748..dcff25c5c 100644 --- a/src/fs/file.rs +++ b/src/fs/file.rs @@ -15,6 +15,7 @@ use crate::future; use crate::io::{self, Read, Seek, SeekFrom, Write}; use crate::prelude::*; use crate::task::{self, blocking, Context, Poll, Waker}; +use crate::thread; /// An open file on the filesystem. /// @@ -34,7 +35,7 @@ use crate::task::{self, blocking, Context, Poll, Waker}; /// Create a new file and write some bytes to it: /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::prelude::*; @@ -48,7 +49,7 @@ use crate::task::{self, blocking, Context, Poll, Waker}; /// Read the contents of a file into a vector of bytes: /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::prelude::*; @@ -87,7 +88,7 @@ impl File { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// @@ -122,7 +123,7 @@ impl File { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// @@ -146,7 +147,7 @@ impl File { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::prelude::*; @@ -182,7 +183,7 @@ impl File { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::prelude::*; @@ -216,7 +217,7 @@ impl File { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// @@ -242,7 +243,7 @@ impl File { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// @@ -268,7 +269,7 @@ impl File { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// @@ -292,7 +293,7 @@ impl Drop for File { // non-blocking fashion, but our only other option here is losing data remaining in the // write cache. Good task schedulers should be resilient to occasional blocking hiccups in // file destructors so we don't expect this to be a common problem in practice. - let _ = task::block_on(self.flush()); + let _ = thread::spawn_task(self.flush()); } } diff --git a/src/fs/file_type.rs b/src/fs/file_type.rs index a1627984c..80a0851dd 100644 --- a/src/fs/file_type.rs +++ b/src/fs/file_type.rs @@ -29,7 +29,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -49,7 +49,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -67,7 +67,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/hard_link.rs b/src/fs/hard_link.rs index 5f950cde1..4d42562a9 100644 --- a/src/fs/hard_link.rs +++ b/src/fs/hard_link.rs @@ -22,7 +22,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/metadata.rs b/src/fs/metadata.rs index 2c9e41ecd..cff9d2899 100644 --- a/src/fs/metadata.rs +++ b/src/fs/metadata.rs @@ -27,7 +27,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -68,7 +68,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -88,7 +88,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -108,7 +108,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -126,7 +126,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -144,7 +144,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -167,7 +167,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -190,7 +190,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -213,7 +213,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/mod.rs b/src/fs/mod.rs index 4598ec849..58ae5a231 100644 --- a/src/fs/mod.rs +++ b/src/fs/mod.rs @@ -14,7 +14,7 @@ //! Create a new file and write some bytes to it: //! //! ```no_run -//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +//! # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { //! # //! use async_std::fs::File; //! use async_std::prelude::*; diff --git a/src/fs/open_options.rs b/src/fs/open_options.rs index 252873cd2..32f76c7ea 100644 --- a/src/fs/open_options.rs +++ b/src/fs/open_options.rs @@ -32,7 +32,7 @@ use crate::task::blocking; /// Open a file for reading: /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// @@ -47,7 +47,7 @@ use crate::task::blocking; /// Open a file for both reading and writing, and create it if it doesn't exist yet: /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// @@ -71,7 +71,7 @@ impl OpenOptions { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// @@ -93,7 +93,7 @@ impl OpenOptions { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// @@ -119,7 +119,7 @@ impl OpenOptions { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// @@ -143,7 +143,7 @@ impl OpenOptions { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// @@ -171,7 +171,7 @@ impl OpenOptions { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// @@ -200,7 +200,7 @@ impl OpenOptions { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// @@ -230,7 +230,7 @@ impl OpenOptions { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// @@ -272,7 +272,7 @@ impl OpenOptions { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::OpenOptions; /// diff --git a/src/fs/permissions.rs b/src/fs/permissions.rs index 628dd392b..46356d848 100644 --- a/src/fs/permissions.rs +++ b/src/fs/permissions.rs @@ -18,7 +18,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// @@ -38,7 +38,7 @@ cfg_if! { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/read.rs b/src/fs/read.rs index 6b3560db7..a41d82211 100644 --- a/src/fs/read.rs +++ b/src/fs/read.rs @@ -27,7 +27,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/read_dir.rs b/src/fs/read_dir.rs index 9b4269df2..3c9ae67b1 100644 --- a/src/fs/read_dir.rs +++ b/src/fs/read_dir.rs @@ -29,7 +29,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll}; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// use async_std::prelude::*; diff --git a/src/fs/read_link.rs b/src/fs/read_link.rs index aede99bc8..8b4637194 100644 --- a/src/fs/read_link.rs +++ b/src/fs/read_link.rs @@ -19,7 +19,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/read_to_string.rs b/src/fs/read_to_string.rs index 345f76efa..4bf0e3d6c 100644 --- a/src/fs/read_to_string.rs +++ b/src/fs/read_to_string.rs @@ -28,7 +28,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/remove_dir.rs b/src/fs/remove_dir.rs index a176edc85..69fe20de3 100644 --- a/src/fs/remove_dir.rs +++ b/src/fs/remove_dir.rs @@ -20,7 +20,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/remove_dir_all.rs b/src/fs/remove_dir_all.rs index 9db0c31f5..c9caa159c 100644 --- a/src/fs/remove_dir_all.rs +++ b/src/fs/remove_dir_all.rs @@ -20,7 +20,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/remove_file.rs b/src/fs/remove_file.rs index cc0eeb259..b0a6f64c8 100644 --- a/src/fs/remove_file.rs +++ b/src/fs/remove_file.rs @@ -20,7 +20,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/rename.rs b/src/fs/rename.rs index 72cd227f9..6b746c172 100644 --- a/src/fs/rename.rs +++ b/src/fs/rename.rs @@ -24,7 +24,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/set_permissions.rs b/src/fs/set_permissions.rs index 6fa6306fc..dceb9da90 100644 --- a/src/fs/set_permissions.rs +++ b/src/fs/set_permissions.rs @@ -21,7 +21,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/symlink_metadata.rs b/src/fs/symlink_metadata.rs index 6f1b9d501..b81bc93c1 100644 --- a/src/fs/symlink_metadata.rs +++ b/src/fs/symlink_metadata.rs @@ -25,7 +25,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/fs/write.rs b/src/fs/write.rs index b0d7abcc2..ac0bff541 100644 --- a/src/fs/write.rs +++ b/src/fs/write.rs @@ -23,7 +23,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs; /// diff --git a/src/future/pending.rs b/src/future/pending.rs index aaee70656..f944a4a8e 100644 --- a/src/future/pending.rs +++ b/src/future/pending.rs @@ -9,7 +9,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use std::time::Duration; /// diff --git a/src/future/poll_fn.rs b/src/future/poll_fn.rs index 116e71c69..56c6bad6b 100644 --- a/src/future/poll_fn.rs +++ b/src/future/poll_fn.rs @@ -10,7 +10,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::future; /// use async_std::task::{Context, Poll}; diff --git a/src/future/ready.rs b/src/future/ready.rs index 04f37b87c..534318167 100644 --- a/src/future/ready.rs +++ b/src/future/ready.rs @@ -7,7 +7,7 @@ /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::future; /// diff --git a/src/future/timeout.rs b/src/future/timeout.rs index aa88f6461..c3293acdf 100644 --- a/src/future/timeout.rs +++ b/src/future/timeout.rs @@ -16,7 +16,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ``` -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use std::time::Duration; /// diff --git a/src/io/buf_read/mod.rs b/src/io/buf_read/mod.rs index d34b2ae26..675c7bf98 100644 --- a/src/io/buf_read/mod.rs +++ b/src/io/buf_read/mod.rs @@ -77,7 +77,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::io::BufReader; @@ -94,7 +94,7 @@ extension_trait! { Multiple successful calls to `read_until` append all bytes up to and including to `buf`: ``` - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::io::BufReader; use async_std::prelude::*; @@ -155,7 +155,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::io::BufReader; @@ -197,7 +197,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::io::BufReader; diff --git a/src/io/buf_reader.rs b/src/io/buf_reader.rs index 13cb4cc56..5972475f8 100644 --- a/src/io/buf_reader.rs +++ b/src/io/buf_reader.rs @@ -29,7 +29,7 @@ const DEFAULT_CAPACITY: usize = 8 * 1024; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::io::BufReader; @@ -57,7 +57,7 @@ impl BufReader { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::io::BufReader; @@ -75,7 +75,7 @@ impl BufReader { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::io::BufReader; @@ -106,7 +106,7 @@ impl BufReader { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::io::BufReader; @@ -127,7 +127,7 @@ impl BufReader { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::io::BufReader; @@ -148,7 +148,7 @@ impl BufReader { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::io::BufReader; @@ -169,7 +169,7 @@ impl BufReader { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::fs::File; /// use async_std::io::BufReader; diff --git a/src/io/buf_writer.rs b/src/io/buf_writer.rs index 2b7545a1f..1b252a195 100644 --- a/src/io/buf_writer.rs +++ b/src/io/buf_writer.rs @@ -35,7 +35,7 @@ const DEFAULT_CAPACITY: usize = 8 * 1024; /// Let's write the numbers one through ten to a [`TcpStream`]: /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use async_std::net::TcpStream; /// use async_std::prelude::*; /// @@ -54,7 +54,7 @@ const DEFAULT_CAPACITY: usize = 8 * 1024; /// `BufWriter`: /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use async_std::io::BufWriter; /// use async_std::net::TcpStream; /// use async_std::prelude::*; @@ -93,7 +93,7 @@ impl BufWriter { /// /// ```no_run /// # #![allow(unused_mut)] - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use async_std::io::BufWriter; /// use async_std::net::TcpStream; /// @@ -113,7 +113,7 @@ impl BufWriter { /// /// ```no_run /// # #![allow(unused_mut)] - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use async_std::io::BufWriter; /// use async_std::net::TcpStream; /// @@ -136,7 +136,7 @@ impl BufWriter { /// /// ```no_run /// # #![allow(unused_mut)] - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use async_std::io::BufWriter; /// use async_std::net::TcpStream; /// @@ -158,7 +158,7 @@ impl BufWriter { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use async_std::io::BufWriter; /// use async_std::net::TcpStream; /// @@ -196,7 +196,7 @@ impl BufWriter { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use async_std::io::BufWriter; /// use async_std::net::TcpStream; /// @@ -312,7 +312,7 @@ mod tests { #[test] fn test_buffered_writer() { - task::block_on(async { + thread::spawn_task(async { let inner = Vec::new(); let mut writer = BufWriter::with_capacity(2, inner); @@ -357,7 +357,7 @@ mod tests { #[test] fn test_buffered_writer_inner_into_inner_does_not_flush() { - task::block_on(async { + thread::spawn_task(async { let mut w = BufWriter::with_capacity(3, Vec::new()); w.write(&[0, 1]).await.unwrap(); assert_eq!(*w.get_ref(), []); @@ -368,7 +368,7 @@ mod tests { #[test] fn test_buffered_writer_seek() { - task::block_on(async { + thread::spawn_task(async { let mut w = BufWriter::with_capacity(3, io::Cursor::new(Vec::new())); w.write_all(&[0, 1, 2, 3, 4, 5]).await.unwrap(); w.write_all(&[6, 7]).await.unwrap(); diff --git a/src/io/copy.rs b/src/io/copy.rs index 3840d2af9..1b26fca4a 100644 --- a/src/io/copy.rs +++ b/src/io/copy.rs @@ -30,7 +30,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ``` -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io; /// diff --git a/src/io/cursor.rs b/src/io/cursor.rs index 603b85a14..0d982b1fc 100644 --- a/src/io/cursor.rs +++ b/src/io/cursor.rs @@ -107,7 +107,7 @@ impl Cursor { /// # Examples /// /// ``` - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io::Cursor; /// use async_std::io::prelude::*; diff --git a/src/io/empty.rs b/src/io/empty.rs index d8d768e02..e8bfe2d8c 100644 --- a/src/io/empty.rs +++ b/src/io/empty.rs @@ -9,7 +9,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ```rust -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io; /// use async_std::prelude::*; diff --git a/src/io/mod.rs b/src/io/mod.rs index 7a9428544..e582b2b36 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -9,7 +9,7 @@ //! Read a line from the standard input: //! //! ```no_run -//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +//! # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { //! # //! use async_std::io; //! diff --git a/src/io/read/bytes.rs b/src/io/read/bytes.rs index 422452433..0e9d497d2 100644 --- a/src/io/read/bytes.rs +++ b/src/io/read/bytes.rs @@ -40,7 +40,7 @@ mod tests { #[test] fn test_bytes_basics() -> std::io::Result<()> { - task::block_on(async move { + thread::spawn_task(async move { let raw: Vec = vec![0, 1, 2, 3, 4, 5, 6, 7, 8]; let source: io::Cursor> = io::Cursor::new(raw.clone()); diff --git a/src/io/read/chain.rs b/src/io/read/chain.rs index 09517ccad..5c88378da 100644 --- a/src/io/read/chain.rs +++ b/src/io/read/chain.rs @@ -23,7 +23,7 @@ impl Chain { /// # Examples /// /// ```no_run - /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> async_std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::fs::File; @@ -45,7 +45,7 @@ impl Chain { /// # Examples /// /// ```no_run - /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> async_std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::fs::File; @@ -71,7 +71,7 @@ impl Chain { /// # Examples /// /// ```no_run - /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> async_std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::fs::File; @@ -183,7 +183,7 @@ mod tests { let source1: io::Cursor> = io::Cursor::new(vec![0, 1, 2]); let source2: io::Cursor> = io::Cursor::new(vec![3, 4, 5]); - task::block_on(async move { + thread::spawn_task(async move { let mut buffer = Vec::new(); let mut source = source1.chain(source2); diff --git a/src/io/read/mod.rs b/src/io/read/mod.rs index 6fd95c120..1cc4eeafc 100644 --- a/src/io/read/mod.rs +++ b/src/io/read/mod.rs @@ -89,7 +89,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::prelude::*; @@ -148,7 +148,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::prelude::*; @@ -187,7 +187,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::prelude::*; @@ -242,7 +242,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::prelude::*; @@ -282,7 +282,7 @@ extension_trait! { [`read()`]: tymethod.read ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::io::prelude::*; use async_std::fs::File; @@ -318,7 +318,7 @@ extension_trait! { [file]: ../fs/struct.File.html ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::fs::File; @@ -359,7 +359,7 @@ extension_trait! { [file]: ../fs/struct.File.html ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::fs::File; @@ -392,7 +392,7 @@ extension_trait! { [file]: ../fs/struct.File.html ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::fs::File; @@ -477,7 +477,7 @@ mod tests { #[test] fn test_read_by_ref() -> io::Result<()> { - crate::task::block_on(async { + crate::thread::spawn_task(async { let mut f = io::Cursor::new(vec![0u8, 1, 2, 3, 4, 5, 6, 7, 8]); let mut buffer = Vec::new(); let mut other_buffer = Vec::new(); diff --git a/src/io/read/take.rs b/src/io/read/take.rs index def4e2405..b4d5991e5 100644 --- a/src/io/read/take.rs +++ b/src/io/read/take.rs @@ -30,7 +30,7 @@ impl Take { /// # Examples /// /// ```no_run - /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> async_std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::fs::File; @@ -56,7 +56,7 @@ impl Take { /// # Examples /// /// ```no_run - /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> async_std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::fs::File; @@ -80,7 +80,7 @@ impl Take { /// # Examples /// /// ```no_run - /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> async_std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::fs::File; @@ -104,7 +104,7 @@ impl Take { /// # Examples /// /// ```no_run - /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> async_std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::fs::File; @@ -132,7 +132,7 @@ impl Take { /// # Examples /// /// ```no_run - /// # fn main() -> async_std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> async_std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::fs::File; @@ -224,7 +224,7 @@ mod tests { fn test_take_basics() -> std::io::Result<()> { let source: io::Cursor> = io::Cursor::new(vec![0, 1, 2, 3, 4, 5, 6, 7, 8]); - task::block_on(async move { + thread::spawn_task(async move { let mut buffer = [0u8; 5]; // read at most five bytes diff --git a/src/io/repeat.rs b/src/io/repeat.rs index a82e21be4..f259e5529 100644 --- a/src/io/repeat.rs +++ b/src/io/repeat.rs @@ -11,7 +11,7 @@ use crate::task::{Context, Poll}; /// ## Examples /// /// ```rust -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io; /// use async_std::prelude::*; diff --git a/src/io/seek.rs b/src/io/seek.rs index 274eee736..dd33e0b0d 100644 --- a/src/io/seek.rs +++ b/src/io/seek.rs @@ -54,7 +54,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::io::SeekFrom; diff --git a/src/io/sink.rs b/src/io/sink.rs index faa763c6a..0b97fb100 100644 --- a/src/io/sink.rs +++ b/src/io/sink.rs @@ -9,7 +9,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ```rust -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io; /// use async_std::prelude::*; diff --git a/src/io/stderr.rs b/src/io/stderr.rs index 5706aa2ed..cecbcc6dd 100644 --- a/src/io/stderr.rs +++ b/src/io/stderr.rs @@ -16,7 +16,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll}; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io; /// use async_std::prelude::*; diff --git a/src/io/stdin.rs b/src/io/stdin.rs index 95a77b805..118e95a60 100644 --- a/src/io/stdin.rs +++ b/src/io/stdin.rs @@ -16,7 +16,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll}; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io; /// @@ -89,7 +89,7 @@ impl Stdin { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io; /// diff --git a/src/io/stdout.rs b/src/io/stdout.rs index 7849f1ce9..2c32d9e90 100644 --- a/src/io/stdout.rs +++ b/src/io/stdout.rs @@ -16,7 +16,7 @@ use crate::task::{blocking, Context, JoinHandle, Poll}; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io; /// use async_std::prelude::*; diff --git a/src/io/timeout.rs b/src/io/timeout.rs index 2d1b29e74..de7df942c 100644 --- a/src/io/timeout.rs +++ b/src/io/timeout.rs @@ -13,7 +13,7 @@ use crate::io; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use std::time::Duration; /// diff --git a/src/io/write/mod.rs b/src/io/write/mod.rs index 5e1ecc8be..5b9ebbe88 100644 --- a/src/io/write/mod.rs +++ b/src/io/write/mod.rs @@ -92,7 +92,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::prelude::*; @@ -120,7 +120,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::prelude::*; @@ -174,7 +174,7 @@ extension_trait! { # Examples ```no_run - # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { # use async_std::fs::File; use async_std::prelude::*; diff --git a/src/lib.rs b/src/lib.rs index fa4e946f9..ab3259c29 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,12 +17,12 @@ //! Spawn a task and block the current thread on its result: //! //! ``` -//! use async_std::task; +//! use async_std::thread; //! //! fn main() { -//! task::block_on(async { +//! thread::spawn_task(async { //! println!("Hello, world!"); -//! }) +//! }).join() //! } //! ``` //! @@ -58,6 +58,7 @@ pub mod os; pub mod prelude; pub mod stream; pub mod sync; +pub mod thread; pub mod task; cfg_if! { diff --git a/src/net/addr.rs b/src/net/addr.rs index 64b22cfcd..13018b5ac 100644 --- a/src/net/addr.rs +++ b/src/net/addr.rs @@ -34,7 +34,7 @@ cfg_if! { /// # Examples /// /// ``` -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::ToSocketAddrs; /// diff --git a/src/net/mod.rs b/src/net/mod.rs index b3ae287fa..19b5daece 100644 --- a/src/net/mod.rs +++ b/src/net/mod.rs @@ -13,7 +13,7 @@ //! A simple UDP echo server: //! //! ```no_run -//! # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +//! # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { //! # //! use async_std::net::UdpSocket; //! diff --git a/src/net/tcp/listener.rs b/src/net/tcp/listener.rs index 26e19d79f..5c8ff65ef 100644 --- a/src/net/tcp/listener.rs +++ b/src/net/tcp/listener.rs @@ -31,7 +31,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::io; /// use async_std::net::TcpListener; @@ -65,7 +65,7 @@ impl TcpListener { /// Create a TCP listener bound to 127.0.0.1:0: /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpListener; /// @@ -104,7 +104,7 @@ impl TcpListener { /// ## Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpListener; /// @@ -136,7 +136,7 @@ impl TcpListener { /// ## Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpListener; /// use async_std::prelude::*; @@ -163,7 +163,7 @@ impl TcpListener { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpListener; /// diff --git a/src/net/tcp/stream.rs b/src/net/tcp/stream.rs index 10569434e..26827523a 100644 --- a/src/net/tcp/stream.rs +++ b/src/net/tcp/stream.rs @@ -34,7 +34,7 @@ use crate::task::{Context, Poll}; /// ## Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpStream; /// use async_std::prelude::*; @@ -64,7 +64,7 @@ impl TcpStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpStream; /// @@ -104,7 +104,7 @@ impl TcpStream { /// ## Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpStream; /// @@ -122,7 +122,7 @@ impl TcpStream { /// ## Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpStream; /// @@ -144,7 +144,7 @@ impl TcpStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpStream; /// @@ -167,7 +167,7 @@ impl TcpStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpStream; /// @@ -193,7 +193,7 @@ impl TcpStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpStream; /// @@ -217,7 +217,7 @@ impl TcpStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpStream; /// @@ -243,7 +243,7 @@ impl TcpStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::TcpStream; /// @@ -268,7 +268,7 @@ impl TcpStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use std::net::Shutdown; /// diff --git a/src/net/udp/mod.rs b/src/net/udp/mod.rs index 4588be12c..b5663871d 100644 --- a/src/net/udp/mod.rs +++ b/src/net/udp/mod.rs @@ -30,7 +30,7 @@ use crate::net::ToSocketAddrs; /// ## Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::UdpSocket; /// @@ -60,7 +60,7 @@ impl UdpSocket { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::UdpSocket; /// @@ -98,7 +98,7 @@ impl UdpSocket { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::UdpSocket; /// @@ -118,7 +118,7 @@ impl UdpSocket { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::UdpSocket; /// @@ -162,7 +162,7 @@ impl UdpSocket { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::UdpSocket; /// @@ -195,7 +195,7 @@ impl UdpSocket { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::UdpSocket; /// @@ -230,7 +230,7 @@ impl UdpSocket { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::UdpSocket; /// @@ -260,7 +260,7 @@ impl UdpSocket { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::net::UdpSocket; /// @@ -381,7 +381,7 @@ impl UdpSocket { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use std::net::Ipv4Addr; /// @@ -410,7 +410,7 @@ impl UdpSocket { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use std::net::{Ipv6Addr, SocketAddr}; /// diff --git a/src/os/unix/fs.rs b/src/os/unix/fs.rs index be8932c06..edf2f22d0 100644 --- a/src/os/unix/fs.rs +++ b/src/os/unix/fs.rs @@ -18,7 +18,7 @@ use crate::task::blocking; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::fs::symlink; /// diff --git a/src/os/unix/net/datagram.rs b/src/os/unix/net/datagram.rs index 1f971f7f5..943250584 100644 --- a/src/os/unix/net/datagram.rs +++ b/src/os/unix/net/datagram.rs @@ -29,7 +29,7 @@ use crate::task::blocking; /// ## Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -57,7 +57,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -76,7 +76,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -96,7 +96,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -123,7 +123,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -143,7 +143,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -165,7 +165,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -186,7 +186,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -211,7 +211,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -232,7 +232,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -256,7 +256,7 @@ impl UnixDatagram { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// @@ -280,7 +280,7 @@ impl UnixDatagram { /// ## Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixDatagram; /// use std::net::Shutdown; diff --git a/src/os/unix/net/listener.rs b/src/os/unix/net/listener.rs index eba2fadc3..8c4d824e1 100644 --- a/src/os/unix/net/listener.rs +++ b/src/os/unix/net/listener.rs @@ -33,7 +33,7 @@ use crate::task::{blocking, Context, Poll}; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixListener; /// use async_std::prelude::*; @@ -58,7 +58,7 @@ impl UnixListener { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixListener; /// @@ -82,7 +82,7 @@ impl UnixListener { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixListener; /// @@ -128,7 +128,7 @@ impl UnixListener { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixListener; /// use async_std::prelude::*; @@ -152,7 +152,7 @@ impl UnixListener { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixListener; /// diff --git a/src/os/unix/net/stream.rs b/src/os/unix/net/stream.rs index ae30b5bf9..0cd48eb10 100644 --- a/src/os/unix/net/stream.rs +++ b/src/os/unix/net/stream.rs @@ -24,7 +24,7 @@ use crate::task::{blocking, Context, Poll}; /// # Examples /// /// ```no_run -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixStream; /// use async_std::prelude::*; @@ -47,7 +47,7 @@ impl UnixStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixStream; /// @@ -75,7 +75,7 @@ impl UnixStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixStream; /// @@ -99,7 +99,7 @@ impl UnixStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixStream; /// @@ -117,7 +117,7 @@ impl UnixStream { /// # Examples /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixStream; /// @@ -138,7 +138,7 @@ impl UnixStream { /// [`Shutdown`]: https://doc.rust-lang.org/std/net/enum.Shutdown.html /// /// ```no_run - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// # /// use async_std::os::unix::net::UnixStream; /// use std::net::Shutdown; diff --git a/src/stream/empty.rs b/src/stream/empty.rs index c9deea867..7ada3255a 100644 --- a/src/stream/empty.rs +++ b/src/stream/empty.rs @@ -9,7 +9,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::stream; diff --git a/src/stream/extend.rs b/src/stream/extend.rs index 27efd8b70..088f7a483 100644 --- a/src/stream/extend.rs +++ b/src/stream/extend.rs @@ -14,7 +14,7 @@ use crate::stream::IntoStream; /// ## Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::stream::{self, Extend}; diff --git a/src/stream/from_stream.rs b/src/stream/from_stream.rs index 047dab8fb..702acd873 100644 --- a/src/stream/from_stream.rs +++ b/src/stream/from_stream.rs @@ -14,7 +14,7 @@ use std::pin::Pin; /// Basic usage: /// /// ``` -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use crate::async_std::stream::FromStream; /// use async_std::prelude::*; /// use async_std::stream; @@ -30,7 +30,7 @@ use std::pin::Pin; /// Using `collect` to implicitly use `FromStream` /// ///``` -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use async_std::prelude::*; /// use async_std::stream; /// let five_fives = stream::repeat(5).take(5); @@ -87,7 +87,7 @@ use std::pin::Pin; /// } /// } /// -/// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { +/// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// // Now we can make a new stream... /// let stream = stream::repeat(5).take(5); /// @@ -116,7 +116,7 @@ pub trait FromStream { /// Basic usage: /// /// ``` - /// # fn main() -> std::io::Result<()> { async_std::task::block_on(async { + /// # fn main() -> std::io::Result<()> { async_std::thread::spawn_task(async { /// use crate::async_std::stream::FromStream; /// use async_std::prelude::*; /// use async_std::stream; diff --git a/src/stream/mod.rs b/src/stream/mod.rs index 8aa12a2f1..bef44f697 100644 --- a/src/stream/mod.rs +++ b/src/stream/mod.rs @@ -7,7 +7,7 @@ //! # Examples //! //! ``` -//! # fn main() { async_std::task::block_on(async { +//! # fn main() { async_std::thread::spawn_task(async { //! # //! use async_std::prelude::*; //! use async_std::stream; diff --git a/src/stream/once.rs b/src/stream/once.rs index 133a155c9..b9d116c1a 100644 --- a/src/stream/once.rs +++ b/src/stream/once.rs @@ -8,7 +8,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::stream; diff --git a/src/stream/repeat.rs b/src/stream/repeat.rs index 1a6da4116..7277701a0 100644 --- a/src/stream/repeat.rs +++ b/src/stream/repeat.rs @@ -8,7 +8,7 @@ use crate::task::{Context, Poll}; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::prelude::*; /// use async_std::stream; diff --git a/src/stream/stream/mod.rs b/src/stream/stream/mod.rs index 8849605ca..aed18b315 100644 --- a/src/stream/stream/mod.rs +++ b/src/stream/stream/mod.rs @@ -7,7 +7,7 @@ //! # Examples //! //! ``` -//! # fn main() { async_std::task::block_on(async { +//! # fn main() { async_std::thread::spawn_task(async { //! # //! use async_std::prelude::*; //! use async_std::stream; @@ -132,7 +132,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::pin::Pin; @@ -185,7 +185,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::stream; @@ -211,7 +211,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::stream; @@ -247,7 +247,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -279,7 +279,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -316,7 +316,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -346,7 +346,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -380,7 +380,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -413,7 +413,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::stream; @@ -444,7 +444,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -476,7 +476,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -517,7 +517,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -556,7 +556,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -572,7 +572,7 @@ extension_trait! { Calling `nth()` multiple times: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -590,7 +590,7 @@ extension_trait! { ``` Returning `None` if the stream finished before returning `n` elements: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -633,7 +633,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::stream; @@ -648,7 +648,7 @@ extension_trait! { Empty stream: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::stream; @@ -684,7 +684,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -699,7 +699,7 @@ extension_trait! { Resuming after a first find: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -729,7 +729,7 @@ extension_trait! { Applies function to the elements of stream and returns the first non-none result. ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -762,7 +762,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -793,7 +793,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use std::collections::VecDeque; @@ -841,7 +841,7 @@ extension_trait! { Basic usage: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::stream; @@ -855,7 +855,7 @@ extension_trait! { Empty stream: ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::stream; @@ -900,7 +900,7 @@ extension_trait! { ## Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -941,7 +941,7 @@ extension_trait! { ## Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -971,7 +971,7 @@ extension_trait! { ## Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -999,7 +999,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; use std::sync::mpsc::channel; @@ -1057,7 +1057,7 @@ extension_trait! { ## Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use std::collections::VecDeque; @@ -1104,7 +1104,7 @@ extension_trait! { # Examples ``` - # fn main() { async_std::task::block_on(async { + # fn main() { async_std::thread::spawn_task(async { # use async_std::prelude::*; use async_std::stream; diff --git a/src/sync/barrier.rs b/src/sync/barrier.rs index 43488ee49..a854c7894 100644 --- a/src/sync/barrier.rs +++ b/src/sync/barrier.rs @@ -8,10 +8,10 @@ use crate::sync::Mutex; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::{Arc, Barrier}; -/// use async_std::task; +/// use async_std::thread; /// /// let mut handles = Vec::with_capacity(10); /// let barrier = Arc::new(Barrier::new(10)); @@ -118,10 +118,10 @@ impl Barrier { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::{Arc, Barrier}; - /// use async_std::task; + /// use async_std::thread; /// /// let mut handles = Vec::with_capacity(10); /// let barrier = Arc::new(Barrier::new(10)); @@ -188,7 +188,7 @@ impl BarrierWaitResult { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::Barrier; /// @@ -219,7 +219,7 @@ mod test { // solid. for _ in 0..1_000 { - task::block_on(async move { + thread::spawn_task(async move { const N: usize = 10; let barrier = Arc::new(Barrier::new(N)); diff --git a/src/sync/mod.rs b/src/sync/mod.rs index df1d71ab3..5eab62584 100644 --- a/src/sync/mod.rs +++ b/src/sync/mod.rs @@ -9,7 +9,7 @@ //! Spawn a task that updates an integer protected by a mutex: //! //! ``` -//! # fn main() { async_std::task::block_on(async { +//! # fn main() { async_std::thread::spawn_task(async { //! # //! use std::sync::Arc; //! diff --git a/src/sync/mutex.rs b/src/sync/mutex.rs index 673eb8300..b5356be4d 100644 --- a/src/sync/mutex.rs +++ b/src/sync/mutex.rs @@ -24,12 +24,12 @@ const BLOCKED: usize = 1 << 1; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use std::sync::Arc; /// /// use async_std::sync::Mutex; -/// use async_std::task; +/// use async_std::thread; /// /// let m = Arc::new(Mutex::new(0)); /// let mut tasks = vec![]; @@ -82,12 +82,12 @@ impl Mutex { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use std::sync::Arc; /// /// use async_std::sync::Mutex; - /// use async_std::task; + /// use async_std::thread; /// /// let m1 = Arc::new(Mutex::new(10)); /// let m2 = m1.clone(); @@ -196,12 +196,12 @@ impl Mutex { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use std::sync::Arc; /// /// use async_std::sync::Mutex; - /// use async_std::task; + /// use async_std::thread; /// /// let m1 = Arc::new(Mutex::new(10)); /// let m2 = m1.clone(); @@ -249,7 +249,7 @@ impl Mutex { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::Mutex; /// diff --git a/src/sync/rwlock.rs b/src/sync/rwlock.rs index 55a29fc4a..e3264a7ea 100644 --- a/src/sync/rwlock.rs +++ b/src/sync/rwlock.rs @@ -33,7 +33,7 @@ const READ_COUNT_MASK: usize = !(ONE_READ - 1); /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::RwLock; /// @@ -89,7 +89,7 @@ impl RwLock { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::RwLock; /// @@ -211,7 +211,7 @@ impl RwLock { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::RwLock; /// @@ -253,7 +253,7 @@ impl RwLock { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::RwLock; /// @@ -374,7 +374,7 @@ impl RwLock { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::RwLock; /// @@ -431,7 +431,7 @@ impl RwLock { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # /// use async_std::sync::RwLock; /// diff --git a/src/task/mod.rs b/src/task/mod.rs index a14e5d521..eebaa3d35 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -10,7 +10,7 @@ //! Spawn a task and await its result: //! //! ``` -//! # fn main() { async_std::task::block_on(async { +//! # fn main() { async_std::thread::spawn_task(async { //! # //! use async_std::task; //! @@ -30,7 +30,6 @@ pub use std::task::{Context, Poll, Waker}; #[doc(inline)] pub use async_macros::ready; -pub use block_on::block_on; pub use builder::Builder; pub use pool::spawn; pub use sleep::sleep; @@ -38,16 +37,15 @@ pub use task::{JoinHandle, Task, TaskId}; pub use task_local::{AccessError, LocalKey}; pub use worker::current; -mod block_on; mod builder; mod pool; mod sleep; mod sleepers; -mod task; -mod task_local; -mod worker; pub(crate) mod blocking; +pub(crate) mod task; +pub(crate) mod task_local; +pub(crate) mod worker; /// Spawns a blocking task. /// @@ -55,18 +53,18 @@ pub(crate) mod blocking; /// is useful to prevent long-running synchronous operations from blocking the main futures /// executor. /// -/// See also: [`task::block_on`]. +/// See also: [`thread::spawn_task`]. /// -/// [`task::block_on`]: fn.block_on.html +/// [`thread::spawn_task`]: fn.block_on.html /// /// # Examples /// /// Basic usage: /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # -/// use async_std::task; +/// use async_std::thread; /// /// task::blocking(async { /// println!("long-running task here"); diff --git a/src/task/pool.rs b/src/task/pool.rs index 49fbfe490..9400ab714 100644 --- a/src/task/pool.rs +++ b/src/task/pool.rs @@ -22,9 +22,9 @@ use crate::utils::abort_on_panic; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # -/// use async_std::task; +/// use async_std::thread; /// /// let handle = task::spawn(async { /// 1 + 2 diff --git a/src/task/sleep.rs b/src/task/sleep.rs index 9db09ffe7..671b92219 100644 --- a/src/task/sleep.rs +++ b/src/task/sleep.rs @@ -14,11 +14,11 @@ use crate::io; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # /// use std::time::Duration; /// -/// use async_std::task; +/// use async_std::thread; /// /// task::sleep(Duration::from_secs(1)).await; /// # diff --git a/src/task/task.rs b/src/task/task.rs index ba808aa22..1341c5b9d 100644 --- a/src/task/task.rs +++ b/src/task/task.rs @@ -68,9 +68,9 @@ impl JoinHandle { /// # Examples /// /// ``` - /// # fn main() { async_std::task::block_on(async { + /// # fn main() { async_std::thread::spawn_task(async { /// # - /// use async_std::task; + /// use async_std::thread; /// /// let handle = task::spawn(async { /// 1 + 2 @@ -101,9 +101,9 @@ impl Future for JoinHandle { /// /// ``` /// # -/// use async_std::task; +/// use async_std::thread; /// -/// task::block_on(async { +/// thread::spawn_task(async { /// println!("id = {:?}", task::current().id()); /// }) /// ``` diff --git a/src/task/task_local.rs b/src/task/task_local.rs index 8347e34b6..7fa45bc69 100644 --- a/src/task/task_local.rs +++ b/src/task/task_local.rs @@ -25,14 +25,14 @@ use crate::utils::abort_on_panic; /// # /// use std::cell::Cell; /// -/// use async_std::task; +/// use async_std::thread; /// use async_std::prelude::*; /// /// task_local! { /// static VAL: Cell = Cell::new(5); /// } /// -/// task::block_on(async { +/// thread::spawn_task(async { /// let v = VAL.with(|c| c.get()); /// assert_eq!(v, 5); /// }); @@ -96,14 +96,14 @@ impl LocalKey { /// # /// use std::cell::Cell; /// - /// use async_std::task; + /// use async_std::thread; /// use async_std::prelude::*; /// /// task_local! { /// static NUMBER: Cell = Cell::new(5); /// } /// - /// task::block_on(async { + /// thread::spawn_task(async { /// let v = NUMBER.with(|c| c.get()); /// assert_eq!(v, 5); /// }); @@ -135,14 +135,14 @@ impl LocalKey { /// # /// use std::cell::Cell; /// - /// use async_std::task; + /// use async_std::thread; /// use async_std::prelude::*; /// /// task_local! { /// static VAL: Cell = Cell::new(5); /// } /// - /// task::block_on(async { + /// thread::spawn_task(async { /// let v = VAL.try_with(|c| c.get()); /// assert_eq!(v, Ok(5)); /// }); diff --git a/src/task/worker.rs b/src/task/worker.rs index fc2a6e7ef..4493ca3be 100644 --- a/src/task/worker.rs +++ b/src/task/worker.rs @@ -22,9 +22,9 @@ use crate::utils::abort_on_panic; /// # Examples /// /// ``` -/// # fn main() { async_std::task::block_on(async { +/// # fn main() { async_std::thread::spawn_task(async { /// # -/// use async_std::task; +/// use async_std::thread; /// /// println!("The name of this task is {:?}", task::current().name()); /// # diff --git a/src/thread/mod.rs b/src/thread/mod.rs new file mode 100644 index 000000000..fb0dfaa9d --- /dev/null +++ b/src/thread/mod.rs @@ -0,0 +1,8 @@ +//! Native threads. + +mod spawn_task; + +#[doc(inline)] +pub use std::thread::{spawn, JoinHandle}; + +pub use spawn_task::spawn_task; diff --git a/src/task/block_on.rs b/src/thread/spawn_task.rs similarity index 93% rename from src/task/block_on.rs rename to src/thread/spawn_task.rs index 115040024..5ae422192 100644 --- a/src/task/block_on.rs +++ b/src/thread/spawn_task.rs @@ -6,9 +6,9 @@ use std::sync::Arc; use std::task::{RawWaker, RawWakerVTable}; use std::thread::{self, Thread}; -use super::task; -use super::task_local; -use super::worker; +use crate::task::task; +use crate::task::task_local; +use crate::task::worker; use crate::future::Future; use crate::task::{Context, Poll, Waker}; @@ -29,15 +29,15 @@ use kv_log_macro::trace; /// # Examples /// /// ```no_run -/// use async_std::task; +/// use async_std::thread; /// /// fn main() { -/// task::block_on(async { +/// thread::spawn_task(async { /// println!("Hello, world!"); /// }) /// } /// ``` -pub fn block_on(future: F) -> T +pub fn spawn_task(future: F) -> T where F: Future, { @@ -60,11 +60,11 @@ where // Create a tag for the task. let tag = task::Tag::new(None); - // Log this `block_on` operation. + // Log this `spawn_task` operation. let child_id = tag.task_id().as_u64(); let parent_id = worker::get_task(|t| t.id().as_u64()).unwrap_or(0); - trace!("block_on", { + trace!("spawn_task", { parent_id: parent_id, child_id: child_id, }); @@ -74,7 +74,7 @@ where let future = async move { future.await; - trace!("block_on completed", { + trace!("spawn_task completed", { parent_id: parent_id, child_id: child_id, }); diff --git a/tests/addr.rs b/tests/addr.rs index aada557c3..a941c8376 100644 --- a/tests/addr.rs +++ b/tests/addr.rs @@ -8,7 +8,7 @@ where A: ToSocketAddrs, A::Iter: Send, { - let socket_addrs = task::block_on(a.to_socket_addrs()); + let socket_addrs = thread::spawn_task(a.to_socket_addrs()); match socket_addrs { Ok(a) => Ok(a.collect()), Err(e) => Err(e.to_string()), diff --git a/tests/block_on.rs b/tests/block_on.rs index c422d0630..efd55adc4 100644 --- a/tests/block_on.rs +++ b/tests/block_on.rs @@ -2,14 +2,14 @@ use async_std::task; #[test] fn smoke() { - let res = task::block_on(async { 1 + 2 }); + let res = thread::spawn_task(async { 1 + 2 }); assert_eq!(res, 3); } #[test] #[should_panic = "boom"] fn panic() { - task::block_on(async { + thread::spawn_task(async { // This panic should get propagated into the parent thread. panic!("boom"); }); diff --git a/tests/io_timeout.rs b/tests/io_timeout.rs index 85a17ab75..b0f1c2047 100644 --- a/tests/io_timeout.rs +++ b/tests/io_timeout.rs @@ -6,7 +6,7 @@ use async_std::task; #[test] #[should_panic(expected = "timed out")] fn io_timeout_timedout() { - task::block_on(async { + thread::spawn_task(async { io::timeout(Duration::from_secs(1), async { let stdin = io::stdin(); let mut line = String::new(); @@ -21,7 +21,7 @@ fn io_timeout_timedout() { #[test] #[should_panic(expected = "My custom error")] fn io_timeout_future_err() { - task::block_on(async { + thread::spawn_task(async { io::timeout(Duration::from_secs(1), async { Err::<(), io::Error>(io::Error::new(io::ErrorKind::Other, "My custom error")) }) @@ -32,7 +32,7 @@ fn io_timeout_future_err() { #[test] fn io_timeout_future_ok() { - task::block_on(async { + thread::spawn_task(async { io::timeout(Duration::from_secs(1), async { Ok(()) }) .await .unwrap(); // We shouldn't panic at all diff --git a/tests/mutex.rs b/tests/mutex.rs index fd1c07b38..dded8326e 100644 --- a/tests/mutex.rs +++ b/tests/mutex.rs @@ -7,7 +7,7 @@ use futures::channel::mpsc; #[test] fn smoke() { - task::block_on(async { + thread::spawn_task(async { let m = Mutex::new(()); drop(m.lock().await); drop(m.lock().await); @@ -35,7 +35,7 @@ fn get_mut() { #[test] fn contention() { - task::block_on(async { + thread::spawn_task(async { let (tx, mut rx) = mpsc::unbounded(); let tx = Arc::new(tx); diff --git a/tests/rwlock.rs b/tests/rwlock.rs index ff25e862d..4bfa3cee8 100644 --- a/tests/rwlock.rs +++ b/tests/rwlock.rs @@ -36,7 +36,7 @@ pub fn random(n: u32) -> u32 { #[test] fn smoke() { - task::block_on(async { + thread::spawn_task(async { let lock = RwLock::new(()); drop(lock.read().await); drop(lock.write().await); @@ -47,7 +47,7 @@ fn smoke() { #[test] fn try_write() { - task::block_on(async { + thread::spawn_task(async { let lock = RwLock::new(0isize); let read_guard = lock.read().await; assert!(lock.try_write().is_none()); @@ -116,7 +116,7 @@ fn contention() { }); } - task::block_on(async { + thread::spawn_task(async { for _ in 0..N { rx.next().await.unwrap(); } @@ -170,7 +170,7 @@ fn writer_and_readers() { })); } - task::block_on(async { + thread::spawn_task(async { // Wait for readers to pass their asserts. for r in readers { r.await; diff --git a/tests/task_local.rs b/tests/task_local.rs index 813185c84..7342eab61 100644 --- a/tests/task_local.rs +++ b/tests/task_local.rs @@ -26,7 +26,7 @@ fn drop_local() { let task = handle.task().clone(); // Wait for the task to finish and make sure its task-local has been dropped. - task::block_on(async { + thread::spawn_task(async { handle.await; assert!(DROP_LOCAL.load(Ordering::SeqCst)); drop(task); diff --git a/tests/tcp.rs b/tests/tcp.rs index c8281d713..a36325b76 100644 --- a/tests/tcp.rs +++ b/tests/tcp.rs @@ -12,7 +12,7 @@ const THE_WINTERS_TALE: &[u8] = b" #[test] fn connect() -> io::Result<()> { - task::block_on(async { + thread::spawn_task(async { let listener = TcpListener::bind("127.0.0.1:0").await?; let addr = listener.local_addr()?; let t = task::spawn(async move { listener.accept().await }); @@ -29,7 +29,7 @@ fn connect() -> io::Result<()> { #[test] fn incoming_read() -> io::Result<()> { - task::block_on(async { + thread::spawn_task(async { let listener = TcpListener::bind("127.0.0.1:0").await?; let addr = listener.local_addr()?; diff --git a/tests/udp.rs b/tests/udp.rs index 319dc74ae..c0c880acf 100644 --- a/tests/udp.rs +++ b/tests/udp.rs @@ -11,7 +11,7 @@ const THE_MERCHANT_OF_VENICE: &[u8] = b" #[test] fn send_recv() -> io::Result<()> { - task::block_on(async { + thread::spawn_task(async { let socket1 = UdpSocket::bind("127.0.0.1:0").await?; let socket2 = UdpSocket::bind("127.0.0.1:0").await?; diff --git a/tests/uds.rs b/tests/uds.rs index 3ab4d6ba4..d9794efc5 100644 --- a/tests/uds.rs +++ b/tests/uds.rs @@ -16,7 +16,7 @@ const JULIUS_CAESAR: &[u8] = b" #[test] fn send_recv() -> io::Result<()> { - task::block_on(async { + thread::spawn_task(async { let (socket1, socket2) = UnixDatagram::pair().unwrap(); socket1.send(JULIUS_CAESAR).await?; @@ -31,7 +31,7 @@ fn send_recv() -> io::Result<()> { #[test] fn into_raw_fd() -> io::Result<()> { use async_std::os::unix::io::{FromRawFd, IntoRawFd}; - task::block_on(async { + thread::spawn_task(async { let (socket1, socket2) = UnixDatagram::pair().unwrap(); socket1.send(JULIUS_CAESAR).await?; @@ -56,14 +56,14 @@ fn socket_ping_pong() { let iter_cnt = 16; let listener = - task::block_on(async { UnixListener::bind(&sock_path).await.expect("Socket bind") }); + thread::spawn_task(async { UnixListener::bind(&sock_path).await.expect("Socket bind") }); let server_handle = std::thread::spawn(move || { - task::block_on(async { ping_pong_server(listener, iter_cnt).await }).unwrap() + thread::spawn_task(async { ping_pong_server(listener, iter_cnt).await }).unwrap() }); let client_handle = std::thread::spawn(move || { - task::block_on(async { ping_pong_client(&sock_path, iter_cnt).await }).unwrap() + thread::spawn_task(async { ping_pong_client(&sock_path, iter_cnt).await }).unwrap() }); client_handle.join().unwrap(); From 653104f1a7cb2325b16e74a94b1f21a0193d419b Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 10 Oct 2019 02:17:10 +0200 Subject: [PATCH 2/4] thread::spawn_task Signed-off-by: Yoshua Wuyts --- src/thread/spawn_task.rs | 107 ++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 53 deletions(-) diff --git a/src/thread/spawn_task.rs b/src/thread/spawn_task.rs index 5ae422192..51ca8359a 100644 --- a/src/thread/spawn_task.rs +++ b/src/thread/spawn_task.rs @@ -21,7 +21,7 @@ use kv_log_macro::trace; /// /// See also: [`task::blocking`]. /// -/// [`task::blocking`]: fn.blocking.html +/// [`task::blocking`]: ../task/fn.blocking.html /// /// [spawning]: https://doc.rust-lang.org/std/thread/fn.spawn.html /// [joining]: https://doc.rust-lang.org/std/thread/struct.JoinHandle.html#method.join @@ -40,64 +40,65 @@ use kv_log_macro::trace; pub fn spawn_task(future: F) -> T where F: Future, + T: Send, { - unsafe { - // A place on the stack where the result will be stored. - let out = &mut UnsafeCell::new(None); - - // Wrap the future into one that stores the result into `out`. - let future = { - let out = out.get(); - - async move { - let future = CatchUnwindFuture { - future: AssertUnwindSafe(future), - }; - *out = Some(future.await); - } - }; - - // Create a tag for the task. - let tag = task::Tag::new(None); - - // Log this `spawn_task` operation. - let child_id = tag.task_id().as_u64(); - let parent_id = worker::get_task(|t| t.id().as_u64()).unwrap_or(0); - - trace!("spawn_task", { - parent_id: parent_id, - child_id: child_id, - }); - - // Wrap the future into one that drops task-local variables on exit. - let future = task_local::add_finalizer(future); - - let future = async move { - future.await; - trace!("spawn_task completed", { + unsafe { + // A place on the stack where the result will be stored. + let out = &mut UnsafeCell::new(None); + + // Wrap the future into one that stores the result into `out`. + let future = { + let out = out.get(); + + async move { + let future = CatchUnwindFuture { + future: AssertUnwindSafe(future), + }; + *out = Some(future.await); + } + }; + + // Create a tag for the task. + let tag = task::Tag::new(None); + + // Log this `spawn_task` operation. + let child_id = tag.task_id().as_u64(); + let parent_id = worker::get_task(|t| t.id().as_u64()).unwrap_or(0); + + trace!("spawn_task", { parent_id: parent_id, child_id: child_id, }); - }; - - // Pin the future onto the stack. - pin_utils::pin_mut!(future); - - // Transmute the future into one that is futurestatic. - let future = mem::transmute::< - Pin<&'_ mut dyn Future>, - Pin<&'static mut dyn Future>, - >(future); - // Block on the future and and wait for it to complete. - worker::set_tag(&tag, || block(future)); - - // Take out the result. - match (*out.get()).take().unwrap() { - Ok(v) => v, - Err(err) => panic::resume_unwind(err), + // Wrap the future into one that drops task-local variables on exit. + let future = task_local::add_finalizer(future); + + let future = async move { + future.await; + trace!("spawn_task completed", { + parent_id: parent_id, + child_id: child_id, + }); + }; + + // Pin the future onto the stack. + pin_utils::pin_mut!(future); + + // Transmute the future into one that is futurestatic. + let future = mem::transmute::< + Pin<&'_ mut dyn Future>, + Pin<&'static mut dyn Future>, + >(future); + + // Block on the future and and wait for it to complete. + worker::set_tag(&tag, || block(future)); + + // Take out the result. + match (*out.get()).take().unwrap() { + Ok(v) => v, + Err(err) => panic::resume_unwind(err), + } } - } } struct CatchUnwindFuture { From 85f6980d4e39452c4740c14422303586123c4184 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 10 Oct 2019 02:31:09 +0200 Subject: [PATCH 3/4] spawn_task Signed-off-by: Yoshua Wuyts --- examples/hello-world.rs | 2 +- examples/line-count.rs | 2 +- examples/list-dir.rs | 2 +- examples/logging.rs | 2 +- examples/print-file.rs | 2 +- examples/stdin-echo.rs | 3 +- examples/stdin-timeout.rs | 2 +- examples/surf-web.rs | 2 +- examples/task-local.rs | 2 +- examples/task-name.rs | 2 +- examples/tcp-client.rs | 2 +- examples/tcp-echo.rs | 2 +- examples/udp-client.rs | 2 +- examples/udp-echo.rs | 2 +- src/io/read/bytes.rs | 1 + src/lib.rs | 4 +- src/thread/mod.rs | 10 ++-- src/thread/spawn_task.rs | 106 +++++++++++++++++++------------------- tests/mutex.rs | 1 + tests/rwlock.rs | 1 + 20 files changed, 80 insertions(+), 72 deletions(-) diff --git a/examples/hello-world.rs b/examples/hello-world.rs index 0f1e0df52..259f56438 100644 --- a/examples/hello-world.rs +++ b/examples/hello-world.rs @@ -1,6 +1,6 @@ //! Spawns a task that says hello. -use async_std::task; +use async_std::thread; async fn say_hi() { println!("Hello, world!"); diff --git a/examples/line-count.rs b/examples/line-count.rs index a33ea1520..dafba774b 100644 --- a/examples/line-count.rs +++ b/examples/line-count.rs @@ -5,7 +5,7 @@ use std::env::args; use async_std::fs::File; use async_std::io::{self, BufReader}; use async_std::prelude::*; -use async_std::task; +use async_std::thread; fn main() -> io::Result<()> { let path = args().nth(1).expect("missing path argument"); diff --git a/examples/list-dir.rs b/examples/list-dir.rs index 056860d19..afacdbc65 100644 --- a/examples/list-dir.rs +++ b/examples/list-dir.rs @@ -5,7 +5,7 @@ use std::env::args; use async_std::fs; use async_std::io; use async_std::prelude::*; -use async_std::task; +use async_std::thread; fn main() -> io::Result<()> { let path = args().nth(1).expect("missing path argument"); diff --git a/examples/logging.rs b/examples/logging.rs index dfd8c145d..ae0ebfebc 100644 --- a/examples/logging.rs +++ b/examples/logging.rs @@ -1,6 +1,6 @@ //! Prints the runtime's execution log on the standard output. -use async_std::task; +use async_std::thread; fn main() { femme::start(log::LevelFilter::Trace).unwrap(); diff --git a/examples/print-file.rs b/examples/print-file.rs index 8eb121ec4..c2febd716 100644 --- a/examples/print-file.rs +++ b/examples/print-file.rs @@ -5,7 +5,7 @@ use std::env::args; use async_std::fs::File; use async_std::io; use async_std::prelude::*; -use async_std::task; +use async_std::thread; const LEN: usize = 16 * 1024; // 16 Kb diff --git a/examples/stdin-echo.rs b/examples/stdin-echo.rs index c671c57cf..e116b3ed4 100644 --- a/examples/stdin-echo.rs +++ b/examples/stdin-echo.rs @@ -2,7 +2,8 @@ use async_std::io; use async_std::prelude::*; -use async_std::task; +use async_std::thread; +use async_std::thread; fn main() -> io::Result<()> { thread::spawn_task(async { diff --git a/examples/stdin-timeout.rs b/examples/stdin-timeout.rs index b061b667a..5e5b2f25e 100644 --- a/examples/stdin-timeout.rs +++ b/examples/stdin-timeout.rs @@ -3,7 +3,7 @@ use std::time::Duration; use async_std::io; -use async_std::task; +use async_std::thread; fn main() -> io::Result<()> { // This async scope times out after 5 seconds. diff --git a/examples/surf-web.rs b/examples/surf-web.rs index b4666e862..863e5f174 100644 --- a/examples/surf-web.rs +++ b/examples/surf-web.rs @@ -1,7 +1,7 @@ /* TODO: Once the next version of surf released, re-enable this example. //! Sends an HTTP request to the Rust website. -use async_std::task; +use async_std::thread; fn main() -> Result<(), surf::Exception> { thread::spawn_task(async { diff --git a/examples/task-local.rs b/examples/task-local.rs index 484751b39..18e5809fb 100644 --- a/examples/task-local.rs +++ b/examples/task-local.rs @@ -3,7 +3,7 @@ use std::cell::Cell; use async_std::prelude::*; -use async_std::task; +use async_std::thread; task_local! { static VAR: Cell = Cell::new(1); diff --git a/examples/task-name.rs b/examples/task-name.rs index 19a3618a1..c026f9fd6 100644 --- a/examples/task-name.rs +++ b/examples/task-name.rs @@ -1,6 +1,6 @@ //! Spawns a named task that prints its name. -use async_std::task; +use async_std::thread; async fn print_name() { println!("My name is {:?}", task::current().name()); diff --git a/examples/tcp-client.rs b/examples/tcp-client.rs index 37291ec54..4903cfb53 100644 --- a/examples/tcp-client.rs +++ b/examples/tcp-client.rs @@ -15,7 +15,7 @@ use async_std::io; use async_std::net::TcpStream; use async_std::prelude::*; -use async_std::task; +use async_std::thread; fn main() -> io::Result<()> { thread::spawn_task(async { diff --git a/examples/tcp-echo.rs b/examples/tcp-echo.rs index fe90fae1b..95e3da984 100644 --- a/examples/tcp-echo.rs +++ b/examples/tcp-echo.rs @@ -9,7 +9,7 @@ use async_std::io; use async_std::net::{TcpListener, TcpStream}; use async_std::prelude::*; -use async_std::task; +use async_std::thread; async fn process(stream: TcpStream) -> io::Result<()> { println!("Accepted from: {}", stream.peer_addr()?); diff --git a/examples/udp-client.rs b/examples/udp-client.rs index f56ddbdf4..7efd7dec4 100644 --- a/examples/udp-client.rs +++ b/examples/udp-client.rs @@ -14,7 +14,7 @@ use async_std::io; use async_std::net::UdpSocket; -use async_std::task; +use async_std::thread; fn main() -> io::Result<()> { thread::spawn_task(async { diff --git a/examples/udp-echo.rs b/examples/udp-echo.rs index ae397caf7..4b75d9b3a 100644 --- a/examples/udp-echo.rs +++ b/examples/udp-echo.rs @@ -8,7 +8,7 @@ use async_std::io; use async_std::net::UdpSocket; -use async_std::task; +use async_std::thread; fn main() -> io::Result<()> { thread::spawn_task(async { diff --git a/src/io/read/bytes.rs b/src/io/read/bytes.rs index 0e9d497d2..4c400cace 100644 --- a/src/io/read/bytes.rs +++ b/src/io/read/bytes.rs @@ -36,6 +36,7 @@ impl Stream for Bytes { mod tests { use crate::io; use crate::prelude::*; + use crate::thread; use crate::task; #[test] diff --git a/src/lib.rs b/src/lib.rs index ab3259c29..962fca7d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ //! fn main() { //! thread::spawn_task(async { //! println!("Hello, world!"); -//! }).join() +//! }) //! } //! ``` //! @@ -58,8 +58,8 @@ pub mod os; pub mod prelude; pub mod stream; pub mod sync; -pub mod thread; pub mod task; +pub mod thread; cfg_if! { if #[cfg(any(feature = "unstable", feature = "docs"))] { diff --git a/src/thread/mod.rs b/src/thread/mod.rs index fb0dfaa9d..7c8fdbc11 100644 --- a/src/thread/mod.rs +++ b/src/thread/mod.rs @@ -1,8 +1,12 @@ //! Native threads. -mod spawn_task; - #[doc(inline)] -pub use std::thread::{spawn, JoinHandle}; +pub use std::thread::Result; +#[doc(inline)] +pub use std::thread::{current, panicking, park, park_timeout, sleep, spawn, yield_now}; +#[doc(inline)] +pub use std::thread::{AccessError, Builder, JoinHandle, LocalKey, Thread, ThreadId}; pub use spawn_task::spawn_task; + +mod spawn_task; diff --git a/src/thread/spawn_task.rs b/src/thread/spawn_task.rs index 51ca8359a..8e525dd7c 100644 --- a/src/thread/spawn_task.rs +++ b/src/thread/spawn_task.rs @@ -6,10 +6,10 @@ use std::sync::Arc; use std::task::{RawWaker, RawWakerVTable}; use std::thread::{self, Thread}; +use crate::future::Future; use crate::task::task; use crate::task::task_local; use crate::task::worker; -use crate::future::Future; use crate::task::{Context, Poll, Waker}; use kv_log_macro::trace; @@ -42,63 +42,63 @@ where F: Future, T: Send, { - unsafe { - // A place on the stack where the result will be stored. - let out = &mut UnsafeCell::new(None); - - // Wrap the future into one that stores the result into `out`. - let future = { - let out = out.get(); - - async move { - let future = CatchUnwindFuture { - future: AssertUnwindSafe(future), - }; - *out = Some(future.await); - } - }; - - // Create a tag for the task. - let tag = task::Tag::new(None); - - // Log this `spawn_task` operation. - let child_id = tag.task_id().as_u64(); - let parent_id = worker::get_task(|t| t.id().as_u64()).unwrap_or(0); - - trace!("spawn_task", { + unsafe { + // A place on the stack where the result will be stored. + let out = &mut UnsafeCell::new(None); + + // Wrap the future into one that stores the result into `out`. + let future = { + let out = out.get(); + + async move { + let future = CatchUnwindFuture { + future: AssertUnwindSafe(future), + }; + *out = Some(future.await); + } + }; + + // Create a tag for the task. + let tag = task::Tag::new(None); + + // Log this `spawn_task` operation. + let child_id = tag.task_id().as_u64(); + let parent_id = worker::get_task(|t| t.id().as_u64()).unwrap_or(0); + + trace!("spawn_task", { + parent_id: parent_id, + child_id: child_id, + }); + + // Wrap the future into one that drops task-local variables on exit. + let future = task_local::add_finalizer(future); + + let future = async move { + future.await; + trace!("spawn_task completed", { parent_id: parent_id, child_id: child_id, }); + }; + + // Pin the future onto the stack. + pin_utils::pin_mut!(future); - // Wrap the future into one that drops task-local variables on exit. - let future = task_local::add_finalizer(future); - - let future = async move { - future.await; - trace!("spawn_task completed", { - parent_id: parent_id, - child_id: child_id, - }); - }; - - // Pin the future onto the stack. - pin_utils::pin_mut!(future); - - // Transmute the future into one that is futurestatic. - let future = mem::transmute::< - Pin<&'_ mut dyn Future>, - Pin<&'static mut dyn Future>, - >(future); - - // Block on the future and and wait for it to complete. - worker::set_tag(&tag, || block(future)); - - // Take out the result. - match (*out.get()).take().unwrap() { - Ok(v) => v, - Err(err) => panic::resume_unwind(err), - } + // Transmute the future into one that is futurestatic. + let future = mem::transmute::< + Pin<&'_ mut dyn Future>, + Pin<&'static mut dyn Future>, + >(future); + + // Block on the future and and wait for it to complete. + worker::set_tag(&tag, || block(future)); + + // Take out the result. + match (*out.get()).take().unwrap() { + Ok(v) => v, + Err(err) => panic::resume_unwind(err), } + } } struct CatchUnwindFuture { diff --git a/tests/mutex.rs b/tests/mutex.rs index dded8326e..1b67aedb3 100644 --- a/tests/mutex.rs +++ b/tests/mutex.rs @@ -3,6 +3,7 @@ use std::sync::Arc; use async_std::prelude::*; use async_std::sync::Mutex; use async_std::task; +use async_std::thread; use futures::channel::mpsc; #[test] diff --git a/tests/rwlock.rs b/tests/rwlock.rs index 4bfa3cee8..3e4cf097f 100644 --- a/tests/rwlock.rs +++ b/tests/rwlock.rs @@ -8,6 +8,7 @@ use std::task::{Context, Poll}; use async_std::prelude::*; use async_std::sync::RwLock; use async_std::task; +use async_std::thread; use futures::channel::mpsc; /// Generates a random number in `0..n`. From a753e9dd443629ac978d56de01f02ec39201afc3 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Thu, 10 Oct 2019 02:37:39 +0200 Subject: [PATCH 4/4] use spawn_task everywhere Signed-off-by: Yoshua Wuyts --- examples/a-chat/client.rs | 2 +- examples/a-chat/server.rs | 1 + examples/logging.rs | 1 + examples/socket-timeouts.rs | 3 ++- examples/stdin-echo.rs | 1 - examples/task-name.rs | 1 + examples/tcp-echo.rs | 1 + src/io/buf_writer.rs | 2 +- src/io/read/bytes.rs | 1 - src/io/read/chain.rs | 2 +- src/io/read/take.rs | 2 +- src/sync/barrier.rs | 2 +- tests/addr.rs | 2 +- tests/io_timeout.rs | 2 +- tests/{block_on.rs => spawn_task.rs} | 2 +- tests/task_local.rs | 1 + tests/tcp.rs | 1 + tests/udp.rs | 2 +- tests/uds.rs | 2 +- 19 files changed, 18 insertions(+), 13 deletions(-) rename tests/{block_on.rs => spawn_task.rs} (92%) diff --git a/examples/a-chat/client.rs b/examples/a-chat/client.rs index d103dc15d..fddfd1b3b 100644 --- a/examples/a-chat/client.rs +++ b/examples/a-chat/client.rs @@ -5,7 +5,7 @@ use async_std::{ io::{stdin, BufReader}, net::{TcpStream, ToSocketAddrs}, prelude::*, - task, + thread, }; type Result = std::result::Result>; diff --git a/examples/a-chat/server.rs b/examples/a-chat/server.rs index b5cafe2de..e5669219e 100644 --- a/examples/a-chat/server.rs +++ b/examples/a-chat/server.rs @@ -10,6 +10,7 @@ use async_std::{ net::{TcpListener, TcpStream, ToSocketAddrs}, prelude::*, task, + thread, }; type Result = std::result::Result>; diff --git a/examples/logging.rs b/examples/logging.rs index ae0ebfebc..825751825 100644 --- a/examples/logging.rs +++ b/examples/logging.rs @@ -1,6 +1,7 @@ //! Prints the runtime's execution log on the standard output. use async_std::thread; +use async_std::task; fn main() { femme::start(log::LevelFilter::Trace).unwrap(); diff --git a/examples/socket-timeouts.rs b/examples/socket-timeouts.rs index 8412c158a..d1cdf28a8 100644 --- a/examples/socket-timeouts.rs +++ b/examples/socket-timeouts.rs @@ -2,7 +2,8 @@ use std::time::Duration; -use async_std::{io, net::TcpStream, prelude::*, task}; +use async_std::{io, net::TcpStream, prelude::*}; +use async_std::thread; async fn get() -> io::Result> { let mut stream = TcpStream::connect("example.com:80").await?; diff --git a/examples/stdin-echo.rs b/examples/stdin-echo.rs index e116b3ed4..0bceedd20 100644 --- a/examples/stdin-echo.rs +++ b/examples/stdin-echo.rs @@ -3,7 +3,6 @@ use async_std::io; use async_std::prelude::*; use async_std::thread; -use async_std::thread; fn main() -> io::Result<()> { thread::spawn_task(async { diff --git a/examples/task-name.rs b/examples/task-name.rs index c026f9fd6..b3bcb51aa 100644 --- a/examples/task-name.rs +++ b/examples/task-name.rs @@ -1,5 +1,6 @@ //! Spawns a named task that prints its name. +use async_std::task; use async_std::thread; async fn print_name() { diff --git a/examples/tcp-echo.rs b/examples/tcp-echo.rs index 95e3da984..f9fa3ee03 100644 --- a/examples/tcp-echo.rs +++ b/examples/tcp-echo.rs @@ -10,6 +10,7 @@ use async_std::io; use async_std::net::{TcpListener, TcpStream}; use async_std::prelude::*; use async_std::thread; +use async_std::task; async fn process(stream: TcpStream) -> io::Result<()> { println!("Accepted from: {}", stream.peer_addr()?); diff --git a/src/io/buf_writer.rs b/src/io/buf_writer.rs index 1b252a195..719229946 100644 --- a/src/io/buf_writer.rs +++ b/src/io/buf_writer.rs @@ -308,7 +308,7 @@ mod tests { use super::BufWriter; use crate::io::{self, SeekFrom}; use crate::prelude::*; - use crate::task; + use crate::thread; #[test] fn test_buffered_writer() { diff --git a/src/io/read/bytes.rs b/src/io/read/bytes.rs index 4c400cace..9d28a9398 100644 --- a/src/io/read/bytes.rs +++ b/src/io/read/bytes.rs @@ -37,7 +37,6 @@ mod tests { use crate::io; use crate::prelude::*; use crate::thread; - use crate::task; #[test] fn test_bytes_basics() -> std::io::Result<()> { diff --git a/src/io/read/chain.rs b/src/io/read/chain.rs index 5c88378da..d35490de1 100644 --- a/src/io/read/chain.rs +++ b/src/io/read/chain.rs @@ -176,7 +176,7 @@ impl BufRead for Chain { mod tests { use crate::io; use crate::prelude::*; - use crate::task; + use crate::thread; #[test] fn test_chain_basics() -> std::io::Result<()> { diff --git a/src/io/read/take.rs b/src/io/read/take.rs index b4d5991e5..26d0e297b 100644 --- a/src/io/read/take.rs +++ b/src/io/read/take.rs @@ -218,7 +218,7 @@ impl BufRead for Take { mod tests { use crate::io; use crate::prelude::*; - use crate::task; + use crate::thread; #[test] fn test_take_basics() -> std::io::Result<()> { diff --git a/src/sync/barrier.rs b/src/sync/barrier.rs index a854c7894..e3607607c 100644 --- a/src/sync/barrier.rs +++ b/src/sync/barrier.rs @@ -210,7 +210,7 @@ mod test { use futures_util::stream::StreamExt; use crate::sync::{Arc, Barrier}; - use crate::task; + use crate::thread; #[test] fn test_barrier() { diff --git a/tests/addr.rs b/tests/addr.rs index a941c8376..208094d1d 100644 --- a/tests/addr.rs +++ b/tests/addr.rs @@ -1,7 +1,7 @@ use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV4, SocketAddrV6}; use async_std::net::ToSocketAddrs; -use async_std::task; +use async_std::thread; fn blocking_resolve(a: A) -> Result, String> where diff --git a/tests/io_timeout.rs b/tests/io_timeout.rs index b0f1c2047..4c4819d75 100644 --- a/tests/io_timeout.rs +++ b/tests/io_timeout.rs @@ -1,7 +1,7 @@ use std::time::Duration; use async_std::io; -use async_std::task; +use async_std::thread; #[test] #[should_panic(expected = "timed out")] diff --git a/tests/block_on.rs b/tests/spawn_task.rs similarity index 92% rename from tests/block_on.rs rename to tests/spawn_task.rs index efd55adc4..8de557c41 100644 --- a/tests/block_on.rs +++ b/tests/spawn_task.rs @@ -1,4 +1,4 @@ -use async_std::task; +use async_std::thread; #[test] fn smoke() { diff --git a/tests/task_local.rs b/tests/task_local.rs index 7342eab61..92facb89f 100644 --- a/tests/task_local.rs +++ b/tests/task_local.rs @@ -2,6 +2,7 @@ use std::sync::atomic::{AtomicBool, Ordering}; use async_std::task; use async_std::task_local; +use async_std::thread; #[test] fn drop_local() { diff --git a/tests/tcp.rs b/tests/tcp.rs index a36325b76..dbb701229 100644 --- a/tests/tcp.rs +++ b/tests/tcp.rs @@ -2,6 +2,7 @@ use async_std::io; use async_std::net::{TcpListener, TcpStream}; use async_std::prelude::*; use async_std::task; +use async_std::thread; const THE_WINTERS_TALE: &[u8] = b" Each your doing, diff --git a/tests/udp.rs b/tests/udp.rs index c0c880acf..201bc4b3d 100644 --- a/tests/udp.rs +++ b/tests/udp.rs @@ -1,6 +1,6 @@ use async_std::io; use async_std::net::UdpSocket; -use async_std::task; +use async_std::thread; const THE_MERCHANT_OF_VENICE: &[u8] = b" If you prick us, do we not bleed? diff --git a/tests/uds.rs b/tests/uds.rs index d9794efc5..0ed18930f 100644 --- a/tests/uds.rs +++ b/tests/uds.rs @@ -3,7 +3,7 @@ use async_std::io; use async_std::os::unix::net::{UnixDatagram, UnixListener, UnixStream}; use async_std::prelude::*; -use async_std::task; +use async_std::thread; use tempdir::TempDir;