Skip to content

Commit 06bd0e5

Browse files
fujitafbq
authored andcommitted
rust: sync: atomic: Add atomic bool support via i8 representation
Add `bool` support, `Atomic<bool>` by using `i8` as its underlying representation. Rust specifies that `bool` has size 1 and alignment 1 [1], so it matches `i8` on layout; keep `static_assert!()` checks to enforce this assumption at build time. [boqun: Remove the unnecessary impl AtomicImpl for bool] Link: https://doc.rust-lang.org/reference/types/boolean.html [1] Signed-off-by: FUJITA Tomonori <fujita.tomonori@gmail.com> Reviewed-by: Gary Guo <gary@garyguo.net> Signed-off-by: Boqun Feng <boqun.feng@gmail.com> Link: https://patch.msgid.link/20260101034922.2020334-2-fujita.tomonori@gmail.com
1 parent 584f286 commit 06bd0e5

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

rust/kernel/sync/atomic/predefine.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@
55
use crate::static_assert;
66
use core::mem::{align_of, size_of};
77

8+
// Ensure size and alignment requirements are checked.
9+
static_assert!(size_of::<bool>() == size_of::<i8>());
10+
static_assert!(align_of::<bool>() == align_of::<i8>());
11+
12+
// SAFETY: `bool` has the same size and alignment as `i8`, and Rust guarantees that `bool` has
13+
// only two valid bit patterns: 0 (false) and 1 (true). Those are valid `i8` values, so `bool` is
14+
// round-trip transmutable to `i8`.
15+
unsafe impl super::AtomicType for bool {
16+
type Repr = i8;
17+
}
18+
819
// SAFETY: `i8` has the same size and alignment with itself, and is round-trip transmutable to
920
// itself.
1021
unsafe impl super::AtomicType for i8 {

0 commit comments

Comments
 (0)