• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Macros that assert properties of code at compile time.
6 //!
7 //! A static assertion is particularly appropriate when unsafe code relies on
8 //! two types to have the same size, or on some type to have a particular size.
9 
10 #[doc(hidden)]
11 pub mod mechanism;
12 
13 // Re-export so that these types appear with a more concise name in error
14 // messages.
15 #[doc(hidden)]
16 pub use crate::mechanism::*;
17 
18 /// Macro that fails to compile if a given const expression is not true.
19 ///
20 /// # Example
21 ///
22 /// ```rust
23 /// use assertions::const_assert;
24 ///
25 /// fn main() {
26 ///     const_assert!(std::mem::size_of::<String>() == 24);
27 /// }
28 /// ```
29 ///
30 /// # Example that fails to compile
31 ///
32 /// ```rust,compile_fail
33 /// use assertions::const_assert;
34 ///
35 /// fn main() {
36 ///     // fails to compile:
37 ///     const_assert!(std::mem::size_of::<String>() == 8);
38 /// }
39 /// ```
40 #[macro_export]
41 macro_rules! const_assert {
42     ($e:expr) => {
43         let _: $crate::Assert<[(); $e as bool as usize]>;
44     };
45 }
46