| 1 | /// Prints to the standard output. |
| 2 | /// |
| 3 | /// Equivalent to the [`println!`] macro except that a newline is not printed at |
| 4 | /// the end of the message. |
| 5 | /// |
| 6 | /// Note that stdout is frequently line-buffered by default so it may be |
| 7 | /// necessary to use [`io::stdout().flush()`][flush] to ensure the output is emitted |
| 8 | /// immediately. |
| 9 | /// |
| 10 | /// Use `print!` only for the primary output of your program. Use |
| 11 | /// [`eprint!`] instead to print error and progress messages. |
| 12 | /// |
| 13 | /// [`println!`]: macro.println.html |
| 14 | /// [flush]: io/trait.Write.html#tymethod.flush |
| 15 | /// [`eprint!`]: macro.eprint.html |
| 16 | /// |
| 17 | /// # Panics |
| 18 | /// |
| 19 | /// Panics if writing to `io::stdout()` fails. |
| 20 | /// |
| 21 | /// # Examples |
| 22 | /// |
| 23 | /// ``` |
| 24 | /// # async_std::task::block_on(async { |
| 25 | /// # |
| 26 | /// use async_std::io; |
| 27 | /// use async_std::prelude::*; |
| 28 | /// use async_std::print; |
| 29 | /// |
| 30 | /// print!("this ").await; |
| 31 | /// print!("will ").await; |
| 32 | /// print!("be ").await; |
| 33 | /// print!("on ").await; |
| 34 | /// print!("the ").await; |
| 35 | /// print!("same ").await; |
| 36 | /// print!("line ").await; |
| 37 | /// |
| 38 | /// io::stdout().flush().await.unwrap(); |
| 39 | /// |
| 40 | /// print!("this string has a newline, why not choose println! instead?\n").await; |
| 41 | /// |
| 42 | /// io::stdout().flush().await.unwrap(); |
| 43 | /// # |
| 44 | /// # }) |
| 45 | /// ``` |
| 46 | #[cfg (feature = "unstable" )] |
| 47 | #[cfg_attr (feature = "docs" , doc(cfg(unstable)))] |
| 48 | #[macro_export ] |
| 49 | macro_rules! print { |
| 50 | ($($arg:tt)*) => ($crate::io::_print(format_args!($($arg)*))) |
| 51 | } |
| 52 | |
| 53 | /// Prints to the standard output, with a newline. |
| 54 | /// |
| 55 | /// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone |
| 56 | /// (no additional CARRIAGE RETURN (`\r`/`U+000D`)). |
| 57 | /// |
| 58 | /// Use the [`format!`] syntax to write data to the standard output. |
| 59 | /// See [`std::fmt`] for more information. |
| 60 | /// |
| 61 | /// Use `println!` only for the primary output of your program. Use |
| 62 | /// [`eprintln!`] instead to print error and progress messages. |
| 63 | /// |
| 64 | /// [`format!`]: macro.format.html |
| 65 | /// [`std::fmt`]: https://doc.rust-lang.org/std/fmt/index.html |
| 66 | /// [`eprintln!`]: macro.eprintln.html |
| 67 | /// # Panics |
| 68 | /// |
| 69 | /// Panics if writing to `io::stdout` fails. |
| 70 | /// |
| 71 | /// # Examples |
| 72 | /// |
| 73 | /// ``` |
| 74 | /// # async_std::task::block_on(async { |
| 75 | /// # |
| 76 | /// use async_std::println; |
| 77 | /// |
| 78 | /// println!().await; // prints just a newline |
| 79 | /// println!("hello there!").await; |
| 80 | /// println!("format {} arguments", "some").await; |
| 81 | /// # |
| 82 | /// # }) |
| 83 | /// ``` |
| 84 | #[cfg (feature = "unstable" )] |
| 85 | #[cfg_attr (feature = "docs" , doc(cfg(unstable)))] |
| 86 | #[macro_export ] |
| 87 | macro_rules! println { |
| 88 | () => ($crate::print!(" \n" )); |
| 89 | ($($arg:tt)*) => (async { |
| 90 | $crate::io::_print(format_args!($($arg)*)).await; |
| 91 | $crate::io::_print(format_args!(" \n" )).await; |
| 92 | }) |
| 93 | } |
| 94 | |
| 95 | /// Prints to the standard error. |
| 96 | /// |
| 97 | /// Equivalent to the [`print!`] macro, except that output goes to |
| 98 | /// [`io::stderr`] instead of `io::stdout`. See [`print!`] for |
| 99 | /// example usage. |
| 100 | /// |
| 101 | /// Use `eprint!` only for error and progress messages. Use `print!` |
| 102 | /// instead for the primary output of your program. |
| 103 | /// |
| 104 | /// [`io::stderr`]: io/struct.Stderr.html |
| 105 | /// [`print!`]: macro.print.html |
| 106 | /// |
| 107 | /// # Panics |
| 108 | /// |
| 109 | /// Panics if writing to `io::stderr` fails. |
| 110 | /// |
| 111 | /// # Examples |
| 112 | /// |
| 113 | /// ``` |
| 114 | /// # async_std::task::block_on(async { |
| 115 | /// # |
| 116 | /// use async_std::eprint; |
| 117 | /// |
| 118 | /// eprint!("Error: Could not complete task").await; |
| 119 | /// # |
| 120 | /// # }) |
| 121 | /// ``` |
| 122 | #[cfg (feature = "unstable" )] |
| 123 | #[cfg_attr (feature = "docs" , doc(cfg(unstable)))] |
| 124 | #[macro_export ] |
| 125 | macro_rules! eprint { |
| 126 | ($($arg:tt)*) => ($crate::io::_eprint(format_args!($($arg)*))) |
| 127 | } |
| 128 | |
| 129 | /// Prints to the standard error, with a newline. |
| 130 | /// |
| 131 | /// Equivalent to the [`println!`] macro, except that output goes to |
| 132 | /// [`io::stderr`] instead of `io::stdout`. See [`println!`] for |
| 133 | /// example usage. |
| 134 | /// |
| 135 | /// Use `eprintln!` only for error and progress messages. Use `println!` |
| 136 | /// instead for the primary output of your program. |
| 137 | /// |
| 138 | /// [`io::stderr`]: io/struct.Stderr.html |
| 139 | /// [`println!`]: macro.println.html |
| 140 | /// |
| 141 | /// # Panics |
| 142 | /// |
| 143 | /// Panics if writing to `io::stderr` fails. |
| 144 | /// |
| 145 | /// # Examples |
| 146 | /// |
| 147 | /// ``` |
| 148 | /// # async_std::task::block_on(async { |
| 149 | /// # |
| 150 | /// use async_std::eprintln; |
| 151 | /// |
| 152 | /// eprintln!("Error: Could not complete task").await; |
| 153 | /// # |
| 154 | /// # }) |
| 155 | /// ``` |
| 156 | #[cfg (feature = "unstable" )] |
| 157 | #[cfg_attr (feature = "docs" , doc(cfg(unstable)))] |
| 158 | #[macro_export ] |
| 159 | macro_rules! eprintln { |
| 160 | () => (async { $crate::eprint!(" \n" ).await; }); |
| 161 | ($($arg:tt)*) => ( |
| 162 | async { |
| 163 | $crate::io::_eprint(format_args!($($arg)*)).await; |
| 164 | $crate::io::_eprint(format_args!(" \n" )).await; |
| 165 | } |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | /// Declares task-local values. |
| 170 | /// |
| 171 | /// The macro wraps any number of static declarations and makes them task-local. Attributes and |
| 172 | /// visibility modifiers are allowed. |
| 173 | /// |
| 174 | /// Each declared value is of the accessor type [`LocalKey`]. |
| 175 | /// |
| 176 | /// [`LocalKey`]: task/struct.LocalKey.html |
| 177 | /// |
| 178 | /// # Examples |
| 179 | /// |
| 180 | /// ``` |
| 181 | /// # |
| 182 | /// use std::cell::Cell; |
| 183 | /// |
| 184 | /// use async_std::prelude::*; |
| 185 | /// use async_std::task; |
| 186 | /// |
| 187 | /// task_local! { |
| 188 | /// static VAL: Cell<u32> = Cell::new(5); |
| 189 | /// } |
| 190 | /// |
| 191 | /// task::block_on(async { |
| 192 | /// let v = VAL.with(|c| c.get()); |
| 193 | /// assert_eq!(v, 5); |
| 194 | /// }); |
| 195 | /// ``` |
| 196 | #[cfg (feature = "default" )] |
| 197 | #[macro_export ] |
| 198 | macro_rules! task_local { |
| 199 | () => (); |
| 200 | |
| 201 | ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr) => ( |
| 202 | $(#[$attr])* $vis static $name: $crate::task::LocalKey<$t> = { |
| 203 | #[inline] |
| 204 | fn __init() -> $t { |
| 205 | $init |
| 206 | } |
| 207 | |
| 208 | $crate::task::LocalKey { |
| 209 | __init, |
| 210 | __key: ::std::sync::atomic::AtomicU32::new(0), |
| 211 | } |
| 212 | }; |
| 213 | ); |
| 214 | |
| 215 | ($(#[$attr:meta])* $vis:vis static $name:ident: $t:ty = $init:expr; $($rest:tt)*) => ( |
| 216 | $crate::task_local!($(#[$attr])* $vis static $name: $t = $init); |
| 217 | $crate::task_local!($($rest)*); |
| 218 | ); |
| 219 | } |
| 220 | |