From 002df10b8c610788b2730ec1234934e81aaa2880 Mon Sep 17 00:00:00 2001 From: bestgopher <84328409@qq.com> Date: Tue, 14 Jul 2026 15:45:41 +0800 Subject: [PATCH] Simplify shared alignment assertions (#841) Replace array-underflow checks with const assert expressions. Add diagnostics that explain the pointer-tagging alignment invariant. Signed-off-by: bestgopher <84328409@qq.com> --- src/bytes.rs | 7 ++++++- src/bytes_mut.rs | 7 ++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/bytes.rs b/src/bytes.rs index 0f1aaed..bbd7bdc 100644 --- a/src/bytes.rs +++ b/src/bytes.rs @@ -1348,7 +1348,12 @@ impl Drop for Shared { // This is a necessary invariant since we depend on allocating `Shared` a // shared object to implicitly carry the `KIND_ARC` flag in its pointer. // This flag is set when the LSB is 0. -const _: [(); 0 - mem::align_of::() % 2] = []; // Assert that the alignment of `Shared` is divisible by 2. +const _: () = { + assert!( + mem::align_of::() % 2 == 0, + "Shared alignment must be divisible by 2 for pointer tagging" + ); +}; static SHARED_VTABLE: Vtable = Vtable { clone: shared_clone, diff --git a/src/bytes_mut.rs b/src/bytes_mut.rs index b983bd1..ae808fa 100644 --- a/src/bytes_mut.rs +++ b/src/bytes_mut.rs @@ -92,7 +92,12 @@ impl Shared { // This is a necessary invariant since we depend on allocating `Shared` a // shared object to implicitly carry the `KIND_ARC` flag in its pointer. // This flag is set when the LSB is 0. -const _: [(); 0 - mem::align_of::() % 2] = []; // Assert that the alignment of `Shared` is divisible by 2. +const _: () = { + assert!( + mem::align_of::() % 2 == 0, + "Shared alignment must be divisible by 2 for pointer tagging" + ); +}; // Buffer storage strategy flags. const KIND_ARC: usize = 0b0;