From 8a2deddac7179f6ef98957881517c518e2fa2731 Mon Sep 17 00:00:00 2001
From: Tamir Duberstein <tamird@gmail.com>
Date: Sat, 24 May 2025 14:32:15 -0400
Subject: [PATCH] aya-ebpf: add a dedicated generic_const_exprs cfg

---
 ebpf/aya-ebpf/build.rs             | 3 ++-
 ebpf/aya-ebpf/src/lib.rs           | 7 ++++++-
 ebpf/aya-ebpf/src/maps/ring_buf.rs | 8 ++++----
 3 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/ebpf/aya-ebpf/build.rs b/ebpf/aya-ebpf/build.rs
index c4dd61e6..7b0557b2 100644
--- a/ebpf/aya-ebpf/build.rs
+++ b/ebpf/aya-ebpf/build.rs
@@ -28,11 +28,12 @@ fn main() {
     }
     println!("))");
 
-    println!("cargo::rustc-check-cfg=cfg(unstable)");
+    println!("cargo::rustc-check-cfg=cfg(generic_const_exprs,unstable)");
 }
 
 #[rustversion::nightly]
 fn check_rust_version() {
+    println!("cargo:rustc-cfg=generic_const_exprs");
     println!("cargo:rustc-cfg=unstable");
 }
 
diff --git a/ebpf/aya-ebpf/src/lib.rs b/ebpf/aya-ebpf/src/lib.rs
index 971a3c40..d842abaf 100644
--- a/ebpf/aya-ebpf/src/lib.rs
+++ b/ebpf/aya-ebpf/src/lib.rs
@@ -8,7 +8,12 @@
     html_logo_url = "https://aya-rs.dev/assets/images/crabby.svg",
     html_favicon_url = "https://aya-rs.dev/assets/images/crabby.svg"
 )]
-#![cfg_attr(unstable, expect(incomplete_features), feature(generic_const_exprs))]
+// TODO(https://github.com/rust-lang/rust/issues/141492): reenable this.
+#![cfg_attr(
+    generic_const_exprs,
+    expect(incomplete_features),
+    feature(generic_const_exprs)
+)]
 #![cfg_attr(unstable, feature(never_type))]
 #![cfg_attr(target_arch = "bpf", feature(asm_experimental_arch))]
 #![warn(clippy::cast_lossless, clippy::cast_sign_loss)]
diff --git a/ebpf/aya-ebpf/src/maps/ring_buf.rs b/ebpf/aya-ebpf/src/maps/ring_buf.rs
index e7db9a53..9de0a7fe 100644
--- a/ebpf/aya-ebpf/src/maps/ring_buf.rs
+++ b/ebpf/aya-ebpf/src/maps/ring_buf.rs
@@ -14,7 +14,7 @@ use crate::{
     maps::PinningType,
 };
 
-#[cfg(unstable)]
+#[cfg(generic_const_exprs)]
 mod const_assert {
     pub struct Assert<const COND: bool> {}
 
@@ -22,7 +22,7 @@ mod const_assert {
 
     impl IsTrue for Assert<true> {}
 }
-#[cfg(unstable)]
+#[cfg(generic_const_exprs)]
 use const_assert::{Assert, IsTrue};
 
 #[repr(transparent)]
@@ -101,7 +101,7 @@ impl RingBuf {
     /// Reserve memory in the ring buffer that can fit `T`.
     ///
     /// Returns `None` if the ring buffer is full.
-    #[cfg(unstable)]
+    #[cfg(generic_const_exprs)]
     pub fn reserve<T: 'static>(&self, flags: u64) -> Option<RingBufEntry<T>>
     where
         Assert<{ 8 % mem::align_of::<T>() == 0 }>: IsTrue,
@@ -117,7 +117,7 @@ impl RingBuf {
     /// be equal or smaller than 8. If you use this with a `T` that isn't properly aligned, this
     /// function will be compiled to a panic; depending on your panic_handler, this may make
     /// the eBPF program fail to load, or it may make it have undefined behavior.
-    #[cfg(not(unstable))]
+    #[cfg(not(generic_const_exprs))]
     pub fn reserve<T: 'static>(&self, flags: u64) -> Option<RingBufEntry<T>> {
         assert_eq!(8 % mem::align_of::<T>(), 0);
         self.reserve_impl(flags)