-
Notifications
You must be signed in to change notification settings - Fork 4
add print macros #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
09e0e88
add print macros
yoshuawuyts 41f4d07
fix macros
yoshuawuyts 1130ff2
fix newline logging
yoshuawuyts 6b0cc79
also export task_local
yoshuawuyts 1819134
cargo fmt
yoshuawuyts ecf5312
skip task_local macros
yoshuawuyts 0834228
fix printing
yoshuawuyts fcaa532
expand args outside async block
yoshuawuyts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
/// 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 | ||
/// | ||
/// ```ignore | ||
/// # 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(); | ||
/// # | ||
/// # }) | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! print { | ||
($($arg:tt)*) => ( | ||
async { | ||
use ::async_std::prelude::*; | ||
let args = $crate::utils::format_args!($($arg)*); | ||
if let Err(e) = ::async_std::io::stdout().write_fmt(args).await { | ||
$crate::utils::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 | ||
/// | ||
/// ```ignore | ||
/// # 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; | ||
/// # | ||
/// # }) | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! println { | ||
() => ($crate::print!("\n")); | ||
($($arg:tt)*) => ( | ||
async { | ||
use ::async_std::prelude::*; | ||
let args = $crate::utils::format_args!($($arg)*); | ||
if let Err(e) = ::async_std::io::stdout().write_fmt(args).await { | ||
$crate::utils::panic!("failed printing to stdout: {}", e); | ||
} | ||
$crate::utils::print!("\n").await; | ||
} | ||
); | ||
} | ||
|
||
/// 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 | ||
/// | ||
/// ```ignore | ||
/// # async_std::task::block_on(async { | ||
/// # | ||
/// use async_std::eprint; | ||
/// | ||
/// eprint!("Error: Could not complete task").await; | ||
/// # | ||
/// # }) | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! eprint { | ||
($($arg:tt)*) => ( | ||
async { | ||
use ::async_std::prelude::*; | ||
let args = $crate::utils::format_args!($($arg)*); | ||
if let Err(e) = ::async_std::io::stderr().write_fmt(args).await { | ||
$crate::utils::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 | ||
/// | ||
/// ```ignore | ||
/// # async_std::task::block_on(async { | ||
/// # | ||
/// use async_std::eprintln; | ||
/// | ||
/// eprintln!("Error: Could not complete task").await; | ||
/// # | ||
/// # }) | ||
/// ``` | ||
#[macro_export] | ||
macro_rules! eprintln { | ||
() => ($crate::eprint!("\n")); | ||
($($arg:tt)*) => ( | ||
async { | ||
use ::async_std::prelude::*; | ||
let args = $crate::utils::format_args!($($arg)*); | ||
if let Err(e) = ::async_std::io::stderr().write_fmt(args).await { | ||
$crate::utils::panic!("failed printing to stderr: {}", e); | ||
} | ||
$crate::eprint!("\n").await; | ||
} | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/// 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 | ||
/// | ||
/// ```ignore | ||
/// # | ||
/// use std::cell::Cell; | ||
/// | ||
/// use async_std::task; | ||
/// use async_std::prelude::*; | ||
/// | ||
/// task_local! { | ||
/// static VAL: Cell<u32> = 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: ::async_std::task::LocalKey<$t> = { | ||
#[inline] | ||
fn __init() -> $t { | ||
$init | ||
} | ||
|
||
::async_std::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)*); | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if this PR is good. This will make it difficult to reexport these macros.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe my understanding of scoping is off here; but doesn't this include the prelude only within the
async
block?The goal of this crate is to provide re-exports for async-std's submodules, because we can't define the macros inside the crate itself. I think as long as we cover that use case we should be okay?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is enough for that purpose.
(I was thinking about the restriction that
$crate
cannot be used. But given proc-macro always has this limitation, we probably don't need to worry.)