From 04a8a80b8030b72f20f012cbd814caa5143d034e Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Sun, 13 Jul 2025 08:49:02 -0400 Subject: [PATCH] ring_buf: destructure, avoid `as` casts --- ebpf/aya-ebpf/src/maps/ring_buf.rs | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/ebpf/aya-ebpf/src/maps/ring_buf.rs b/ebpf/aya-ebpf/src/maps/ring_buf.rs index 9de0a7fe..c134be94 100644 --- a/ebpf/aya-ebpf/src/maps/ring_buf.rs +++ b/ebpf/aya-ebpf/src/maps/ring_buf.rs @@ -45,25 +45,29 @@ impl Deref for RingBufEntry { type Target = MaybeUninit; fn deref(&self) -> &Self::Target { - self.0 + let Self(inner) = self; + inner } } impl DerefMut for RingBufEntry { fn deref_mut(&mut self) -> &mut Self::Target { - self.0 + let Self(inner) = self; + inner } } impl RingBufEntry { /// Discard this ring buffer entry. The entry will be skipped by the userspace reader. pub fn discard(self, flags: u64) { - unsafe { bpf_ringbuf_discard(self.0.as_mut_ptr() as *mut _, flags) }; + let Self(inner) = self; + unsafe { bpf_ringbuf_discard(inner.as_mut_ptr().cast(), flags) }; } /// Commit this ring buffer entry. The entry will be made visible to the userspace reader. pub fn submit(self, flags: u64) { - unsafe { bpf_ringbuf_submit(self.0.as_mut_ptr() as *mut _, flags) }; + let Self(inner) = self; + unsafe { bpf_ringbuf_submit(inner.as_mut_ptr().cast(), flags) }; } } @@ -125,8 +129,9 @@ impl RingBuf { fn reserve_impl(&self, flags: u64) -> Option> { let ptr = unsafe { - bpf_ringbuf_reserve(self.def.get() as *mut _, mem::size_of::() as _, flags) - } as *mut MaybeUninit; + bpf_ringbuf_reserve(self.def.get().cast(), mem::size_of::() as u64, flags) + } + .cast::>(); unsafe { ptr.as_mut() }.map(|ptr| RingBufEntry(ptr)) } @@ -149,9 +154,9 @@ impl RingBuf { assert_eq!(8 % mem::align_of_val(data), 0); let ret = unsafe { bpf_ringbuf_output( - self.def.get() as *mut _, - data as *const _ as *mut _, - mem::size_of_val(data) as _, + self.def.get().cast(), + core::ptr::from_ref(data).cast_mut().cast(), + mem::size_of_val(data) as u64, flags, ) }; @@ -162,6 +167,6 @@ impl RingBuf { /// /// Consult `bpf_ringbuf_query` documentation for a list of allowed flags. pub fn query(&self, flags: u64) -> u64 { - unsafe { bpf_ringbuf_query(self.def.get() as *mut _, flags) } + unsafe { bpf_ringbuf_query(self.def.get().cast(), flags) } } }