Skip to content

Commit f3da7e4

Browse files
committed
Enable assert_eq_size anywhere like const_assert
This is a partial solution to issue #1.
1 parent b7ca44e commit f3da7e4

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

src/lib.rs

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,18 @@
2929
//! ```
3030
//! # #[macro_use]
3131
//! # 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);
3434
//!
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+
//! }
3844
//! ```
3945
//!
4046
//! Similar to [`assert_eq_size`], there is [`assert_eq_size_val`]. Instead of
@@ -110,11 +116,11 @@
110116
//!
111117
//! # Limitations
112118
//!
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.
115122
//!
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.
118124
//!
119125
//! [crate]: https://crates.io/crates/static_assertions
120126
//! [static_assert]: http://en.cppreference.com/w/cpp/language/static_assert
@@ -142,15 +148,20 @@ pub extern crate core as _core;
142148
/// # extern crate static_assertions;
143149
/// struct Byte(u8);
144150
///
145-
/// # fn main() {
146-
/// assert_eq_size!(Byte, u8);
151+
/// assert_eq_size!(pair; (u16, u16), [u16; 2], [u8; 4]);
147152
///
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);
150155
///
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+
/// }
154165
/// ```
155166
#[macro_export]
156167
macro_rules! assert_eq_size {
@@ -160,7 +171,13 @@ macro_rules! assert_eq_size {
160171
use $crate::_core::mem::{forget, transmute, uninitialized};
161172
$(forget::<$xs>(transmute(uninitialized::<$x>()));)+
162173
}
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+
};
164181
}
165182

166183
/// Asserts at compile-time that the values pointed to have equal sizes.

tests/eq_size.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
#[macro_use]
44
extern crate static_assertions;
55

6+
assert_eq_size!(byte; u8, u8, (u8,), [u8; 1]);
7+
8+
#[cfg(feature = "failure")]
9+
const_assert!(byte; u8, u8);
10+
611
// Placed in separate module so that DropCounter's fields are private
712
mod dc {
813
/// A type that acts somewhat of a reference counter.

0 commit comments

Comments
 (0)