2019-06-03 22:25:20 -07:00
|
|
|
#[doc(include = "panic.md")]
|
2019-10-23 19:30:21 -07:00
|
|
|
#[macro_export]
|
2019-10-30 18:55:17 +02:00
|
|
|
#[allow_internal_unstable(core_panic,
|
|
|
|
// FIXME(anp, eddyb) `core_intrinsics` is used here to allow calling
|
|
|
|
// the `caller_location` intrinsic, but once `#[track_caller]` is implemented,
|
|
|
|
// `panicking::{panic, panic_fmt}` can use that instead of a `Location` argument.
|
|
|
|
core_intrinsics,
|
|
|
|
)]
|
2019-10-23 19:30:21 -07:00
|
|
|
#[stable(feature = "core", since = "1.6.0")]
|
|
|
|
macro_rules! panic {
|
|
|
|
() => (
|
|
|
|
$crate::panic!("explicit panic")
|
|
|
|
);
|
2019-10-30 18:55:17 +02:00
|
|
|
($msg:expr) => (
|
|
|
|
$crate::panicking::panic($msg, $crate::intrinsics::caller_location())
|
|
|
|
);
|
2019-10-23 19:30:21 -07:00
|
|
|
($msg:expr,) => (
|
|
|
|
$crate::panic!($msg)
|
|
|
|
);
|
2019-10-30 18:55:17 +02:00
|
|
|
($fmt:expr, $($arg:tt)+) => (
|
|
|
|
$crate::panicking::panic_fmt(
|
|
|
|
$crate::format_args!($fmt, $($arg)+),
|
|
|
|
$crate::intrinsics::caller_location(),
|
|
|
|
)
|
|
|
|
);
|
2019-10-23 19:30:21 -07:00
|
|
|
}
|
|
|
|
|
2017-06-05 16:46:12 +07:00
|
|
|
/// Asserts that two expressions are equal to each other (using [`PartialEq`]).
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2015-03-10 17:59:23 -07:00
|
|
|
/// On panic, this macro will print the values of the expressions with their
|
|
|
|
/// debug representations.
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2017-06-05 16:46:12 +07:00
|
|
|
/// Like [`assert!`], this macro has a second form, where a custom
|
2016-12-08 14:54:04 -06:00
|
|
|
/// panic message can be provided.
|
|
|
|
///
|
2017-06-05 16:46:12 +07:00
|
|
|
/// [`PartialEq`]: cmp/trait.PartialEq.html
|
2017-03-12 14:04:52 -04:00
|
|
|
/// [`assert!`]: macro.assert.html
|
2017-03-07 19:13:17 +01:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let a = 3;
|
|
|
|
/// let b = 1 + 2;
|
2014-09-15 19:29:47 -07:00
|
|
|
/// assert_eq!(a, b);
|
2016-12-08 14:54:04 -06:00
|
|
|
///
|
|
|
|
/// assert_eq!(a, b, "we are testing addition with {} and {}", a, b);
|
2014-09-15 19:29:47 -07:00
|
|
|
/// ```
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 18:50:12 -07:00
|
|
|
#[macro_export]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-14 09:18:10 -08:00
|
|
|
macro_rules! assert_eq {
|
improve error message when two-arg assert_eq! receives a trailing comma
Previously, `assert_eq!(left, right,)` (respectively, `assert_ne!(left,
right,)`; note the trailing comma) would result in a confusing "requires
at least a format string argument" error. In reality, a format string is
optional, but the trailing comma puts us into the "match a token tree of
zero or more tokens" branch of the macro (in order to support the
optional format string), and passing the empty token tree into
`format_args!` results in the confusing error. If instead we match a
token tree of one or more tokens, we get a much more sensible
"unexpected end of macro invocation" error.
While we're here, fix up a stray space before a comma in the match
guards.
Resolves #39369.
2017-02-04 22:47:46 -08:00
|
|
|
($left:expr, $right:expr) => ({
|
2016-05-18 22:24:33 +09:00
|
|
|
match (&$left, &$right) {
|
2014-09-15 19:29:47 -07:00
|
|
|
(left_val, right_val) => {
|
2015-03-10 17:59:23 -07:00
|
|
|
if !(*left_val == *right_val) {
|
2019-01-21 19:36:27 +01:00
|
|
|
// The reborrows below are intentional. Without them, the stack slot for the
|
|
|
|
// borrow is initialized even before the values are compared, leading to a
|
|
|
|
// noticeable slow down.
|
2017-06-13 14:17:59 +01:00
|
|
|
panic!(r#"assertion failed: `(left == right)`
|
2017-06-22 22:18:57 +01:00
|
|
|
left: `{:?}`,
|
2019-01-21 19:36:27 +01:00
|
|
|
right: `{:?}`"#, &*left_val, &*right_val)
|
2014-09-15 19:29:47 -07:00
|
|
|
}
|
|
|
|
}
|
2014-05-11 11:14:14 -07:00
|
|
|
}
|
2016-05-31 01:53:14 +09:00
|
|
|
});
|
2017-11-09 12:36:38 +01:00
|
|
|
($left:expr, $right:expr,) => ({
|
2019-06-07 18:29:48 +03:00
|
|
|
$crate::assert_eq!($left, $right)
|
2017-11-09 12:36:38 +01:00
|
|
|
});
|
improve error message when two-arg assert_eq! receives a trailing comma
Previously, `assert_eq!(left, right,)` (respectively, `assert_ne!(left,
right,)`; note the trailing comma) would result in a confusing "requires
at least a format string argument" error. In reality, a format string is
optional, but the trailing comma puts us into the "match a token tree of
zero or more tokens" branch of the macro (in order to support the
optional format string), and passing the empty token tree into
`format_args!` results in the confusing error. If instead we match a
token tree of one or more tokens, we get a much more sensible
"unexpected end of macro invocation" error.
While we're here, fix up a stray space before a comma in the match
guards.
Resolves #39369.
2017-02-04 22:47:46 -08:00
|
|
|
($left:expr, $right:expr, $($arg:tt)+) => ({
|
2016-05-31 01:53:14 +09:00
|
|
|
match (&($left), &($right)) {
|
|
|
|
(left_val, right_val) => {
|
|
|
|
if !(*left_val == *right_val) {
|
2019-01-21 19:36:27 +01:00
|
|
|
// The reborrows below are intentional. Without them, the stack slot for the
|
|
|
|
// borrow is initialized even before the values are compared, leading to a
|
|
|
|
// noticeable slow down.
|
2017-06-13 14:17:59 +01:00
|
|
|
panic!(r#"assertion failed: `(left == right)`
|
2017-06-22 22:18:57 +01:00
|
|
|
left: `{:?}`,
|
2019-01-21 19:36:27 +01:00
|
|
|
right: `{:?}`: {}"#, &*left_val, &*right_val,
|
2019-08-15 21:23:11 +03:00
|
|
|
$crate::format_args!($($arg)+))
|
2016-05-31 01:53:14 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-11-14 09:18:10 -08:00
|
|
|
}
|
2014-05-11 11:14:14 -07:00
|
|
|
|
2017-06-05 16:46:12 +07:00
|
|
|
/// Asserts that two expressions are not equal to each other (using [`PartialEq`]).
|
2016-07-27 10:47:19 -07:00
|
|
|
///
|
|
|
|
/// On panic, this macro will print the values of the expressions with their
|
|
|
|
/// debug representations.
|
|
|
|
///
|
2017-06-05 16:46:12 +07:00
|
|
|
/// Like [`assert!`], this macro has a second form, where a custom
|
2016-12-08 14:54:04 -06:00
|
|
|
/// panic message can be provided.
|
|
|
|
///
|
2017-06-05 16:46:12 +07:00
|
|
|
/// [`PartialEq`]: cmp/trait.PartialEq.html
|
2017-03-07 19:13:17 +01:00
|
|
|
/// [`assert!`]: macro.assert.html
|
|
|
|
///
|
2016-07-27 10:47:19 -07:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let a = 3;
|
|
|
|
/// let b = 2;
|
|
|
|
/// assert_ne!(a, b);
|
2016-12-08 14:54:04 -06:00
|
|
|
///
|
|
|
|
/// assert_ne!(a, b, "we are testing that the values are not equal");
|
2016-07-27 10:47:19 -07:00
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
2017-04-18 23:33:38 +01:00
|
|
|
#[stable(feature = "assert_ne", since = "1.13.0")]
|
2016-07-27 10:47:19 -07:00
|
|
|
macro_rules! assert_ne {
|
improve error message when two-arg assert_eq! receives a trailing comma
Previously, `assert_eq!(left, right,)` (respectively, `assert_ne!(left,
right,)`; note the trailing comma) would result in a confusing "requires
at least a format string argument" error. In reality, a format string is
optional, but the trailing comma puts us into the "match a token tree of
zero or more tokens" branch of the macro (in order to support the
optional format string), and passing the empty token tree into
`format_args!` results in the confusing error. If instead we match a
token tree of one or more tokens, we get a much more sensible
"unexpected end of macro invocation" error.
While we're here, fix up a stray space before a comma in the match
guards.
Resolves #39369.
2017-02-04 22:47:46 -08:00
|
|
|
($left:expr, $right:expr) => ({
|
2016-07-27 10:47:19 -07:00
|
|
|
match (&$left, &$right) {
|
|
|
|
(left_val, right_val) => {
|
|
|
|
if *left_val == *right_val {
|
2019-01-21 19:36:27 +01:00
|
|
|
// The reborrows below are intentional. Without them, the stack slot for the
|
|
|
|
// borrow is initialized even before the values are compared, leading to a
|
|
|
|
// noticeable slow down.
|
2017-06-13 14:53:01 +01:00
|
|
|
panic!(r#"assertion failed: `(left != right)`
|
2017-06-22 22:18:57 +01:00
|
|
|
left: `{:?}`,
|
2019-01-21 19:36:27 +01:00
|
|
|
right: `{:?}`"#, &*left_val, &*right_val)
|
2016-07-27 10:47:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2017-11-09 12:36:38 +01:00
|
|
|
($left:expr, $right:expr,) => {
|
2019-06-07 18:29:48 +03:00
|
|
|
$crate::assert_ne!($left, $right)
|
2017-11-09 12:36:38 +01:00
|
|
|
};
|
improve error message when two-arg assert_eq! receives a trailing comma
Previously, `assert_eq!(left, right,)` (respectively, `assert_ne!(left,
right,)`; note the trailing comma) would result in a confusing "requires
at least a format string argument" error. In reality, a format string is
optional, but the trailing comma puts us into the "match a token tree of
zero or more tokens" branch of the macro (in order to support the
optional format string), and passing the empty token tree into
`format_args!` results in the confusing error. If instead we match a
token tree of one or more tokens, we get a much more sensible
"unexpected end of macro invocation" error.
While we're here, fix up a stray space before a comma in the match
guards.
Resolves #39369.
2017-02-04 22:47:46 -08:00
|
|
|
($left:expr, $right:expr, $($arg:tt)+) => ({
|
2016-07-27 10:47:19 -07:00
|
|
|
match (&($left), &($right)) {
|
|
|
|
(left_val, right_val) => {
|
|
|
|
if *left_val == *right_val {
|
2019-01-21 19:36:27 +01:00
|
|
|
// The reborrows below are intentional. Without them, the stack slot for the
|
|
|
|
// borrow is initialized even before the values are compared, leading to a
|
|
|
|
// noticeable slow down.
|
2017-06-13 14:53:01 +01:00
|
|
|
panic!(r#"assertion failed: `(left != right)`
|
2017-06-22 22:18:57 +01:00
|
|
|
left: `{:?}`,
|
2019-01-21 19:36:27 +01:00
|
|
|
right: `{:?}`: {}"#, &*left_val, &*right_val,
|
2019-08-15 21:23:11 +03:00
|
|
|
$crate::format_args!($($arg)+))
|
2016-07-27 10:47:19 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Asserts that a boolean expression is `true` at runtime.
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2017-03-07 19:13:17 +01:00
|
|
|
/// This will invoke the [`panic!`] macro if the provided expression cannot be
|
2014-09-15 19:29:47 -07:00
|
|
|
/// evaluated to `true` at runtime.
|
|
|
|
///
|
2017-03-07 19:13:17 +01:00
|
|
|
/// Like [`assert!`], this macro also has a second version, where a custom panic
|
2015-10-28 00:56:27 +01:00
|
|
|
/// message can be provided.
|
|
|
|
///
|
2017-06-05 16:46:12 +07:00
|
|
|
/// # Uses
|
|
|
|
///
|
2017-03-07 19:13:17 +01:00
|
|
|
/// Unlike [`assert!`], `debug_assert!` statements are only enabled in non
|
2019-07-09 15:26:18 +03:00
|
|
|
/// optimized builds by default. An optimized build will not execute
|
2015-03-02 14:51:24 -08:00
|
|
|
/// `debug_assert!` statements unless `-C debug-assertions` is passed to the
|
|
|
|
/// compiler. This makes `debug_assert!` useful for checks that are too
|
|
|
|
/// expensive to be present in a release build but may be helpful during
|
2019-07-09 19:10:22 +03:00
|
|
|
/// development. The result of expanding `debug_assert!` is always type checked.
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2016-07-27 13:12:35 -04:00
|
|
|
/// An unchecked assertion allows a program in an inconsistent state to keep
|
|
|
|
/// running, which might have unexpected consequences but does not introduce
|
|
|
|
/// unsafety as long as this only happens in safe code. The performance cost
|
2017-03-07 19:13:17 +01:00
|
|
|
/// of assertions, is however, not measurable in general. Replacing [`assert!`]
|
2016-07-27 15:01:43 -04:00
|
|
|
/// with `debug_assert!` is thus only encouraged after thorough profiling, and
|
2016-07-27 13:12:35 -04:00
|
|
|
/// more importantly, only in safe code!
|
|
|
|
///
|
2017-03-07 19:13:17 +01:00
|
|
|
/// [`panic!`]: macro.panic.html
|
|
|
|
/// [`assert!`]: macro.assert.html
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // the panic message for these assertions is the stringified value of the
|
|
|
|
/// // expression given.
|
|
|
|
/// debug_assert!(true);
|
2015-01-12 16:59:34 -05:00
|
|
|
///
|
|
|
|
/// fn some_expensive_computation() -> bool { true } // a very simple function
|
2014-09-15 19:29:47 -07:00
|
|
|
/// debug_assert!(some_expensive_computation());
|
|
|
|
///
|
|
|
|
/// // assert with a custom message
|
2015-01-12 16:59:34 -05:00
|
|
|
/// let x = true;
|
2014-09-15 19:29:47 -07:00
|
|
|
/// debug_assert!(x, "x wasn't true!");
|
2015-01-12 16:59:34 -05:00
|
|
|
///
|
|
|
|
/// let a = 3; let b = 27;
|
2014-09-15 19:29:47 -07:00
|
|
|
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-09-15 19:29:47 -07:00
|
|
|
macro_rules! debug_assert {
|
2019-08-15 21:23:11 +03:00
|
|
|
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert!($($arg)*); })
|
2014-09-15 19:29:47 -07:00
|
|
|
}
|
|
|
|
|
2015-12-29 11:01:35 -05:00
|
|
|
/// Asserts that two expressions are equal to each other.
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2015-12-29 11:01:35 -05:00
|
|
|
/// On panic, this macro will print the values of the expressions with their
|
|
|
|
/// debug representations.
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// Unlike [`assert_eq!`], `debug_assert_eq!` statements are only enabled in non
|
2019-07-09 15:26:18 +03:00
|
|
|
/// optimized builds by default. An optimized build will not execute
|
2015-03-02 14:51:24 -08:00
|
|
|
/// `debug_assert_eq!` statements unless `-C debug-assertions` is passed to the
|
|
|
|
/// compiler. This makes `debug_assert_eq!` useful for checks that are too
|
|
|
|
/// expensive to be present in a release build but may be helpful during
|
2019-07-09 19:10:22 +03:00
|
|
|
/// development. The result of expanding `debug_assert_eq!` is always type checked.
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// [`assert_eq!`]: ../std/macro.assert_eq.html
|
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
|
|
|
/// ```
|
2015-01-22 14:08:56 +00:00
|
|
|
/// let a = 3;
|
|
|
|
/// let b = 1 + 2;
|
2014-09-15 19:29:47 -07:00
|
|
|
/// debug_assert_eq!(a, b);
|
|
|
|
/// ```
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 18:50:12 -07:00
|
|
|
#[macro_export]
|
2015-12-02 17:31:49 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-11-14 09:18:10 -08:00
|
|
|
macro_rules! debug_assert_eq {
|
2019-08-15 21:23:11 +03:00
|
|
|
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert_eq!($($arg)*); })
|
2014-11-14 09:18:10 -08:00
|
|
|
}
|
std: Recreate a `collections` module
As with the previous commit with `librand`, this commit shuffles around some
`collections` code. The new state of the world is similar to that of librand:
* The libcollections crate now only depends on libcore and liballoc.
* The standard library has a new module, `std::collections`. All functionality
of libcollections is reexported through this module.
I would like to stress that this change is purely cosmetic. There are very few
alterations to these primitives.
There are a number of notable points about the new organization:
* std::{str, slice, string, vec} all moved to libcollections. There is no reason
that these primitives shouldn't be necessarily usable in a freestanding
context that has allocation. These are all reexported in their usual places in
the standard library.
* The `hashmap`, and transitively the `lru_cache`, modules no longer reside in
`libcollections`, but rather in libstd. The reason for this is because the
`HashMap::new` contructor requires access to the OSRng for initially seeding
the hash map. Beyond this requirement, there is no reason that the hashmap
could not move to libcollections.
I do, however, have a plan to move the hash map to the collections module. The
`HashMap::new` function could be altered to require that the `H` hasher
parameter ascribe to the `Default` trait, allowing the entire `hashmap` module
to live in libcollections. The key idea would be that the default hasher would
be different in libstd. Something along the lines of:
// src/libstd/collections/mod.rs
pub type HashMap<K, V, H = RandomizedSipHasher> =
core_collections::HashMap<K, V, H>;
This is not possible today because you cannot invoke static methods through
type aliases. If we modified the compiler, however, to allow invocation of
static methods through type aliases, then this type definition would
essentially be switching the default hasher from `SipHasher` in libcollections
to a libstd-defined `RandomizedSipHasher` type. This type's `Default`
implementation would randomly seed the `SipHasher` instance, and otherwise
perform the same as `SipHasher`.
This future state doesn't seem incredibly far off, but until that time comes,
the hashmap module will live in libstd to not compromise on functionality.
* In preparation for the hashmap moving to libcollections, the `hash` module has
moved from libstd to libcollections. A previously snapshotted commit enables a
distinct `Writer` trait to live in the `hash` module which `Hash`
implementations are now parameterized over.
Due to using a custom trait, the `SipHasher` implementation has lost its
specialized methods for writing integers. These can be re-added
backwards-compatibly in the future via default methods if necessary, but the
FNV hashing should satisfy much of the need for speedier hashing.
A list of breaking changes:
* HashMap::{get, get_mut} no longer fails with the key formatted into the error
message with `{:?}`, instead, a generic message is printed. With backtraces,
it should still be not-too-hard to track down errors.
* The HashMap, HashSet, and LruCache types are now available through
std::collections instead of the collections crate.
* Manual implementations of hash should be parameterized over `hash::Writer`
instead of just `Writer`.
[breaking-change]
2014-05-29 18:50:12 -07:00
|
|
|
|
2016-07-27 10:47:19 -07:00
|
|
|
/// Asserts that two expressions are not equal to each other.
|
|
|
|
///
|
|
|
|
/// On panic, this macro will print the values of the expressions with their
|
|
|
|
/// debug representations.
|
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// Unlike [`assert_ne!`], `debug_assert_ne!` statements are only enabled in non
|
2019-07-09 15:26:18 +03:00
|
|
|
/// optimized builds by default. An optimized build will not execute
|
2016-07-27 10:47:19 -07:00
|
|
|
/// `debug_assert_ne!` statements unless `-C debug-assertions` is passed to the
|
|
|
|
/// compiler. This makes `debug_assert_ne!` useful for checks that are too
|
|
|
|
/// expensive to be present in a release build but may be helpful during
|
2019-07-09 19:10:22 +03:00
|
|
|
/// development. The result of expanding `debug_assert_ne!` is always type checked.
|
2016-07-27 10:47:19 -07:00
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// [`assert_ne!`]: ../std/macro.assert_ne.html
|
|
|
|
///
|
2016-07-27 10:47:19 -07:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let a = 3;
|
|
|
|
/// let b = 2;
|
|
|
|
/// debug_assert_ne!(a, b);
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
2017-04-18 23:33:38 +01:00
|
|
|
#[stable(feature = "assert_ne", since = "1.13.0")]
|
2016-07-27 10:47:19 -07:00
|
|
|
macro_rules! debug_assert_ne {
|
2019-08-15 21:23:11 +03:00
|
|
|
($($arg:tt)*) => (if $crate::cfg!(debug_assertions) { $crate::assert_ne!($($arg)*); })
|
2016-07-27 10:47:19 -07:00
|
|
|
}
|
|
|
|
|
2019-10-23 15:42:52 +02:00
|
|
|
/// Returns whether the given expression matches any of the given patterns.
|
|
|
|
///
|
|
|
|
/// Like in a `match` expression, the pattern can be optionally followed by `if`
|
|
|
|
/// and a guard expression that has access to names bound by the pattern.
|
2019-10-23 15:30:04 +02:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(matches_macro)]
|
|
|
|
///
|
|
|
|
/// let foo = 'f';
|
|
|
|
/// assert!(matches!(foo, 'A'..='Z' | 'a'..='z'));
|
|
|
|
///
|
|
|
|
/// let bar = Some(4);
|
|
|
|
/// assert!(matches!(bar, Some(x) if x > 2));
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
2019-10-23 15:34:24 +02:00
|
|
|
#[unstable(feature = "matches_macro", issue = "65721")]
|
2019-10-23 15:30:04 +02:00
|
|
|
macro_rules! matches {
|
|
|
|
($expression:expr, $( $pattern:pat )|+ $( if $guard: expr )?) => {
|
|
|
|
match $expression {
|
|
|
|
$( $pattern )|+ $( if $guard )? => true,
|
|
|
|
_ => false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Unwraps a result or propagates its error.
|
2016-08-29 20:05:47 +02:00
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// The `?` operator was added to replace `try!` and should be used instead.
|
2018-12-01 18:16:08 -06:00
|
|
|
/// Furthermore, `try` is a reserved word in Rust 2018, so if you must use
|
2018-12-01 18:24:11 -06:00
|
|
|
/// it, you will need to use the [raw-identifier syntax][ris]: `r#try`.
|
|
|
|
///
|
|
|
|
/// [ris]: https://doc.rust-lang.org/nightly/rust-by-example/compatibility/raw_identifiers.html
|
2016-10-06 11:36:36 +13:00
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// `try!` matches the given [`Result`]. In case of the `Ok` variant, the
|
2016-08-29 20:05:47 +02:00
|
|
|
/// expression has the value of the wrapped value.
|
|
|
|
///
|
|
|
|
/// In case of the `Err` variant, it retrieves the inner error. `try!` then
|
|
|
|
/// performs conversion using `From`. This provides automatic conversion
|
|
|
|
/// between specialized errors and more general ones. The resulting
|
|
|
|
/// error is then immediately returned.
|
|
|
|
///
|
|
|
|
/// Because of the early return, `try!` can only be used in functions that
|
2017-08-29 10:17:33 -07:00
|
|
|
/// return [`Result`].
|
|
|
|
///
|
|
|
|
/// [`Result`]: ../std/result/enum.Result.html
|
2015-12-02 17:31:49 -08:00
|
|
|
///
|
|
|
|
/// # Examples
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2015-12-02 17:31:49 -08:00
|
|
|
/// ```
|
|
|
|
/// use std::io;
|
|
|
|
/// use std::fs::File;
|
|
|
|
/// use std::io::prelude::*;
|
|
|
|
///
|
2016-08-29 20:05:47 +02:00
|
|
|
/// enum MyError {
|
|
|
|
/// FileWriteError
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl From<io::Error> for MyError {
|
|
|
|
/// fn from(e: io::Error) -> MyError {
|
|
|
|
/// MyError::FileWriteError
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
2018-02-10 12:22:57 +01:00
|
|
|
/// // The preferred method of quick returning Errors
|
2017-08-29 10:17:33 -07:00
|
|
|
/// fn write_to_file_question() -> Result<(), MyError> {
|
|
|
|
/// let mut file = File::create("my_best_friends.txt")?;
|
2018-01-04 15:55:01 +02:00
|
|
|
/// file.write_all(b"This is a list of my best friends.")?;
|
2017-08-29 10:17:33 -07:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // The previous method of quick returning Errors
|
2016-08-29 20:05:47 +02:00
|
|
|
/// fn write_to_file_using_try() -> Result<(), MyError> {
|
2018-12-01 15:48:55 -06:00
|
|
|
/// let mut file = r#try!(File::create("my_best_friends.txt"));
|
|
|
|
/// r#try!(file.write_all(b"This is a list of my best friends."));
|
2015-12-02 17:31:49 -08:00
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2017-08-29 10:17:33 -07:00
|
|
|
///
|
2015-12-02 17:31:49 -08:00
|
|
|
/// // This is equivalent to:
|
2016-08-29 20:05:47 +02:00
|
|
|
/// fn write_to_file_using_match() -> Result<(), MyError> {
|
2018-12-01 15:48:55 -06:00
|
|
|
/// let mut file = r#try!(File::create("my_best_friends.txt"));
|
2015-12-02 17:31:49 -08:00
|
|
|
/// match file.write_all(b"This is a list of my best friends.") {
|
2016-04-12 01:18:35 +02:00
|
|
|
/// Ok(v) => v,
|
2016-08-29 20:05:47 +02:00
|
|
|
/// Err(e) => return Err(From::from(e)),
|
2015-12-02 17:31:49 -08:00
|
|
|
/// }
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-05-10 13:48:26 -07:00
|
|
|
#[macro_export]
|
2015-12-02 17:31:49 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-07-15 17:42:08 +00:00
|
|
|
#[rustc_deprecated(since = "1.39.0", reason = "use the `?` operator instead")]
|
2018-04-19 19:56:10 +02:00
|
|
|
#[doc(alias = "?")]
|
2018-12-01 15:48:55 -06:00
|
|
|
macro_rules! r#try {
|
2015-12-02 17:31:49 -08:00
|
|
|
($expr:expr) => (match $expr {
|
|
|
|
$crate::result::Result::Ok(val) => val,
|
|
|
|
$crate::result::Result::Err(err) => {
|
|
|
|
return $crate::result::Result::Err($crate::convert::From::from(err))
|
2014-09-15 19:29:47 -07:00
|
|
|
}
|
2018-02-07 09:31:22 -05:00
|
|
|
});
|
2019-06-07 18:29:48 +03:00
|
|
|
($expr:expr,) => ($crate::r#try!($expr));
|
2014-11-14 09:18:10 -08:00
|
|
|
}
|
2014-05-11 11:14:14 -07:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Writes formatted data into a buffer.
|
2015-08-27 14:24:53 -04:00
|
|
|
///
|
2017-03-30 13:23:35 -05:00
|
|
|
/// This macro accepts a format string, a list of arguments, and a 'writer'. Arguments will be
|
|
|
|
/// formatted according to the specified format string and the result will be passed to the writer.
|
|
|
|
/// The writer may be any value with a `write_fmt` method; generally this comes from an
|
|
|
|
/// implementation of either the [`std::fmt::Write`] or the [`std::io::Write`] trait. The macro
|
2017-08-29 10:17:33 -07:00
|
|
|
/// returns whatever the `write_fmt` method returns; commonly a [`std::fmt::Result`], or an
|
2017-03-30 13:23:35 -05:00
|
|
|
/// [`io::Result`].
|
2016-08-04 03:11:50 +03:00
|
|
|
///
|
2017-03-30 13:23:35 -05:00
|
|
|
/// See [`std::fmt`] for more information on the format string syntax.
|
2016-08-04 03:11:50 +03:00
|
|
|
///
|
2017-03-30 13:23:35 -05:00
|
|
|
/// [`std::fmt`]: ../std/fmt/index.html
|
|
|
|
/// [`std::fmt::Write`]: ../std/fmt/trait.Write.html
|
|
|
|
/// [`std::io::Write`]: ../std/io/trait.Write.html
|
|
|
|
/// [`std::fmt::Result`]: ../std/fmt/type.Result.html
|
|
|
|
/// [`io::Result`]: ../std/io/type.Result.html
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2015-03-11 21:11:40 -04:00
|
|
|
/// # Examples
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
|
|
|
/// ```
|
2015-03-17 13:33:26 -07:00
|
|
|
/// use std::io::Write;
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2019-06-20 14:36:53 +02:00
|
|
|
/// fn main() -> std::io::Result<()> {
|
|
|
|
/// let mut w = Vec::new();
|
|
|
|
/// write!(&mut w, "test")?;
|
|
|
|
/// write!(&mut w, "formatted {}", "arguments")?;
|
2015-08-27 14:24:53 -04:00
|
|
|
///
|
2019-06-20 14:36:53 +02:00
|
|
|
/// assert_eq!(w, b"testformatted arguments");
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2014-09-15 19:29:47 -07:00
|
|
|
/// ```
|
2016-10-29 15:23:49 -07:00
|
|
|
///
|
|
|
|
/// A module can import both `std::fmt::Write` and `std::io::Write` and call `write!` on objects
|
|
|
|
/// implementing either, as objects do not typically implement both. However, the module must
|
|
|
|
/// import the traits qualified so their names do not conflict:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fmt::Write as FmtWrite;
|
|
|
|
/// use std::io::Write as IoWrite;
|
|
|
|
///
|
2019-07-13 19:27:22 +02:00
|
|
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
/// let mut s = String::new();
|
|
|
|
/// let mut v = Vec::new();
|
|
|
|
///
|
|
|
|
/// write!(&mut s, "{} {}", "abc", 123)?; // uses fmt::Write::write_fmt
|
|
|
|
/// write!(&mut v, "s = {:?}", s)?; // uses io::Write::write_fmt
|
|
|
|
/// assert_eq!(v, b"s = \"abc 123\"");
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2016-10-29 15:23:49 -07:00
|
|
|
/// ```
|
2018-08-12 22:17:30 -04:00
|
|
|
///
|
2018-11-09 23:12:46 -05:00
|
|
|
/// Note: This macro can be used in `no_std` setups as well.
|
|
|
|
/// In a `no_std` setup you are responsible for the implementation details of the components.
|
2018-08-12 22:17:30 -04:00
|
|
|
///
|
2018-08-13 00:10:14 -04:00
|
|
|
/// ```no_run
|
2018-09-05 09:09:50 -04:00
|
|
|
/// # extern crate core;
|
|
|
|
/// use core::fmt::Write;
|
2018-08-12 22:17:30 -04:00
|
|
|
///
|
2018-09-05 09:09:50 -04:00
|
|
|
/// struct Example;
|
2018-08-12 22:17:30 -04:00
|
|
|
///
|
2018-09-05 09:09:50 -04:00
|
|
|
/// impl Write for Example {
|
|
|
|
/// fn write_str(&mut self, _s: &str) -> core::fmt::Result {
|
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
|
|
|
/// }
|
2018-08-12 22:17:30 -04:00
|
|
|
///
|
2018-09-05 09:09:50 -04:00
|
|
|
/// let mut m = Example{};
|
|
|
|
/// write!(&mut m, "Hello World").expect("Not written");
|
2018-08-11 14:43:50 -04:00
|
|
|
/// ```
|
2014-12-27 23:57:43 +02:00
|
|
|
#[macro_export]
|
2017-04-18 23:33:38 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-12-27 23:57:43 +02:00
|
|
|
macro_rules! write {
|
2019-08-15 21:23:11 +03:00
|
|
|
($dst:expr, $($arg:tt)*) => ($dst.write_fmt($crate::format_args!($($arg)*)))
|
2014-12-27 23:57:43 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 15:44:43 -07:00
|
|
|
/// Write formatted data into a buffer, with a newline appended.
|
2016-08-04 03:11:50 +03:00
|
|
|
///
|
2016-08-04 04:33:50 +03:00
|
|
|
/// On all platforms, the newline is the LINE FEED character (`\n`/`U+000A`) alone
|
|
|
|
/// (no additional CARRIAGE RETURN (`\r`/`U+000D`).
|
2015-08-27 14:24:53 -04:00
|
|
|
///
|
2017-03-30 13:23:35 -05:00
|
|
|
/// For more information, see [`write!`]. For information on the format string syntax, see
|
|
|
|
/// [`std::fmt`].
|
2016-08-04 04:33:50 +03:00
|
|
|
///
|
2017-03-30 13:23:35 -05:00
|
|
|
/// [`write!`]: macro.write.html
|
|
|
|
/// [`std::fmt`]: ../std/fmt/index.html
|
2016-08-04 04:33:50 +03:00
|
|
|
///
|
2015-08-27 14:24:53 -04:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
2019-07-13 19:27:22 +02:00
|
|
|
/// use std::io::{Write, Result};
|
2015-08-27 14:24:53 -04:00
|
|
|
///
|
2019-07-13 19:27:22 +02:00
|
|
|
/// fn main() -> Result<()> {
|
|
|
|
/// let mut w = Vec::new();
|
|
|
|
/// writeln!(&mut w)?;
|
|
|
|
/// writeln!(&mut w, "test")?;
|
|
|
|
/// writeln!(&mut w, "formatted {}", "arguments")?;
|
2015-08-27 14:24:53 -04:00
|
|
|
///
|
2019-07-13 19:27:22 +02:00
|
|
|
/// assert_eq!(&w[..], "\ntest\nformatted arguments\n".as_bytes());
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2015-08-27 14:24:53 -04:00
|
|
|
/// ```
|
2016-10-29 15:23:49 -07:00
|
|
|
///
|
|
|
|
/// A module can import both `std::fmt::Write` and `std::io::Write` and call `write!` on objects
|
|
|
|
/// implementing either, as objects do not typically implement both. However, the module must
|
|
|
|
/// import the traits qualified so their names do not conflict:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fmt::Write as FmtWrite;
|
|
|
|
/// use std::io::Write as IoWrite;
|
|
|
|
///
|
2019-07-13 19:27:22 +02:00
|
|
|
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
/// let mut s = String::new();
|
|
|
|
/// let mut v = Vec::new();
|
|
|
|
///
|
|
|
|
/// writeln!(&mut s, "{} {}", "abc", 123)?; // uses fmt::Write::write_fmt
|
|
|
|
/// writeln!(&mut v, "s = {:?}", s)?; // uses io::Write::write_fmt
|
|
|
|
/// assert_eq!(v, b"s = \"abc 123\\n\"\n");
|
|
|
|
/// Ok(())
|
|
|
|
/// }
|
2016-10-29 15:23:49 -07:00
|
|
|
/// ```
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
#[macro_export]
|
2015-01-23 21:48:20 -08:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-02-27 16:58:12 -07:00
|
|
|
#[allow_internal_unstable(format_args_nl)]
|
2014-11-14 09:18:10 -08:00
|
|
|
macro_rules! writeln {
|
2016-12-19 16:57:23 +01:00
|
|
|
($dst:expr) => (
|
2019-06-07 18:29:48 +03:00
|
|
|
$crate::write!($dst, "\n")
|
2016-12-19 16:57:23 +01:00
|
|
|
);
|
2018-02-07 09:31:22 -05:00
|
|
|
($dst:expr,) => (
|
2019-06-07 18:29:48 +03:00
|
|
|
$crate::writeln!($dst)
|
2018-02-07 09:31:22 -05:00
|
|
|
);
|
2018-08-10 19:01:54 +01:00
|
|
|
($dst:expr, $($arg:tt)*) => (
|
2019-08-15 21:23:11 +03:00
|
|
|
$dst.write_fmt($crate::format_args_nl!($($arg)*))
|
2015-01-06 18:46:37 -05:00
|
|
|
);
|
2014-11-14 09:18:10 -08:00
|
|
|
}
|
std: Extract librustrt out of libstd
As part of the libstd facade efforts, this commit extracts the runtime interface
out of the standard library into a standalone crate, librustrt. This crate will
provide the following services:
* Definition of the rtio interface
* Definition of the Runtime interface
* Implementation of the Task structure
* Implementation of task-local-data
* Implementation of task failure via unwinding via libunwind
* Implementation of runtime initialization and shutdown
* Implementation of thread-local-storage for the local rust Task
Notably, this crate avoids the following services:
* Thread creation and destruction. The crate does not require the knowledge of
an OS threading system, and as a result it seemed best to leave out the
`rt::thread` module from librustrt. The librustrt module does depend on
mutexes, however.
* Implementation of backtraces. There is no inherent requirement for the runtime
to be able to generate backtraces. As will be discussed later, this
functionality continues to live in libstd rather than librustrt.
As usual, a number of architectural changes were required to make this crate
possible. Users of "stable" functionality will not be impacted by this change,
but users of the `std::rt` module will likely note the changes. A list of
architectural changes made is:
* The stdout/stderr handles no longer live directly inside of the `Task`
structure. This is a consequence of librustrt not knowing about `std::io`.
These two handles are now stored inside of task-local-data.
The handles were originally stored inside of the `Task` for perf reasons, and
TLD is not currently as fast as it could be. For comparison, 100k prints goes
from 59ms to 68ms (a 15% slowdown). This appeared to me to be an acceptable
perf loss for the successful extraction of a librustrt crate.
* The `rtio` module was forced to duplicate more functionality of `std::io`. As
the module no longer depends on `std::io`, `rtio` now defines structures such
as socket addresses, addrinfo fiddly bits, etc. The primary change made was
that `rtio` now defines its own `IoError` type. This type is distinct from
`std::io::IoError` in that it does not have an enum for what error occurred,
but rather a platform-specific error code.
The native and green libraries will be updated in later commits for this
change, and the bulk of this effort was put behind updating the two libraries
for this change (with `rtio`).
* Printing a message on task failure (along with the backtrace) continues to
live in libstd, not in librustrt. This is a consequence of the above decision
to move the stdout/stderr handles to TLD rather than inside the `Task` itself.
The unwinding API now supports registration of global callback functions which
will be invoked when a task fails, allowing for libstd to register a function
to print a message and a backtrace.
The API for registering a callback is experimental and unsafe, as the
ramifications of running code on unwinding is pretty hairy.
* The `std::unstable::mutex` module has moved to `std::rt::mutex`.
* The `std::unstable::sync` module has been moved to `std::rt::exclusive` and
the type has been rewritten to not internally have an Arc and to have an RAII
guard structure when locking. Old code should stop using `Exclusive` in favor
of the primitives in `libsync`, but if necessary, old code should port to
`Arc<Exclusive<T>>`.
* The local heap has been stripped down to have fewer debugging options. None of
these were tested, and none of these have been used in a very long time.
[breaking-change]
2014-06-03 19:11:49 -07:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Indicates unreachable code.
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
|
|
|
/// This is useful any time that the compiler can't determine that some code is unreachable. For
|
|
|
|
/// example:
|
|
|
|
///
|
|
|
|
/// * Match arms with guard conditions.
|
|
|
|
/// * Loops that dynamically terminate.
|
|
|
|
/// * Iterators that dynamically terminate.
|
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// If the determination that the code is unreachable proves incorrect, the
|
2019-05-23 09:04:01 -06:00
|
|
|
/// program immediately terminates with a [`panic!`].
|
|
|
|
///
|
|
|
|
/// The unsafe counterpart of this macro is the [`unreachable_unchecked`] function, which
|
2019-05-23 17:34:10 -06:00
|
|
|
/// will cause undefined behavior if the code is reached.
|
2017-08-29 10:17:33 -07:00
|
|
|
///
|
2019-06-16 01:24:37 +02:00
|
|
|
/// [`panic!`]: ../std/macro.panic.html
|
2018-04-12 21:47:38 +08:00
|
|
|
/// [`unreachable_unchecked`]: ../std/hint/fn.unreachable_unchecked.html
|
|
|
|
/// [`std::hint`]: ../std/hint/index.html
|
2017-08-29 10:17:33 -07:00
|
|
|
///
|
2014-09-15 19:29:47 -07:00
|
|
|
/// # Panics
|
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// This will always [`panic!`]
|
2014-09-15 19:29:47 -07:00
|
|
|
///
|
2017-08-29 10:17:33 -07:00
|
|
|
/// [`panic!`]: ../std/macro.panic.html
|
2019-06-16 01:24:37 +02:00
|
|
|
///
|
2014-09-15 19:29:47 -07:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Match arms:
|
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-11-03 15:27:03 +00:00
|
|
|
/// # #[allow(dead_code)]
|
2015-03-25 17:06:52 -07:00
|
|
|
/// fn foo(x: Option<i32>) {
|
2014-09-15 19:29:47 -07:00
|
|
|
/// match x {
|
|
|
|
/// Some(n) if n >= 0 => println!("Some(Non-negative)"),
|
|
|
|
/// Some(n) if n < 0 => println!("Some(Negative)"),
|
|
|
|
/// Some(_) => unreachable!(), // compile error if commented out
|
|
|
|
/// None => println!("None")
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Iterators:
|
|
|
|
///
|
2015-03-12 22:42:38 -04:00
|
|
|
/// ```
|
2015-11-03 15:27:03 +00:00
|
|
|
/// # #[allow(dead_code)]
|
2014-09-15 19:29:47 -07:00
|
|
|
/// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
|
2015-03-30 11:00:05 -07:00
|
|
|
/// for i in 0.. {
|
2014-09-15 19:29:47 -07:00
|
|
|
/// if 3*i < i { panic!("u32 overflow"); }
|
|
|
|
/// if x < 3*i { return i-1; }
|
|
|
|
/// }
|
|
|
|
/// unreachable!();
|
|
|
|
/// }
|
|
|
|
/// ```
|
2014-06-07 11:13:26 -07:00
|
|
|
#[macro_export]
|
2017-04-18 23:33:38 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-09-15 19:29:47 -07:00
|
|
|
macro_rules! unreachable {
|
|
|
|
() => ({
|
|
|
|
panic!("internal error: entered unreachable code")
|
|
|
|
});
|
|
|
|
($msg:expr) => ({
|
2019-06-07 18:29:48 +03:00
|
|
|
$crate::unreachable!("{}", $msg)
|
2014-09-15 19:29:47 -07:00
|
|
|
});
|
2018-02-07 09:31:22 -05:00
|
|
|
($msg:expr,) => ({
|
2019-06-07 18:29:48 +03:00
|
|
|
$crate::unreachable!($msg)
|
2018-02-07 09:31:22 -05:00
|
|
|
});
|
2014-09-15 19:29:47 -07:00
|
|
|
($fmt:expr, $($arg:tt)*) => ({
|
2019-08-15 21:23:11 +03:00
|
|
|
panic!($crate::concat!("internal error: entered unreachable code: ", $fmt), $($arg)*)
|
2014-09-15 19:29:47 -07:00
|
|
|
});
|
|
|
|
}
|
2014-11-14 09:18:10 -08:00
|
|
|
|
2019-09-23 23:20:55 -04:00
|
|
|
/// Indicates unfinished code by panicking with a message of "not yet implemented".
|
2015-08-27 14:14:03 -04:00
|
|
|
///
|
2019-09-23 23:20:55 -04:00
|
|
|
/// This allows the your code to type-check, which is useful if you are prototyping or
|
|
|
|
/// implementing a trait that requires multiple methods which you don't plan of using all of.
|
2015-08-27 14:14:03 -04:00
|
|
|
///
|
2019-06-16 01:24:37 +02:00
|
|
|
/// There is no difference between `unimplemented!` and `todo!` apart from the
|
|
|
|
/// name.
|
|
|
|
///
|
2017-08-30 22:26:54 -04:00
|
|
|
/// # Panics
|
|
|
|
///
|
2019-09-23 23:20:55 -04:00
|
|
|
/// This will always [panic!](macro.panic.html) because `unimplemented!` is just a
|
|
|
|
/// shorthand for `panic!` with a fixed, specific message.
|
|
|
|
///
|
|
|
|
/// Like `panic!`, this macro has a second form for displaying custom values.
|
2017-08-30 22:26:54 -04:00
|
|
|
///
|
2015-08-27 14:14:03 -04:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Here's an example of some in-progress code. We have a trait `Foo`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// trait Foo {
|
2019-09-23 23:20:55 -04:00
|
|
|
/// fn bar(&self) -> u8;
|
2015-08-27 14:14:03 -04:00
|
|
|
/// fn baz(&self);
|
2019-09-23 23:20:55 -04:00
|
|
|
/// fn qux(&self) -> Result<u64, ()>;
|
2015-08-27 14:14:03 -04:00
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2019-09-23 23:20:55 -04:00
|
|
|
/// We want to implement `Foo` for 'MyStruct', but so far we only know how to
|
|
|
|
/// implement the `bar()` function. `baz()` and `qux()` will still need to be defined
|
|
|
|
/// in our implementation of `Foo`, but we can use `unimplemented!` in their definitions
|
|
|
|
/// to allow our code to compile.
|
|
|
|
///
|
|
|
|
/// In the meantime, we want to have our program stop running once these
|
|
|
|
/// unimplemented functions are reached.
|
2015-08-27 14:14:03 -04:00
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # trait Foo {
|
2019-09-23 23:20:55 -04:00
|
|
|
/// # fn bar(&self) -> u8;
|
2015-11-12 00:47:42 +09:00
|
|
|
/// # fn baz(&self);
|
2019-09-23 23:20:55 -04:00
|
|
|
/// # fn qux(&self) -> Result<u64, ()>;
|
2015-08-27 14:14:03 -04:00
|
|
|
/// # }
|
|
|
|
/// struct MyStruct;
|
|
|
|
///
|
|
|
|
/// impl Foo for MyStruct {
|
2019-09-23 23:20:55 -04:00
|
|
|
/// fn bar(&self) -> u8 {
|
|
|
|
/// 1 + 1
|
2015-08-27 14:14:03 -04:00
|
|
|
/// }
|
|
|
|
///
|
2015-11-12 00:47:42 +09:00
|
|
|
/// fn baz(&self) {
|
2019-09-23 23:20:55 -04:00
|
|
|
/// // We aren't sure how to even start writing baz yet,
|
|
|
|
/// // so we have no logic here at all.
|
|
|
|
/// // This will display "thread 'main' panicked at 'not yet implemented'".
|
2015-08-27 14:14:03 -04:00
|
|
|
/// unimplemented!();
|
|
|
|
/// }
|
2019-09-23 23:20:55 -04:00
|
|
|
///
|
|
|
|
/// fn qux(&self) -> Result<u64, ()> {
|
|
|
|
/// let n = self.bar();
|
|
|
|
/// // We have some logic here,
|
|
|
|
/// // so we can use unimplemented! to display what we have so far.
|
|
|
|
/// // This will display:
|
|
|
|
/// // "thread 'main' panicked at 'not yet implemented: we need to divide by 2'".
|
|
|
|
/// unimplemented!("we need to divide by {}", n);
|
|
|
|
/// }
|
2015-08-27 14:14:03 -04:00
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let s = MyStruct;
|
2015-11-12 00:47:42 +09:00
|
|
|
/// s.bar();
|
2015-08-27 14:14:03 -04:00
|
|
|
/// }
|
|
|
|
/// ```
|
2014-09-15 19:29:47 -07:00
|
|
|
#[macro_export]
|
2017-04-18 23:33:38 +01:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2014-09-15 19:29:47 -07:00
|
|
|
macro_rules! unimplemented {
|
2017-06-07 17:06:55 -07:00
|
|
|
() => (panic!("not yet implemented"));
|
2019-08-15 21:23:11 +03:00
|
|
|
($($arg:tt)+) => (panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
|
2014-09-15 19:29:47 -07:00
|
|
|
}
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Indicates unfinished code.
|
2018-11-29 21:50:49 +03:00
|
|
|
///
|
|
|
|
/// This can be useful if you are prototyping and are just looking to have your
|
2019-06-16 01:24:37 +02:00
|
|
|
/// code typecheck.
|
|
|
|
///
|
|
|
|
/// There is no difference between `unimplemented!` and `todo!` apart from the
|
|
|
|
/// name.
|
2018-11-29 21:50:49 +03:00
|
|
|
///
|
|
|
|
/// # Panics
|
|
|
|
///
|
|
|
|
/// This will always [panic!](macro.panic.html)
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Here's an example of some in-progress code. We have a trait `Foo`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// trait Foo {
|
|
|
|
/// fn bar(&self);
|
|
|
|
/// fn baz(&self);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// We want to implement `Foo` on one of our types, but we also want to work on
|
|
|
|
/// just `bar()` first. In order for our code to compile, we need to implement
|
|
|
|
/// `baz()`, so we can use `todo!`:
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// # trait Foo {
|
|
|
|
/// # fn bar(&self);
|
|
|
|
/// # fn baz(&self);
|
|
|
|
/// # }
|
|
|
|
/// struct MyStruct;
|
|
|
|
///
|
|
|
|
/// impl Foo for MyStruct {
|
|
|
|
/// fn bar(&self) {
|
|
|
|
/// // implementation goes here
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn baz(&self) {
|
|
|
|
/// // let's not worry about implementing baz() for now
|
|
|
|
/// todo!();
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn main() {
|
|
|
|
/// let s = MyStruct;
|
|
|
|
/// s.bar();
|
|
|
|
///
|
|
|
|
/// // we aren't even using baz() yet, so this is fine.
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
#[macro_export]
|
2019-09-04 19:35:57 +02:00
|
|
|
#[stable(feature = "todo_macro", since = "1.39.0")]
|
2018-11-29 21:50:49 +03:00
|
|
|
macro_rules! todo {
|
|
|
|
() => (panic!("not yet implemented"));
|
2019-08-15 21:23:11 +03:00
|
|
|
($($arg:tt)+) => (panic!("not yet implemented: {}", $crate::format_args!($($arg)+)));
|
2018-11-29 21:50:49 +03:00
|
|
|
}
|
|
|
|
|
2019-06-20 11:52:31 +03:00
|
|
|
/// Definitions of built-in macros.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-20 11:52:31 +03:00
|
|
|
/// Most of the macro properties (stability, visibility, etc.) are taken from the source code here,
|
|
|
|
/// with exception of expansion functions transforming macro inputs into outputs,
|
|
|
|
/// those functions are provided by the compiler.
|
|
|
|
pub(crate) mod builtin {
|
2017-05-06 23:26:45 -04:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Causes compilation to fail with the given error message when encountered.
|
2017-05-06 23:26:45 -04:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// This macro should be used when a crate uses a conditional compilation strategy to provide
|
|
|
|
/// better error messages for erroneous conditions. It's the compiler-level form of [`panic!`],
|
2019-08-08 18:05:10 +08:00
|
|
|
/// but emits an error during *compilation* rather than at *runtime*.
|
2019-06-29 17:51:20 +03:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Two such examples are macros and `#[cfg]` environments.
|
|
|
|
///
|
|
|
|
/// Emit better compiler error if a macro is passed invalid values. Without the final branch,
|
|
|
|
/// the compiler would still emit an error, but the error's message would not mention the two
|
|
|
|
/// valid values.
|
|
|
|
///
|
|
|
|
/// ```compile_fail
|
|
|
|
/// macro_rules! give_me_foo_or_bar {
|
|
|
|
/// (foo) => {};
|
|
|
|
/// (bar) => {};
|
|
|
|
/// ($x:ident) => {
|
|
|
|
/// compile_error!("This macro only accepts `foo` or `bar`");
|
|
|
|
/// }
|
|
|
|
/// }
|
2017-05-06 23:26:45 -04:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// give_me_foo_or_bar!(neither);
|
|
|
|
/// // ^ will fail at compile time with message "This macro only accepts `foo` or `bar`"
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Emit compiler error if one of a number of features isn't available.
|
|
|
|
///
|
|
|
|
/// ```compile_fail
|
|
|
|
/// #[cfg(not(any(feature = "foo", feature = "bar")))]
|
|
|
|
/// compile_error!("Either feature \"foo\" or \"bar\" must be enabled for this crate.");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// [`panic!`]: ../std/macro.panic.html
|
2017-07-20 15:50:33 -07:00
|
|
|
#[stable(feature = "compile_error_macro", since = "1.20.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! compile_error {
|
|
|
|
($msg:expr) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($msg:expr,) => ({ /* compiler built-in */ })
|
2018-02-07 10:24:34 -05:00
|
|
|
}
|
2017-05-06 23:26:45 -04:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Constructs parameters for the other string-formatting macros.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// This macro functions by taking a formatting string literal containing
|
|
|
|
/// `{}` for each additional argument passed. `format_args!` prepares the
|
|
|
|
/// additional parameters to ensure the output can be interpreted as a string
|
|
|
|
/// and canonicalizes the arguments into a single type. Any value that implements
|
|
|
|
/// the [`Display`] trait can be passed to `format_args!`, as can any
|
|
|
|
/// [`Debug`] implementation be passed to a `{:?}` within the formatting string.
|
|
|
|
///
|
|
|
|
/// This macro produces a value of type [`fmt::Arguments`]. This value can be
|
|
|
|
/// passed to the macros within [`std::fmt`] for performing useful redirection.
|
|
|
|
/// All other formatting macros ([`format!`], [`write!`], [`println!`], etc) are
|
|
|
|
/// proxied through this one. `format_args!`, unlike its derived macros, avoids
|
|
|
|
/// heap allocations.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// You can use the [`fmt::Arguments`] value that `format_args!` returns
|
|
|
|
/// in `Debug` and `Display` contexts as seen below. The example also shows
|
|
|
|
/// that `Debug` and `Display` format to the same thing: the interpolated
|
|
|
|
/// format string in `format_args!`.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// let debug = format!("{:?}", format_args!("{} foo {:?}", 1, 2));
|
|
|
|
/// let display = format!("{}", format_args!("{} foo {:?}", 1, 2));
|
|
|
|
/// assert_eq!("1 foo 2", display);
|
|
|
|
/// assert_eq!(display, debug);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// For more information, see the documentation in [`std::fmt`].
|
|
|
|
///
|
|
|
|
/// [`Display`]: ../std/fmt/trait.Display.html
|
|
|
|
/// [`Debug`]: ../std/fmt/trait.Debug.html
|
|
|
|
/// [`fmt::Arguments`]: ../std/fmt/struct.Arguments.html
|
|
|
|
/// [`std::fmt`]: ../std/fmt/index.html
|
|
|
|
/// [`format!`]: ../std/macro.format.html
|
|
|
|
/// [`write!`]: ../std/macro.write.html
|
|
|
|
/// [`println!`]: ../std/macro.println.html
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use std::fmt;
|
|
|
|
///
|
|
|
|
/// let s = fmt::format(format_args!("hello {}", "world"));
|
|
|
|
/// assert_eq!(s, format!("hello {}", "world"));
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:51:20 +03:00
|
|
|
#[allow_internal_unstable(fmt_internals)]
|
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! format_args {
|
|
|
|
($fmt:expr) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Same as `format_args`, but adds a newline in the end.
|
|
|
|
#[unstable(feature = "format_args_nl", issue = "0",
|
|
|
|
reason = "`format_args_nl` is only for internal \
|
|
|
|
language use and is subject to change")]
|
|
|
|
#[allow_internal_unstable(fmt_internals)]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! format_args_nl {
|
|
|
|
($fmt:expr) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($fmt:expr, $($args:tt)*) => ({ /* compiler built-in */ })
|
2017-11-26 19:59:21 -05:00
|
|
|
}
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Inspects an environment variable at compile time.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// This macro will expand to the value of the named environment variable at
|
|
|
|
/// compile time, yielding an expression of type `&'static str`.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// If the environment variable is not defined, then a compilation error
|
|
|
|
/// will be emitted. To not emit a compile error, use the [`option_env!`]
|
|
|
|
/// macro instead.
|
|
|
|
///
|
|
|
|
/// [`option_env!`]: ../std/macro.option_env.html
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let path: &'static str = env!("PATH");
|
|
|
|
/// println!("the $PATH variable at the time of compiling was: {}", path);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// You can customize the error message by passing a string as the second
|
|
|
|
/// parameter:
|
|
|
|
///
|
|
|
|
/// ```compile_fail
|
|
|
|
/// let doc: &'static str = env!("documentation", "what's that?!");
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// If the `documentation` environment variable is not defined, you'll get
|
|
|
|
/// the following error:
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// error: what's that?!
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! env {
|
|
|
|
($name:expr) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($name:expr,) => ({ /* compiler built-in */ })
|
2017-11-26 19:59:21 -05:00
|
|
|
}
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Optionally inspects an environment variable at compile time.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// If the named environment variable is present at compile time, this will
|
|
|
|
/// expand into an expression of type `Option<&'static str>` whose value is
|
|
|
|
/// `Some` of the value of the environment variable. If the environment
|
|
|
|
/// variable is not present, then this will expand to `None`. See
|
|
|
|
/// [`Option<T>`][option] for more information on this type.
|
|
|
|
///
|
|
|
|
/// A compile time error is never emitted when using this macro regardless
|
|
|
|
/// of whether the environment variable is present or not.
|
|
|
|
///
|
|
|
|
/// [option]: ../std/option/enum.Option.html
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let key: Option<&'static str> = option_env!("SECRET_KEY");
|
|
|
|
/// println!("the secret key might be: {:?}", key);
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! option_env {
|
|
|
|
($name:expr) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($name:expr,) => ({ /* compiler built-in */ })
|
2018-02-07 10:24:34 -05:00
|
|
|
}
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Concatenates identifiers into one identifier.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// This macro takes any number of comma-separated identifiers, and
|
|
|
|
/// concatenates them all into one, yielding an expression which is a new
|
|
|
|
/// identifier. Note that hygiene makes it such that this macro cannot
|
|
|
|
/// capture local variables. Also, as a general rule, macros are only
|
|
|
|
/// allowed in item, statement or expression position. That means while
|
|
|
|
/// you may use this macro for referring to existing variables, functions or
|
|
|
|
/// modules etc, you cannot define a new one with it.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// #![feature(concat_idents)]
|
|
|
|
///
|
|
|
|
/// # fn main() {
|
|
|
|
/// fn foobar() -> u32 { 23 }
|
|
|
|
///
|
|
|
|
/// let f = concat_idents!(foo, bar);
|
|
|
|
/// println!("{}", f());
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// // fn concat_idents!(new, fun, name) { } // not usable in this way!
|
|
|
|
/// # }
|
|
|
|
/// ```
|
|
|
|
#[unstable(feature = "concat_idents", issue = "29599",
|
|
|
|
reason = "`concat_idents` is not stable enough for use and is subject to change")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! concat_idents {
|
|
|
|
($($e:ident),+) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($($e:ident,)+) => ({ /* compiler built-in */ })
|
2016-10-21 17:23:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Concatenates literals into a static string slice.
|
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// This macro takes any number of comma-separated literals, yielding an
|
|
|
|
/// expression of type `&'static str` which represents all of the literals
|
|
|
|
/// concatenated left-to-right.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// Integer and floating point literals are stringified in order to be
|
|
|
|
/// concatenated.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let s = concat!("test", 10, 'b', true);
|
|
|
|
/// assert_eq!(s, "test10btrue");
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! concat {
|
|
|
|
($($e:expr),*) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($($e:expr,)*) => ({ /* compiler built-in */ })
|
2017-11-26 19:59:21 -05:00
|
|
|
}
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Expands to the line number on which it was invoked.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// With [`column!`] and [`file!`], these macros provide debugging information for
|
|
|
|
/// developers about the location within the source.
|
|
|
|
///
|
|
|
|
/// The expanded expression has type `u32` and is 1-based, so the first line
|
|
|
|
/// in each file evaluates to 1, the second to 2, etc. This is consistent
|
|
|
|
/// with error messages by common compilers or popular editors.
|
|
|
|
/// The returned line is *not necessarily* the line of the `line!` invocation itself,
|
|
|
|
/// but rather the first macro invocation leading up to the invocation
|
|
|
|
/// of the `line!` macro.
|
|
|
|
///
|
|
|
|
/// [`column!`]: macro.column.html
|
|
|
|
/// [`file!`]: macro.file.html
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let current_line = line!();
|
|
|
|
/// println!("defined on line: {}", current_line);
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! line { () => { /* compiler built-in */ } }
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-06-29 17:51:20 +03:00
|
|
|
/// Expands to the column number at which it was invoked.
|
|
|
|
///
|
|
|
|
/// With [`line!`] and [`file!`], these macros provide debugging information for
|
|
|
|
/// developers about the location within the source.
|
|
|
|
///
|
|
|
|
/// The expanded expression has type `u32` and is 1-based, so the first column
|
|
|
|
/// in each line evaluates to 1, the second to 2, etc. This is consistent
|
|
|
|
/// with error messages by common compilers or popular editors.
|
|
|
|
/// The returned column is *not necessarily* the line of the `column!` invocation itself,
|
|
|
|
/// but rather the first macro invocation leading up to the invocation
|
|
|
|
/// of the `column!` macro.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// [`line!`]: macro.line.html
|
|
|
|
/// [`file!`]: macro.file.html
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let current_col = column!();
|
|
|
|
/// println!("defined on column: {}", current_col);
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! column { () => { /* compiler built-in */ } }
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-06-29 17:51:20 +03:00
|
|
|
/// Expands to the file name in which it was invoked.
|
|
|
|
///
|
|
|
|
/// With [`line!`] and [`column!`], these macros provide debugging information for
|
|
|
|
/// developers about the location within the source.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// The expanded expression has type `&'static str`, and the returned file
|
|
|
|
/// is not the invocation of the `file!` macro itself, but rather the
|
|
|
|
/// first macro invocation leading up to the invocation of the `file!`
|
|
|
|
/// macro.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// [`line!`]: macro.line.html
|
|
|
|
/// [`column!`]: macro.column.html
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let this_file = file!();
|
|
|
|
/// println!("defined in file: {}", this_file);
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! file { () => { /* compiler built-in */ } }
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Stringifies its arguments.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// This macro will yield an expression of type `&'static str` which is the
|
|
|
|
/// stringification of all the tokens passed to the macro. No restrictions
|
|
|
|
/// are placed on the syntax of the macro invocation itself.
|
|
|
|
///
|
|
|
|
/// Note that the expanded results of the input tokens may change in the
|
|
|
|
/// future. You should be careful if you rely on the output.
|
|
|
|
///
|
|
|
|
/// # Examples
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// ```
|
|
|
|
/// let one_plus_one = stringify!(1 + 1);
|
|
|
|
/// assert_eq!(one_plus_one, "1 + 1");
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! stringify { ($($t:tt)*) => { /* compiler built-in */ } }
|
2016-10-21 17:23:50 +03:00
|
|
|
|
|
|
|
/// Includes a utf8-encoded file as a string.
|
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// The file is located relative to the current file. (similarly to how
|
|
|
|
/// modules are found)
|
|
|
|
///
|
|
|
|
/// This macro will yield an expression of type `&'static str` which is the
|
|
|
|
/// contents of the file.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Assume there are two files in the same directory with the following
|
|
|
|
/// contents:
|
|
|
|
///
|
|
|
|
/// File 'spanish.in':
|
|
|
|
///
|
|
|
|
/// ```text
|
|
|
|
/// adiós
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// File 'main.rs':
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// ```ignore (cannot-doctest-external-file-dependency)
|
|
|
|
/// fn main() {
|
|
|
|
/// let my_str = include_str!("spanish.in");
|
|
|
|
/// assert_eq!(my_str, "adiós\n");
|
|
|
|
/// print!("{}", my_str);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Compiling 'main.rs' and running the resulting binary will print "adiós".
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! include_str {
|
|
|
|
($file:expr) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($file:expr,) => ({ /* compiler built-in */ })
|
2018-02-07 10:24:34 -05:00
|
|
|
}
|
2016-10-21 17:23:50 +03:00
|
|
|
|
|
|
|
/// Includes a file as a reference to a byte array.
|
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// The file is located relative to the current file. (similarly to how
|
|
|
|
/// modules are found)
|
|
|
|
///
|
|
|
|
/// This macro will yield an expression of type `&'static [u8; N]` which is
|
|
|
|
/// the contents of the file.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Assume there are two files in the same directory with the following
|
|
|
|
/// contents:
|
|
|
|
///
|
|
|
|
/// File 'spanish.in':
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// ```text
|
|
|
|
/// adiós
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// File 'main.rs':
|
|
|
|
///
|
|
|
|
/// ```ignore (cannot-doctest-external-file-dependency)
|
|
|
|
/// fn main() {
|
|
|
|
/// let bytes = include_bytes!("spanish.in");
|
|
|
|
/// assert_eq!(bytes, b"adi\xc3\xb3s\n");
|
|
|
|
/// print!("{}", String::from_utf8_lossy(bytes));
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Compiling 'main.rs' and running the resulting binary will print "adiós".
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! include_bytes {
|
|
|
|
($file:expr) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($file:expr,) => ({ /* compiler built-in */ })
|
2018-02-07 10:24:34 -05:00
|
|
|
}
|
2016-10-21 17:23:50 +03:00
|
|
|
|
|
|
|
/// Expands to a string that represents the current module path.
|
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// The current module path can be thought of as the hierarchy of modules
|
|
|
|
/// leading back up to the crate root. The first component of the path
|
|
|
|
/// returned is the name of the crate currently being compiled.
|
|
|
|
///
|
|
|
|
/// # Examples
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// ```
|
|
|
|
/// mod test {
|
|
|
|
/// pub fn foo() {
|
|
|
|
/// assert!(module_path!().ends_with("test"));
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// test::foo();
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! module_path { () => { /* compiler built-in */ } }
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-06-29 17:51:20 +03:00
|
|
|
/// Evaluates boolean combinations of configuration flags at compile-time.
|
|
|
|
///
|
|
|
|
/// In addition to the `#[cfg]` attribute, this macro is provided to allow
|
|
|
|
/// boolean expression evaluation of configuration flags. This frequently
|
|
|
|
/// leads to less duplicated code.
|
|
|
|
///
|
|
|
|
/// The syntax given to this macro is the same syntax as the [`cfg`]
|
|
|
|
/// attribute.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// [`cfg`]: ../reference/conditional-compilation.html#the-cfg-attribute
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// let my_directory = if cfg!(windows) {
|
|
|
|
/// "windows-specific-directory"
|
|
|
|
/// } else {
|
|
|
|
/// "unix-directory"
|
|
|
|
/// };
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! cfg { ($($cfg:tt)*) => { /* compiler built-in */ } }
|
2016-10-21 17:23:50 +03:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Parses a file as an expression or an item according to the context.
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// The file is located relative to the current file (similarly to how
|
|
|
|
/// modules are found).
|
|
|
|
///
|
|
|
|
/// Using this macro is often a bad idea, because if the file is
|
|
|
|
/// parsed as an expression, it is going to be placed in the
|
|
|
|
/// surrounding code unhygienically. This could result in variables
|
|
|
|
/// or functions being different from what the file expected if
|
|
|
|
/// there are variables or functions that have the same name in
|
|
|
|
/// the current file.
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// Assume there are two files in the same directory with the following
|
|
|
|
/// contents:
|
|
|
|
///
|
|
|
|
/// File 'monkeys.in':
|
|
|
|
///
|
|
|
|
/// ```ignore (only-for-syntax-highlight)
|
|
|
|
/// ['🙈', '🙊', '🙉']
|
|
|
|
/// .iter()
|
|
|
|
/// .cycle()
|
|
|
|
/// .take(6)
|
|
|
|
/// .collect::<String>()
|
|
|
|
/// ```
|
2016-10-21 17:23:50 +03:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// File 'main.rs':
|
|
|
|
///
|
|
|
|
/// ```ignore (cannot-doctest-external-file-dependency)
|
|
|
|
/// fn main() {
|
|
|
|
/// let my_string = include!("monkeys.in");
|
|
|
|
/// assert_eq!("🙈🙊🙉🙈🙊🙉", my_string);
|
|
|
|
/// println!("{}", my_string);
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// Compiling 'main.rs' and running the resulting binary will print
|
|
|
|
/// "🙈🙊🙉🙈🙊🙉".
|
2016-10-21 17:23:50 +03:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:30:51 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! include {
|
|
|
|
($file:expr) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($file:expr,) => ({ /* compiler built-in */ })
|
2018-02-07 10:24:34 -05:00
|
|
|
}
|
2018-03-07 16:13:15 +09:00
|
|
|
|
2019-03-26 18:22:25 -04:00
|
|
|
/// Asserts that a boolean expression is `true` at runtime.
|
2018-03-07 16:13:15 +09:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// This will invoke the [`panic!`] macro if the provided expression cannot be
|
|
|
|
/// evaluated to `true` at runtime.
|
2018-03-07 16:13:15 +09:00
|
|
|
///
|
2019-06-29 17:51:20 +03:00
|
|
|
/// # Uses
|
|
|
|
///
|
|
|
|
/// Assertions are always checked in both debug and release builds, and cannot
|
|
|
|
/// be disabled. See [`debug_assert!`] for assertions that are not enabled in
|
|
|
|
/// release builds by default.
|
|
|
|
///
|
|
|
|
/// Unsafe code relies on `assert!` to enforce run-time invariants that, if
|
|
|
|
/// violated could lead to unsafety.
|
|
|
|
///
|
|
|
|
/// Other use-cases of `assert!` include testing and enforcing run-time
|
|
|
|
/// invariants in safe code (whose violation cannot result in unsafety).
|
|
|
|
///
|
|
|
|
/// # Custom Messages
|
|
|
|
///
|
|
|
|
/// This macro has a second form, where a custom panic message can
|
|
|
|
/// be provided with or without arguments for formatting. See [`std::fmt`]
|
|
|
|
/// for syntax for this form.
|
|
|
|
///
|
|
|
|
/// [`panic!`]: macro.panic.html
|
|
|
|
/// [`debug_assert!`]: macro.debug_assert.html
|
|
|
|
/// [`std::fmt`]: ../std/fmt/index.html
|
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// // the panic message for these assertions is the stringified value of the
|
|
|
|
/// // expression given.
|
|
|
|
/// assert!(true);
|
|
|
|
///
|
|
|
|
/// fn some_computation() -> bool { true } // a very simple function
|
|
|
|
///
|
|
|
|
/// assert!(some_computation());
|
|
|
|
///
|
|
|
|
/// // assert with a custom message
|
|
|
|
/// let x = true;
|
|
|
|
/// assert!(x, "x wasn't true!");
|
|
|
|
///
|
|
|
|
/// let a = 3; let b = 27;
|
|
|
|
/// assert!(a + b == 30, "a = {}, b = {}", a, b);
|
|
|
|
/// ```
|
2018-03-07 16:13:15 +09:00
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-29 17:51:20 +03:00
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! assert {
|
|
|
|
($cond:expr) => ({ /* compiler built-in */ });
|
|
|
|
($cond:expr,) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
($cond:expr, $($arg:tt)+) => ({ /* compiler built-in */ })
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Inline assembly.
|
2019-11-08 15:58:25 +00:00
|
|
|
///
|
|
|
|
/// Read the [unstable book] for the usage.
|
|
|
|
///
|
|
|
|
/// [unstable book]: ../unstable-book/library-features/asm.html
|
2019-06-29 17:51:20 +03:00
|
|
|
#[unstable(feature = "asm", issue = "29722",
|
|
|
|
reason = "inline assembly is not stable enough for use and is subject to change")]
|
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! asm { ("assembly template"
|
|
|
|
: $("output"(operand),)*
|
|
|
|
: $("input"(operand),)*
|
|
|
|
: $("clobbers",)*
|
|
|
|
: $("options",)*) => { /* compiler built-in */ } }
|
2019-06-29 17:51:20 +03:00
|
|
|
|
|
|
|
/// Module-level inline assembly.
|
|
|
|
#[unstable(feature = "global_asm", issue = "35119",
|
|
|
|
reason = "`global_asm!` is not stable enough for use and is subject to change")]
|
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! global_asm { ("assembly") => { /* compiler built-in */ } }
|
2019-06-29 17:51:20 +03:00
|
|
|
|
|
|
|
/// Prints passed tokens into the standard output.
|
|
|
|
#[unstable(feature = "log_syntax", issue = "29598",
|
|
|
|
reason = "`log_syntax!` is not stable enough for use and is subject to change")]
|
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! log_syntax { ($($arg:tt)*) => { /* compiler built-in */ } }
|
2019-06-29 17:51:20 +03:00
|
|
|
|
|
|
|
/// Enables or disables tracing functionality used for debugging other macros.
|
|
|
|
#[unstable(feature = "trace_macros", issue = "29598",
|
|
|
|
reason = "`trace_macros` is not stable enough for use and is subject to change")]
|
|
|
|
#[rustc_builtin_macro]
|
2019-07-28 01:51:21 +03:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! trace_macros {
|
|
|
|
(true) => ({ /* compiler built-in */ });
|
2019-06-29 17:51:20 +03:00
|
|
|
(false) => ({ /* compiler built-in */ })
|
2018-03-07 16:13:15 +09:00
|
|
|
}
|
2019-06-29 17:51:20 +03:00
|
|
|
|
|
|
|
/// Attribute macro applied to a function to turn it into a unit test.
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
2019-06-20 02:33:39 +03:00
|
|
|
#[allow_internal_unstable(test, rustc_attrs)]
|
2019-06-29 17:51:20 +03:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
pub macro test($item:item) { /* compiler built-in */ }
|
|
|
|
|
|
|
|
/// Attribute macro applied to a function to turn it into a benchmark test.
|
2019-09-25 08:42:46 -04:00
|
|
|
#[unstable(soft, feature = "test", issue = "50297",
|
|
|
|
reason = "`bench` is a part of custom test frameworks which are unstable")]
|
2019-06-20 02:33:39 +03:00
|
|
|
#[allow_internal_unstable(test, rustc_attrs)]
|
2019-06-29 17:51:20 +03:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
pub macro bench($item:item) { /* compiler built-in */ }
|
|
|
|
|
|
|
|
/// An implementation detail of the `#[test]` and `#[bench]` macros.
|
|
|
|
#[unstable(feature = "custom_test_frameworks", issue = "50297",
|
|
|
|
reason = "custom test frameworks are an unstable feature")]
|
2019-06-20 02:33:39 +03:00
|
|
|
#[allow_internal_unstable(test, rustc_attrs)]
|
2019-06-29 17:51:20 +03:00
|
|
|
#[rustc_builtin_macro]
|
|
|
|
pub macro test_case($item:item) { /* compiler built-in */ }
|
|
|
|
|
2019-07-19 00:24:58 +03:00
|
|
|
/// Attribute macro applied to a static to register it as a global allocator.
|
|
|
|
#[stable(feature = "global_allocator", since = "1.28.0")]
|
|
|
|
#[allow_internal_unstable(rustc_attrs)]
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
pub macro global_allocator($item:item) { /* compiler built-in */ }
|
|
|
|
|
2019-06-29 17:51:20 +03:00
|
|
|
/// Unstable implementation detail of the `rustc` compiler, do not use.
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
#[allow_internal_unstable(core_intrinsics, libstd_sys_internals)]
|
|
|
|
pub macro RustcDecodable($item:item) { /* compiler built-in */ }
|
|
|
|
|
|
|
|
/// Unstable implementation detail of the `rustc` compiler, do not use.
|
|
|
|
#[rustc_builtin_macro]
|
|
|
|
#[stable(feature = "rust1", since = "1.0.0")]
|
|
|
|
#[allow_internal_unstable(core_intrinsics)]
|
|
|
|
pub macro RustcEncodable($item:item) { /* compiler built-in */ }
|
2018-08-12 22:17:30 -04:00
|
|
|
}
|