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<_>>.
pull/1/head
Alessandro Decina 4 years ago
parent d3482c063c
commit 42e0a659b2

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

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

Loading…
Cancel
Save