29
29
//! ```
30
30
//! # #[macro_use]
31
31
//! # extern crate static_assertions;
32
- //! # fn main() {
33
- //! assert_eq_size!([u8; 4], (u16, u16 ), u32 );
32
+ //! // Can be declared outside of a function if labeled
33
+ //! assert_eq_size!(bytes; (u8, u8 ), u16 );
34
34
//!
35
- //! // Produces a compilation failure:
36
- //! // assert_eq_size!(u32, u8);
37
- //! # }
35
+ //! // Fails to compile (same label):
36
+ //! // assert_eq_size!(bytes; u8, u8);
37
+ //!
38
+ //! fn main() {
39
+ //! assert_eq_size!([u8; 4], (u16, u16), u32);
40
+ //!
41
+ //! // Produces a compilation failure:
42
+ //! // assert_eq_size!(u32, u8);
43
+ //! }
38
44
//! ```
39
45
//!
40
46
//! Similar to [`assert_eq_size`], there is [`assert_eq_size_val`]. Instead of
110
116
//!
111
117
//! # Limitations
112
118
//!
113
- //! Due to implementation details, [`assert_eq_size`] can only be used from
114
- //! within the context of a function.
119
+ //! Due to implementation details, [`assert_eq_size`], [`const_assert`], and
120
+ //! [`const_assert_eq`] can only be used normally from within the context of a
121
+ //! function.
115
122
//!
116
- //! This is partially resolved in [`const_assert`] and [`const_assert_eq`] by
117
- //! requiring a unique label when not inside a function.
123
+ //! To use these macros in other contexts, a unique label must be provided.
118
124
//!
119
125
//! [crate]: https://crates.io/crates/static_assertions
120
126
//! [static_assert]: http://en.cppreference.com/w/cpp/language/static_assert
@@ -142,15 +148,20 @@ pub extern crate core as _core;
142
148
/// # extern crate static_assertions;
143
149
/// struct Byte(u8);
144
150
///
145
- /// # fn main() {
146
- /// assert_eq_size!(Byte, u8);
151
+ /// assert_eq_size!(pair; (u16, u16), [u16; 2], [u8; 4]);
147
152
///
148
- /// // Supports unlimited arguments :
149
- /// assert_eq_size!([Byte; 4], [u16; 2], u32 );
153
+ /// // Fails to compile (same label) :
154
+ /// // assert_eq_size!(pair; u8, u8 );
150
155
///
151
- /// // Fails to compile:
152
- /// // assert_eq_size!(Byte, u16);
153
- /// # }
156
+ /// fn main() {
157
+ /// assert_eq_size!(Byte, u8);
158
+ ///
159
+ /// // Supports unlimited arguments:
160
+ /// assert_eq_size!([Byte; 4], [u16; 2], u32);
161
+ ///
162
+ /// // Produces a compilation failure:
163
+ /// // assert_eq_size!(Byte, u16);
164
+ /// }
154
165
/// ```
155
166
#[ macro_export]
156
167
macro_rules! assert_eq_size {
@@ -160,7 +171,13 @@ macro_rules! assert_eq_size {
160
171
use $crate:: _core:: mem:: { forget, transmute, uninitialized} ;
161
172
$( forget:: <$xs>( transmute( uninitialized:: <$x>( ) ) ) ; ) +
162
173
}
163
- }
174
+ } ;
175
+ ( $label: ident; $x: ty, $( $xs: ty) ,+) => {
176
+ #[ allow( non_snake_case) ]
177
+ mod $label {
178
+ fn _impl( ) { assert_eq_size!( $x, $( $xs) ,+) }
179
+ }
180
+ } ;
164
181
}
165
182
166
183
/// Asserts at compile-time that the values pointed to have equal sizes.
0 commit comments