Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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

authored by

FUJITA Tomonori and committed by
Boqun Feng
06bd0e52 584f286f

+11
+11
rust/kernel/sync/atomic/predefine.rs
··· 5 5 use crate::static_assert; 6 6 use core::mem::{align_of, size_of}; 7 7 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 + 8 19 // SAFETY: `i8` has the same size and alignment with itself, and is round-trip transmutable to 9 20 // itself. 10 21 unsafe impl super::AtomicType for i8 {