static_assert.rs 879 B

12345678910111213141516171819202122232425262728293031323334
  1. // SPDX-License-Identifier: GPL-2.0
  2. //! Static assert.
  3. /// Static assert (i.e. compile-time assert).
  4. ///
  5. /// Similar to C11 [`_Static_assert`] and C++11 [`static_assert`].
  6. ///
  7. /// The feature may be added to Rust in the future: see [RFC 2790].
  8. ///
  9. /// [`_Static_assert`]: https://en.cppreference.com/w/c/language/_Static_assert
  10. /// [`static_assert`]: https://en.cppreference.com/w/cpp/language/static_assert
  11. /// [RFC 2790]: https://github.com/rust-lang/rfcs/issues/2790
  12. ///
  13. /// # Examples
  14. ///
  15. /// ```
  16. /// static_assert!(42 > 24);
  17. /// static_assert!(core::mem::size_of::<u8>() == 1);
  18. ///
  19. /// const X: &[u8] = b"bar";
  20. /// static_assert!(X[1] == b'a');
  21. ///
  22. /// const fn f(x: i32) -> i32 {
  23. /// x + 2
  24. /// }
  25. /// static_assert!(f(40) == 42);
  26. /// ```
  27. #[macro_export]
  28. macro_rules! static_assert {
  29. ($condition:expr) => {
  30. const _: () = core::assert!($condition);
  31. };
  32. }