Skip to content

Commit 327da1f

Browse files
committed
refactor: use const _: () = { ... }; for static assert
1 parent 7860e58 commit 327da1f

File tree

6 files changed

+30
-28
lines changed

6 files changed

+30
-28
lines changed

src/lib.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,12 @@ mod features;
3636
#[repr(transparent)]
3737
pub struct LeanString(Repr);
3838

39-
fn _static_assert() {
40-
const {
41-
assert!(size_of::<LeanString>() == 2 * size_of::<usize>());
42-
assert!(size_of::<Option<LeanString>>() == 2 * size_of::<usize>());
43-
assert!(align_of::<LeanString>() == align_of::<usize>());
44-
assert!(align_of::<Option<LeanString>>() == align_of::<usize>());
45-
}
46-
}
39+
const _: () = {
40+
assert!(size_of::<LeanString>() == size_of::<[usize; 2]>());
41+
assert!(size_of::<Option<LeanString>>() == size_of::<[usize; 2]>());
42+
assert!(align_of::<LeanString>() == align_of::<usize>());
43+
assert!(align_of::<Option<LeanString>>() == align_of::<usize>());
44+
};
4745

4846
impl LeanString {
4947
/// Creates a new empty [`LeanString`].

src/repr.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,12 @@ pub(crate) struct Repr(*const (), [u8; 7], LastByte);
3232
#[cfg(target_pointer_width = "32")]
3333
pub(crate) struct Repr(*const (), [u8; 3], LastByte);
3434

35-
fn _static_assert() {
36-
const {
37-
assert!(size_of::<Repr>() == MAX_INLINE_SIZE);
38-
assert!(size_of::<Option<Repr>>() == MAX_INLINE_SIZE);
39-
assert!(align_of::<Repr>() == align_of::<usize>());
40-
assert!(align_of::<Option<Repr>>() == align_of::<usize>());
41-
}
42-
}
35+
const _: () = {
36+
assert!(size_of::<Repr>() == MAX_INLINE_SIZE);
37+
assert!(size_of::<Option<Repr>>() == MAX_INLINE_SIZE);
38+
assert!(align_of::<Repr>() == align_of::<usize>());
39+
assert!(align_of::<Option<Repr>>() == align_of::<usize>());
40+
};
4341

4442
impl Repr {
4543
#[inline]

src/repr/heap_buffer.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,10 @@ struct Header {
3434
capacity: Capacity,
3535
}
3636

37-
fn _static_assert() {
38-
const {
39-
assert!(size_of::<HeapBuffer>() == MAX_INLINE_SIZE);
40-
assert!(align_of::<HeapBuffer>() == align_of::<usize>());
41-
}
42-
}
37+
const _: () = {
38+
assert!(size_of::<HeapBuffer>() == MAX_INLINE_SIZE);
39+
assert!(align_of::<HeapBuffer>() == align_of::<usize>());
40+
};
4341

4442
impl HeapBuffer {
4543
pub(super) fn new(text: &str) -> Result<Self, ReserveError> {

src/repr/inline_buffer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ pub(super) struct InlineBuffer([u8; MAX_INLINE_SIZE]);
88
#[repr(C, align(4))]
99
pub(super) struct InlineBuffer([u8; MAX_INLINE_SIZE]);
1010

11+
const _: () = {
12+
assert!(size_of::<InlineBuffer>() == MAX_INLINE_SIZE);
13+
assert!(align_of::<InlineBuffer>() == align_of::<usize>());
14+
};
15+
1116
impl InlineBuffer {
1217
/// # Safety
1318
/// `text` must have a length less than or equal to `MAX_INLINE_SIZE`.

src/repr/last_byte.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -230,12 +230,10 @@ pub enum LastByte {
230230
StaticMarker = 0xD1,
231231
}
232232

233-
fn _static_assert() {
234-
const {
235-
assert!(size_of::<LastByte>() == 1);
236-
assert!(size_of::<Option<LastByte>>() == 1);
237-
}
238-
}
233+
const _: () = {
234+
assert!(size_of::<LastByte>() == 1);
235+
assert!(size_of::<Option<LastByte>>() == 1);
236+
};
239237

240238
#[allow(dead_code)]
241239
impl LastByte {

src/repr/static_buffer.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ pub(super) struct StaticBuffer {
66
len: usize, // stored as little-endian
77
}
88

9+
const _: () = {
10+
assert!(size_of::<StaticBuffer>() == MAX_INLINE_SIZE);
11+
assert!(align_of::<StaticBuffer>() == align_of::<usize>());
12+
};
13+
914
const USIZE_SIZE: usize = size_of::<usize>();
1015

1116
impl StaticBuffer {

0 commit comments

Comments
 (0)