macros.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. macro_rules! test_println {
  2. ($($arg:tt)*) => {
  3. if cfg!(test) || cfg!(all(thingbuf_trace, feature = "std")) {
  4. if crate::util::panic::panicking() {
  5. // getting the thread ID while panicking doesn't seem to play super nicely with loom's
  6. // mock lazy_static...
  7. println!("[PANIC {:>30}:{:<3}] {}", file!(), line!(), format_args!($($arg)*))
  8. } else {
  9. crate::loom::traceln(format_args!(
  10. "[{:?} {:>30}:{:<3}] {}",
  11. crate::loom::thread::current().id(),
  12. file!(),
  13. line!(),
  14. format_args!($($arg)*),
  15. ));
  16. }
  17. }
  18. }
  19. }
  20. macro_rules! test_dbg {
  21. ($e:expr) => {
  22. match $e {
  23. e => {
  24. #[cfg(test)]
  25. test_println!("{} = {:?}", stringify!($e), &e);
  26. e
  27. }
  28. }
  29. };
  30. }
  31. macro_rules! feature {
  32. (
  33. #![$meta:meta]
  34. $($item:item)*
  35. ) => {
  36. $(
  37. #[cfg($meta)]
  38. #[cfg_attr(docsrs, doc(cfg($meta)))]
  39. $item
  40. )*
  41. }
  42. }
  43. #[allow(unused_macros)]
  44. macro_rules! unreachable_unchecked {
  45. ($($arg:tt)+) => {
  46. crate::unreachable_unchecked!(@inner , format_args!(": {}", format_args!($($arg)*)))
  47. };
  48. () => {
  49. crate::unreachable_unchecked!(@inner ".")
  50. };
  51. (@inner $msg:expr) => {
  52. #[cfg(debug_assertions)] {
  53. panic!(
  54. "internal error: entered unreachable code{}\n\n\
  55. /!\\ EXTREMELY SERIOUS WARNING /!\\\n
  56. This code should NEVER be entered; in release mode, this would \
  57. have been an `unreachable_unchecked` hint. The fact that this \
  58. occurred means something VERY bad is going on. \n\
  59. Please contact the `thingbuf` maintainers immediately. Sorry!",
  60. $msg,
  61. );
  62. }
  63. #[cfg(not(debug_assertions))]
  64. unsafe {
  65. core::hint::unreachable_unchecked();
  66. }
  67. };
  68. }