Browse Source

aya: Add Map::fd() function to return a MapFd

Signed-off-by: Dave Tucker <[email protected]>
Dave Tucker 2 years ago
parent
commit
623579a47f
1 changed files with 25 additions and 2 deletions
  1. 25 2
      aya/src/maps/mod.rs

+ 25 - 2
aya/src/maps/mod.rs

@@ -33,8 +33,15 @@
 //! versa. Because of that, all map values must be plain old data and therefore
 //! implement the [Pod] trait.
 use std::{
-    convert::TryFrom, ffi::CString, io, marker::PhantomData, mem, ops::Deref, os::unix::io::RawFd,
-    path::Path, ptr,
+    convert::TryFrom,
+    ffi::CString,
+    io,
+    marker::PhantomData,
+    mem,
+    ops::Deref,
+    os::unix::{io::RawFd, prelude::AsRawFd},
+    path::Path,
+    ptr,
 };
 use thiserror::Error;
 
@@ -206,6 +213,15 @@ pub enum MapError {
     },
 }
 
+/// A map file descriptor.
+pub struct MapFd(RawFd);
+
+impl AsRawFd for MapFd {
+    fn as_raw_fd(&self) -> RawFd {
+        self.0
+    }
+}
+
 /// A generic handle to a BPF map.
 ///
 /// You should never need to use this unless you're implementing a new map type.
@@ -295,6 +311,13 @@ impl Map {
         self.pinned = true;
         Ok(())
     }
+
+    /// Returns the file descriptor of the map.
+    ///
+    /// Can be converted to [`RawFd`] using [`AsRawFd`].
+    pub fn fd(&self) -> Option<MapFd> {
+        self.fd.map(MapFd)
+    }
 }
 
 impl Drop for Map {