|
|
@ -28,7 +28,6 @@ |
|
|
|
/// panic!("this is a {} {message}", "fancy", message = "message");
|
|
|
|
/// panic!("this is a {} {message}", "fancy", message = "message");
|
|
|
|
/// ```
|
|
|
|
/// ```
|
|
|
|
#[macro_export] |
|
|
|
#[macro_export] |
|
|
|
#[allow_internal_unstable] |
|
|
|
|
|
|
|
macro_rules! panic { |
|
|
|
macro_rules! panic { |
|
|
|
() => ({ |
|
|
|
() => ({ |
|
|
|
panic!("explicit panic") |
|
|
|
panic!("explicit panic") |
|
|
@ -52,132 +51,6 @@ macro_rules! panic { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Ensure that a boolean expression is `true` at runtime.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// This will invoke the `panic!` macro if the provided expression cannot be
|
|
|
|
|
|
|
|
/// evaluated to `true` at runtime.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// This macro has a second version, where a custom panic message can be provided.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # 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);
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
|
|
|
macro_rules! assert { |
|
|
|
|
|
|
|
($cond:expr) => ( |
|
|
|
|
|
|
|
if !$cond { |
|
|
|
|
|
|
|
panic!(concat!("assertion failed: ", stringify!($cond))) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
($cond:expr, $($arg:tt)+) => ( |
|
|
|
|
|
|
|
if !$cond { |
|
|
|
|
|
|
|
panic!($($arg)+) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Asserts that two expressions are equal to each other.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// On panic, this macro will print the values of the expressions with their
|
|
|
|
|
|
|
|
/// debug representations.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// let a = 3;
|
|
|
|
|
|
|
|
/// let b = 1 + 2;
|
|
|
|
|
|
|
|
/// assert_eq!(a, b);
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
|
|
|
macro_rules! assert_eq { |
|
|
|
|
|
|
|
($left:expr , $right:expr) => ({ |
|
|
|
|
|
|
|
match (&$left, &$right) { |
|
|
|
|
|
|
|
(left_val, right_val) => { |
|
|
|
|
|
|
|
if !(*left_val == *right_val) { |
|
|
|
|
|
|
|
panic!("assertion failed: `(left == right)` \ |
|
|
|
|
|
|
|
(left: `{:?}`, right: `{:?}`)", left_val, right_val) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Ensure that a boolean expression is `true` at runtime.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// This will invoke the `panic!` macro if the provided expression cannot be
|
|
|
|
|
|
|
|
/// evaluated to `true` at runtime.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Like `assert!`, this macro also has a second version, where a custom panic
|
|
|
|
|
|
|
|
/// message can be provided.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Unlike `assert!`, `debug_assert!` statements are only enabled in non
|
|
|
|
|
|
|
|
/// optimized builds by default. An optimized build will omit all
|
|
|
|
|
|
|
|
/// `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
|
|
|
|
|
|
|
|
/// development.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// // the panic message for these assertions is the stringified value of the
|
|
|
|
|
|
|
|
/// // expression given.
|
|
|
|
|
|
|
|
/// debug_assert!(true);
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// fn some_expensive_computation() -> bool { true } // a very simple function
|
|
|
|
|
|
|
|
/// debug_assert!(some_expensive_computation());
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// // assert with a custom message
|
|
|
|
|
|
|
|
/// let x = true;
|
|
|
|
|
|
|
|
/// debug_assert!(x, "x wasn't true!");
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// let a = 3; let b = 27;
|
|
|
|
|
|
|
|
/// debug_assert!(a + b == 30, "a = {}, b = {}", a, b);
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
|
|
|
macro_rules! debug_assert { |
|
|
|
|
|
|
|
($($arg:tt)*) => (if cfg!(debug_assertions) { assert!($($arg)*); }) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Asserts that two expressions are equal to each other.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// On panic, this macro will print the values of the expressions with their
|
|
|
|
|
|
|
|
/// debug representations.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Unlike `assert_eq!`, `debug_assert_eq!` statements are only enabled in non
|
|
|
|
|
|
|
|
/// optimized builds by default. An optimized build will omit all
|
|
|
|
|
|
|
|
/// `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
|
|
|
|
|
|
|
|
/// development.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// let a = 3;
|
|
|
|
|
|
|
|
/// let b = 1 + 2;
|
|
|
|
|
|
|
|
/// debug_assert_eq!(a, b);
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
|
|
|
macro_rules! debug_assert_eq { |
|
|
|
|
|
|
|
($($arg:tt)*) => (if cfg!(debug_assertions) { assert_eq!($($arg)*); }) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Helper macro for unwrapping `Result` values while returning early with an
|
|
|
|
/// Helper macro for unwrapping `Result` values while returning early with an
|
|
|
|
/// error if the value of the expression is `Err`. Can only be used in
|
|
|
|
/// error if the value of the expression is `Err`. Can only be used in
|
|
|
|
/// functions that return `Result` because of the early return of `Err` that
|
|
|
|
/// functions that return `Result` because of the early return of `Err` that
|
|
|
@ -217,169 +90,7 @@ macro_rules! try { |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Use the `format!` syntax to write data into a buffer.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// This macro is typically used with a buffer of `&mut `[`Write`][write].
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// See [`std::fmt`][fmt] for more information on format syntax.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// [fmt]: ../std/fmt/index.html
|
|
|
|
|
|
|
|
/// [write]: ../std/io/trait.Write.html
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// use std::io::Write;
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// let mut w = Vec::new();
|
|
|
|
|
|
|
|
/// write!(&mut w, "test").unwrap();
|
|
|
|
|
|
|
|
/// write!(&mut w, "formatted {}", "arguments").unwrap();
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// assert_eq!(w, b"testformatted arguments");
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
|
|
|
macro_rules! write { |
|
|
|
|
|
|
|
($dst:expr, $($arg:tt)*) => ($dst.write_fmt(format_args!($($arg)*))) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Use the `format!` syntax to write data into a buffer, appending a newline.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// This macro is typically used with a buffer of `&mut `[`Write`][write].
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// See [`std::fmt`][fmt] for more information on format syntax.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// [fmt]: ../std/fmt/index.html
|
|
|
|
|
|
|
|
/// [write]: ../std/io/trait.Write.html
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// use std::io::Write;
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// let mut w = Vec::new();
|
|
|
|
|
|
|
|
/// writeln!(&mut w, "test").unwrap();
|
|
|
|
|
|
|
|
/// writeln!(&mut w, "formatted {}", "arguments").unwrap();
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// assert_eq!(&w[..], "test\nformatted arguments\n".as_bytes());
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
|
|
|
macro_rules! writeln { |
|
|
|
|
|
|
|
($dst:expr, $fmt:expr) => ( |
|
|
|
|
|
|
|
write!($dst, concat!($fmt, "\n")) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
($dst:expr, $fmt:expr, $($arg:tt)*) => ( |
|
|
|
|
|
|
|
write!($dst, concat!($fmt, "\n"), $($arg)*) |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A utility macro for indicating unreachable code.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// 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.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Panics
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// This will always panic.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # Examples
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// Match arms:
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// # #[allow(dead_code)]
|
|
|
|
|
|
|
|
/// fn foo(x: Option<i32>) {
|
|
|
|
|
|
|
|
/// 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:
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// # #[allow(dead_code)]
|
|
|
|
|
|
|
|
/// fn divide_by_three(x: u32) -> u32 { // one of the poorest implementations of x/3
|
|
|
|
|
|
|
|
/// for i in 0.. {
|
|
|
|
|
|
|
|
/// if 3*i < i { panic!("u32 overflow"); }
|
|
|
|
|
|
|
|
/// if x < 3*i { return i-1; }
|
|
|
|
|
|
|
|
/// }
|
|
|
|
|
|
|
|
/// unreachable!();
|
|
|
|
|
|
|
|
/// }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
|
|
|
macro_rules! unreachable { |
|
|
|
|
|
|
|
() => ({ |
|
|
|
|
|
|
|
panic!("internal error: entered unreachable code") |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
($msg:expr) => ({ |
|
|
|
|
|
|
|
unreachable!("{}", $msg) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
($fmt:expr, $($arg:tt)*) => ({ |
|
|
|
|
|
|
|
panic!(concat!("internal error: entered unreachable code: ", $fmt), $($arg)*) |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// A standardized placeholder for marking unfinished code. It panics with the
|
|
|
|
|
|
|
|
/// message `"not yet implemented"` when executed.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// This can be useful if you are prototyping and are just looking to have your
|
|
|
|
|
|
|
|
/// code typecheck, or if you're implementing a trait that requires multiple
|
|
|
|
|
|
|
|
/// methods, and you're only planning on using one of them.
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// # 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 `unimplemented!`:
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
/// # 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
|
|
|
|
|
|
|
|
/// unimplemented!();
|
|
|
|
|
|
|
|
/// }
|
|
|
|
|
|
|
|
/// }
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// fn main() {
|
|
|
|
|
|
|
|
/// let s = MyStruct;
|
|
|
|
|
|
|
|
/// s.bar();
|
|
|
|
|
|
|
|
///
|
|
|
|
|
|
|
|
/// // we aren't even using baz() yet, so this is fine.
|
|
|
|
|
|
|
|
/// }
|
|
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
|
|
|
macro_rules! unimplemented { |
|
|
|
|
|
|
|
() => (panic!("not yet implemented")) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
#[macro_export] |
|
|
|
#[allow_internal_unstable] |
|
|
|
|
|
|
|
macro_rules! print { |
|
|
|
macro_rules! print { |
|
|
|
($($arg:tt)*) => ( |
|
|
|
($($arg:tt)*) => ( |
|
|
|
$crate::io::_print(format_args!($($arg)*)); |
|
|
|
$crate::io::_print(format_args!($($arg)*)); |
|
|
|