diff --git a/libbpf-rs/src/program.rs b/libbpf-rs/src/program.rs index f39ac997..5a0a5c96 100644 --- a/libbpf-rs/src/program.rs +++ b/libbpf-rs/src/program.rs @@ -564,6 +564,13 @@ pub struct ProgramImpl<'obj, T = ()> { _phantom: PhantomData<&'obj T>, } +impl ProgramImpl<'_, T> { + fn as_fd(&self) -> BorrowedFd<'_> { + let fd = unsafe { libbpf_sys::bpf_program__fd(self.ptr.as_ptr()) }; + unsafe { BorrowedFd::borrow_raw(fd) } + } +} + impl<'obj> Program<'obj> { /// Create a [`Program`] from a [`libbpf_sys::bpf_program`] pub fn new(prog: &'obj libbpf_sys::bpf_program) -> Self { @@ -706,6 +713,12 @@ impl<'obj> Program<'obj> { } } +impl AsFd for Program<'_> { + fn as_fd(&self) -> BorrowedFd<'_> { + ProgramImpl::as_fd(self) + } +} + impl<'obj> ProgramMut<'obj> { /// Create a [`Program`] from a [`libbpf_sys::bpf_program`] pub fn new_mut(prog: &'obj mut libbpf_sys::bpf_program) -> Self { @@ -1153,6 +1166,12 @@ impl<'obj> ProgramMut<'obj> { } } +impl AsFd for ProgramMut<'_> { + fn as_fd(&self) -> BorrowedFd<'_> { + ProgramImpl::as_fd(self) + } +} + impl<'obj> Deref for ProgramMut<'obj> { type Target = Program<'obj>; @@ -1163,13 +1182,6 @@ impl<'obj> Deref for ProgramMut<'obj> { } } -impl AsFd for ProgramImpl<'_, T> { - fn as_fd(&self) -> BorrowedFd<'_> { - let fd = unsafe { libbpf_sys::bpf_program__fd(self.ptr.as_ptr()) }; - unsafe { BorrowedFd::borrow_raw(fd) } - } -} - impl AsRawLibbpf for ProgramImpl<'_, T> { type LibbpfType = libbpf_sys::bpf_program;