Skip to content

Commit

Permalink
libbpf-rs: program: Show AsFd in documentation
Browse files Browse the repository at this point in the history
Previously, AsFd trait was implemented on ProgramImpl. Since ProgramImpl
is not exported, the trait implementation did not show up on docs.rs.
You could still use it - but it wasn't obvious that it was available.
  • Loading branch information
danobi committed Sep 13, 2024
1 parent bece44a commit bb1d8ad
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions libbpf-rs/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,13 @@ pub struct ProgramImpl<'obj, T = ()> {
_phantom: PhantomData<&'obj T>,
}

impl<T> 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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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>;

Expand All @@ -1163,13 +1182,6 @@ impl<'obj> Deref for ProgramMut<'obj> {
}
}

impl<T> 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<T> AsRawLibbpf for ProgramImpl<'_, T> {
type LibbpfType = libbpf_sys::bpf_program;

Expand Down

0 comments on commit bb1d8ad

Please sign in to comment.