From a5086321051bd9e398df8efdbd0e74cdba1093b5 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 14 Oct 2019 17:13:43 +0200 Subject: [PATCH 1/3] re-export macros from async-macros Signed-off-by: Yoshua Wuyts --- Cargo.toml | 3 ++- examples/logging.rs | 2 ++ src/lib.rs | 3 +-- src/macros.rs | 34 +++++++++++++++++++++++++++++----- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0c652988e..a04bd122e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,8 @@ docs = ["broadcaster"] unstable = ["broadcaster"] [dependencies] -async-macros = "1.0.0" +# async-macros = "1.0.0" +async-macros = { path = "../async-macros"} async-task = "1.0.0" cfg-if = "0.1.9" crossbeam-channel = "0.3.9" diff --git a/examples/logging.rs b/examples/logging.rs index bd55aaa0c..8c13d4c20 100644 --- a/examples/logging.rs +++ b/examples/logging.rs @@ -1,12 +1,14 @@ //! Prints the runtime's execution log on the standard output. use async_std::task; +use async_std::prelude::*; fn main() { femme::start(log::LevelFilter::Trace).unwrap(); task::block_on(async { let handle = task::spawn(async { + async_std::println!("hello").await; log::info!("Hello world!"); }); diff --git a/src/lib.rs b/src/lib.rs index cef50ae24..5f30f5a84 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -76,10 +76,9 @@ cfg_if! { } } -mod macros; pub(crate) mod utils; #[cfg(any(feature = "unstable", feature = "docs"))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[doc(inline)] -pub use std::{write, writeln}; +pub use async_macros::{eprint, eprintln, print, println, write, writeln}; diff --git a/src/macros.rs b/src/macros.rs index d70b779fe..9c55996eb 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -47,7 +47,14 @@ #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[macro_export] macro_rules! print { - ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))) + ($($arg:tt)*) => ( + async { + let args = format_args!($($arg)*); + if let Err(e) = stdout().write_fmt(args).await { + panic!("failed printing to stdout: {}", e); + } + } + ); } /// Prints to the standard output, with a newline. @@ -86,7 +93,14 @@ macro_rules! print { #[macro_export] macro_rules! println { () => ($crate::print!("\n")); - ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))) + ($($arg:tt)*) => ( + async { + let args = format_args!($($arg)*); + if let Err(e) = stdout().write_fmt(args).await { + panic!("failed printing to stdout: {}", e); + } + } + ); } /// Prints to the standard error. @@ -120,7 +134,14 @@ macro_rules! println { #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[macro_export] macro_rules! eprint { - ($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*))) + ($($arg:tt)*) => ( + async { + let args = format_args!($($arg)*); + if let Err(e) = stderr().write_fmt(args).await { + panic!("failed printing to stderr: {}", e); + } + } + ); } /// Prints to the standard error, with a newline. @@ -154,10 +175,13 @@ macro_rules! eprint { #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[macro_export] macro_rules! eprintln { - () => (async { $crate::eprint!("\n").await; }); + () => ($crate::eprint!("\n")); ($($arg:tt)*) => ( async { - $crate::io::_eprint(format_args!($($arg)*)).await; + let args = format_args!($($arg)*); + if let Err(e) = stderr().write_fmt(args).await { + panic!("failed printing to stderr: {}", e); + } } ); } From 495a823bf3d77c31ff1c12b09505f9bb66f913d5 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 14 Oct 2019 17:21:03 +0200 Subject: [PATCH 2/3] wip Signed-off-by: Yoshua Wuyts --- examples/logging.rs | 2 +- src/io/mod.rs | 8 ++++---- src/io/stdio.rs | 21 --------------------- src/task/mod.rs | 5 +++++ 4 files changed, 10 insertions(+), 26 deletions(-) delete mode 100644 src/io/stdio.rs diff --git a/examples/logging.rs b/examples/logging.rs index 8c13d4c20..cf6299850 100644 --- a/examples/logging.rs +++ b/examples/logging.rs @@ -9,7 +9,7 @@ fn main() { task::block_on(async { let handle = task::spawn(async { async_std::println!("hello").await; - log::info!("Hello world!"); + // log::info!("Hello world!"); }); handle.await; diff --git a/src/io/mod.rs b/src/io/mod.rs index 812c97ab6..656f8991e 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -39,9 +39,10 @@ pub use stdout::{stdout, Stdout}; pub use timeout::timeout; pub use write::Write; -// For use in the print macros. -#[doc(hidden)] -pub use stdio::{_eprint, _print}; +#[cfg(any(feature = "unstable", feature = "docs"))] +#[cfg_attr(feature = "docs", doc(cfg(unstable)))] +#[doc(inline)] +pub use async_macros::{print, println, eprint, eprintln, write, writeln}; pub mod prelude; @@ -59,6 +60,5 @@ mod repeat; mod sink; mod stderr; mod stdin; -mod stdio; mod stdout; mod timeout; diff --git a/src/io/stdio.rs b/src/io/stdio.rs deleted file mode 100644 index 0e11e1a99..000000000 --- a/src/io/stdio.rs +++ /dev/null @@ -1,21 +0,0 @@ -//! Internal types for stdio. -//! -//! This module is a port of `libstd/io/stdio.rs`,and contains internal types for `print`/`eprint`. - -use crate::io::{stderr, stdout}; -use crate::prelude::*; -use std::fmt; - -#[doc(hidden)] -pub async fn _print(args: fmt::Arguments<'_>) { - if let Err(e) = stdout().write_fmt(args).await { - panic!("failed printing to stdout: {}", e); - } -} - -#[doc(hidden)] -pub async fn _eprint(args: fmt::Arguments<'_>) { - if let Err(e) = stderr().write_fmt(args).await { - panic!("failed printing to stderr: {}", e); - } -} diff --git a/src/task/mod.rs b/src/task/mod.rs index 578a545f7..42c4e2a31 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -30,6 +30,11 @@ pub use std::task::{Context, Poll, Waker}; #[doc(inline)] pub use async_macros::ready; +// #[cfg(any(feature = "unstable", feature = "docs"))] +// #[cfg_attr(feature = "docs", doc(cfg(unstable)))] +// #[doc(inline)] +// pub use async_macros::task_local; + pub use block_on::block_on; pub use builder::Builder; pub use pool::spawn; From 2f7ec1de9ef88cba245e0ac64423c1765f9ec3e0 Mon Sep 17 00:00:00 2001 From: Yoshua Wuyts Date: Mon, 14 Oct 2019 18:07:24 +0200 Subject: [PATCH 3/3] re-export macros Signed-off-by: Yoshua Wuyts --- examples/logging.rs | 4 +- src/io/mod.rs | 2 +- src/lib.rs | 2 + src/macros.rs | 187 ----------------------------------------- src/prelude.rs | 2 +- src/task/mod.rs | 8 +- src/task/task_local.rs | 51 ----------- 7 files changed, 9 insertions(+), 247 deletions(-) delete mode 100644 src/macros.rs diff --git a/examples/logging.rs b/examples/logging.rs index cf6299850..bd55aaa0c 100644 --- a/examples/logging.rs +++ b/examples/logging.rs @@ -1,15 +1,13 @@ //! Prints the runtime's execution log on the standard output. use async_std::task; -use async_std::prelude::*; fn main() { femme::start(log::LevelFilter::Trace).unwrap(); task::block_on(async { let handle = task::spawn(async { - async_std::println!("hello").await; - // log::info!("Hello world!"); + log::info!("Hello world!"); }); handle.await; diff --git a/src/io/mod.rs b/src/io/mod.rs index 656f8991e..75d025b02 100644 --- a/src/io/mod.rs +++ b/src/io/mod.rs @@ -42,7 +42,7 @@ pub use write::Write; #[cfg(any(feature = "unstable", feature = "docs"))] #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[doc(inline)] -pub use async_macros::{print, println, eprint, eprintln, write, writeln}; +pub use async_macros::{eprint, eprintln, print, println, write, writeln}; pub mod prelude; diff --git a/src/lib.rs b/src/lib.rs index 5f30f5a84..77f63b651 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,3 +82,5 @@ pub(crate) mod utils; #[cfg_attr(feature = "docs", doc(cfg(unstable)))] #[doc(inline)] pub use async_macros::{eprint, eprintln, print, println, write, writeln}; + +pub use async_macros::task_local; diff --git a/src/macros.rs b/src/macros.rs deleted file mode 100644 index 9c55996eb..000000000 --- a/src/macros.rs +++ /dev/null @@ -1,187 +0,0 @@ -/// Prints to the standard output. -/// -/// Equivalent to the [`println!`] macro except that a newline is not printed at -/// the end of the message. -/// -/// Note that stdout is frequently line-buffered by default so it may be -/// necessary to use [`io::stdout().flush()`][flush] to ensure the output is emitted -/// immediately. -/// -/// Use `print!` only for the primary output of your program. Use -/// [`eprint!`] instead to print error and progress messages. -/// -/// [`println!`]: macro.println.html -/// [flush]: io/trait.Write.html#tymethod.flush -/// [`eprint!`]: macro.eprint.html -/// -/// # Panics -/// -/// Panics if writing to `io::stdout()` fails. -/// -/// # Examples -/// -/// ``` -/// # async_std::task::block_on(async { -/// # -/// use async_std::prelude::*; -/// use async_std::io; -/// use async_std::print; -/// -/// print!("this ").await; -/// print!("will ").await; -/// print!("be ").await; -/// print!("on ").await; -/// print!("the ").await; -/// print!("same ").await; -/// print!("line ").await; -/// -/// io::stdout().flush().await.unwrap(); -/// -/// print!("this string has a newline, why not choose println! instead?\n").await; -/// -/// io::stdout().flush().await.unwrap(); -/// # -/// # }) -/// ``` -#[cfg(any(feature = "unstable", feature = "docs"))] -#[cfg_attr(feature = "docs", doc(cfg(unstable)))] -#[macro_export] -macro_rules! print { - ($($arg:tt)*) => ( - async { - let args = format_args!($($arg)*); - if let Err(e) = stdout().write_fmt(args).await { - panic!("failed printing to stdout: {}", e); - } - } - ); -} - -/// Prints to the standard output, with a newline. -/// -/// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone -/// (no additional CARRIAGE RETURN (`\r`/`U+000D`)). -/// -/// Use the [`format!`] syntax to write data to the standard output. -/// See [`std::fmt`] for more information. -/// -/// Use `println!` only for the primary output of your program. Use -/// [`eprintln!`] instead to print error and progress messages. -/// -/// [`format!`]: macro.format.html -/// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html -/// [`eprintln!`]: macro.eprintln.html -/// # Panics -/// -/// Panics if writing to `io::stdout` fails. -/// -/// # Examples -/// -/// ``` -/// # async_std::task::block_on(async { -/// # -/// use async_std::println; -/// -/// println!().await; // prints just a newline -/// println!("hello there!").await; -/// println!("format {} arguments", "some").await; -/// # -/// # }) -/// ``` -#[cfg(any(feature = "unstable", feature = "docs"))] -#[cfg_attr(feature = "docs", doc(cfg(unstable)))] -#[macro_export] -macro_rules! println { - () => ($crate::print!("\n")); - ($($arg:tt)*) => ( - async { - let args = format_args!($($arg)*); - if let Err(e) = stdout().write_fmt(args).await { - panic!("failed printing to stdout: {}", e); - } - } - ); -} - -/// Prints to the standard error. -/// -/// Equivalent to the [`print!`] macro, except that output goes to -/// [`io::stderr`] instead of `io::stdout`. See [`print!`] for -/// example usage. -/// -/// Use `eprint!` only for error and progress messages. Use `print!` -/// instead for the primary output of your program. -/// -/// [`io::stderr`]: io/struct.Stderr.html -/// [`print!`]: macro.print.html -/// -/// # Panics -/// -/// Panics if writing to `io::stderr` fails. -/// -/// # Examples -/// -/// ``` -/// # async_std::task::block_on(async { -/// # -/// use async_std::eprint; -/// -/// eprint!("Error: Could not complete task").await; -/// # -/// # }) -/// ``` -#[cfg(any(feature = "unstable", feature = "docs"))] -#[cfg_attr(feature = "docs", doc(cfg(unstable)))] -#[macro_export] -macro_rules! eprint { - ($($arg:tt)*) => ( - async { - let args = format_args!($($arg)*); - if let Err(e) = stderr().write_fmt(args).await { - panic!("failed printing to stderr: {}", e); - } - } - ); -} - -/// Prints to the standard error, with a newline. -/// -/// Equivalent to the [`println!`] macro, except that output goes to -/// [`io::stderr`] instead of `io::stdout`. See [`println!`] for -/// example usage. -/// -/// Use `eprintln!` only for error and progress messages. Use `println!` -/// instead for the primary output of your program. -/// -/// [`io::stderr`]: io/struct.Stderr.html -/// [`println!`]: macro.println.html -/// -/// # Panics -/// -/// Panics if writing to `io::stderr` fails. -/// -/// # Examples -/// -/// ``` -/// # async_std::task::block_on(async { -/// # -/// use async_std::eprintln; -/// -/// eprintln!("Error: Could not complete task").await; -/// # -/// # }) -/// ``` -#[cfg(any(feature = "unstable", feature = "docs"))] -#[cfg_attr(feature = "docs", doc(cfg(unstable)))] -#[macro_export] -macro_rules! eprintln { - () => ($crate::eprint!("\n")); - ($($arg:tt)*) => ( - async { - let args = format_args!($($arg)*); - if let Err(e) = stderr().write_fmt(args).await { - panic!("failed printing to stderr: {}", e); - } - } - ); -} diff --git a/src/prelude.rs b/src/prelude.rs index 645d7a24d..a34a69bd9 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -24,7 +24,7 @@ pub use crate::io::Write as _; #[doc(no_inline)] pub use crate::stream::Stream; #[doc(no_inline)] -pub use crate::task_local; +pub use async_macros::task_local; #[doc(hidden)] pub use crate::io::buf_read::BufReadExt as _; diff --git a/src/task/mod.rs b/src/task/mod.rs index 42c4e2a31..cd3ec01fc 100644 --- a/src/task/mod.rs +++ b/src/task/mod.rs @@ -30,10 +30,10 @@ pub use std::task::{Context, Poll, Waker}; #[doc(inline)] pub use async_macros::ready; -// #[cfg(any(feature = "unstable", feature = "docs"))] -// #[cfg_attr(feature = "docs", doc(cfg(unstable)))] -// #[doc(inline)] -// pub use async_macros::task_local; +#[cfg(any(feature = "unstable", feature = "docs"))] +#[cfg_attr(feature = "docs", doc(cfg(unstable)))] +#[doc(inline)] +pub use async_macros::task_local; pub use block_on::block_on; pub use builder::Builder; diff --git a/src/task/task_local.rs b/src/task/task_local.rs index 8347e34b6..1c134ab88 100644 --- a/src/task/task_local.rs +++ b/src/task/task_local.rs @@ -10,57 +10,6 @@ use super::worker; use crate::future::Future; use crate::utils::abort_on_panic; -/// Declares task-local values. -/// -/// The macro wraps any number of static declarations and makes them task-local. Attributes and -/// visibility modifiers are allowed. -/// -/// Each declared value is of the accessor type [`LocalKey`]. -/// -/// [`LocalKey`]: task/struct.LocalKey.html -/// -/// # Examples -/// -/// ``` -/// # -/// use std::cell::Cell; -/// -/// use async_std::task; -/// use async_std::prelude::*; -/// -/// task_local! { -/// static VAL: Cell = Cell::new(5); -/// } -/// -/// task::block_on(async { -/// let v = VAL.with(|c| c.get()); -/// assert_eq!(v, 5); -/// }); -/// ``` -#[macro_export] -macro_rules! task_local { - () => (); - - ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => ( - $(#[$attr])* $vis static $name: $crate::task::LocalKey<$t> = { - #[inline] - fn __init() -> $t { - $init - } - - $crate::task::LocalKey { - __init, - __key: ::std::sync::atomic::AtomicUsize::new(0), - } - }; - ); - - ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => ( - $crate::task_local!($(#[$attr])* $vis static $name: $t = $init); - $crate::task_local!($($rest)*); - ); -} - /// The key for accessing a task-local value. /// /// Every task-local value is lazily initialized on first access and destroyed when the task