浏览代码

aya: remove TryInto cleverness from map() and map_mut()

Require callers to call try_into() explicitly. It's more characters, but
it's easier to understand/document.

Also introduce MapError::NotFound instead of returning Result<Option<_>>.
Alessandro Decina 4 年之前
父节点
当前提交
42e0a659b2
共有 2 个文件被更改,包括 17 次插入22 次删除
  1. 14 22
      aya/src/bpf.rs
  2. 3 0
      aya/src/maps/mod.rs

+ 14 - 22
aya/src/bpf.rs

@@ -148,38 +148,30 @@ impl Bpf {
         })
     }
 
-    pub fn map<T: TryFrom<MapRef>>(
-        &self,
-        name: &str,
-    ) -> Result<Option<T>, <T as TryFrom<MapRef>>::Error>
-    where
-        <T as TryFrom<MapRef>>::Error: From<MapError>,
-    {
+    pub fn map(&self, name: &str) -> Result<MapRef, MapError> {
         self.maps
             .get(name)
-            .map(|lock| {
-                T::try_from(lock.try_read().map_err(|_| MapError::BorrowError {
+            .ok_or_else(|| MapError::NotFound {
+                name: name.to_owned(),
+            })
+            .and_then(|lock| {
+                lock.try_read().map_err(|_| MapError::BorrowError {
                     name: name.to_owned(),
-                })?)
+                })
             })
-            .transpose()
     }
 
-    pub fn map_mut<T: TryFrom<MapRefMut>>(
-        &self,
-        name: &str,
-    ) -> Result<Option<T>, <T as TryFrom<MapRefMut>>::Error>
-    where
-        <T as TryFrom<MapRefMut>>::Error: From<MapError>,
-    {
+    pub fn map_mut(&self, name: &str) -> Result<MapRefMut, MapError> {
         self.maps
             .get(name)
-            .map(|lock| {
-                T::try_from(lock.try_write().map_err(|_| MapError::BorrowError {
+            .ok_or_else(|| MapError::NotFound {
+                name: name.to_owned(),
+            })
+            .and_then(|lock| {
+                lock.try_write().map_err(|_| MapError::BorrowError {
                     name: name.to_owned(),
-                })?)
+                })
             })
-            .transpose()
     }
 
     pub fn maps<'a>(&'a self) -> impl Iterator<Item = (&'a str, Result<MapRef, MapError>)> + 'a {

+ 3 - 0
aya/src/maps/mod.rs

@@ -20,6 +20,9 @@ pub use program_array::*;
 
 #[derive(Error, Debug)]
 pub enum MapError {
+    #[error("map `{name}` not found ")]
+    NotFound { name: String },
+
     #[error("invalid map type {map_type}")]
     InvalidMapType { map_type: u32 },