macros.rs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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(any(test, all(thingbuf_trace, feature = "std")))]
  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. macro_rules! fmt_bits {
  44. ($self: expr, $f: expr, $has_states: ident, $($name: ident),+) => {
  45. $(
  46. if $self.contains(Self::$name) {
  47. if $has_states {
  48. $f.write_str(" | ")?;
  49. }
  50. $f.write_str(stringify!($name))?;
  51. $has_states = true;
  52. }
  53. )+
  54. };
  55. }
  56. #[allow(unused_macros)]
  57. macro_rules! unreachable_unchecked {
  58. ($($arg:tt)+) => {
  59. crate::unreachable_unchecked!(@inner , format_args!(": {}", format_args!($($arg)*)))
  60. };
  61. () => {
  62. crate::unreachable_unchecked!(@inner ".")
  63. };
  64. (@inner $msg:expr) => {
  65. #[cfg(debug_assertions)] {
  66. panic!(
  67. "internal error: entered unreachable code{}\n\n\
  68. /!\\ EXTREMELY SERIOUS WARNING /!\\\n
  69. This code should NEVER be entered; in release mode, this would \
  70. have been an `unreachable_unchecked` hint. The fact that this \
  71. occurred means something VERY bad is going on. \n\
  72. Please contact the `thingbuf` maintainers immediately. Sorry!",
  73. $msg,
  74. );
  75. }
  76. #[cfg(not(debug_assertions))]
  77. unsafe {
  78. core::hint::unreachable_unchecked();
  79. }
  80. };
  81. }