From 42e0a659b2f82cca537d70f906e0a475f0ab6b03 Mon Sep 17 00:00:00 2001 From: Alessandro Decina Date: Tue, 2 Mar 2021 08:27:09 +0000 Subject: [PATCH] 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>. --- aya/src/bpf.rs | 36 ++++++++++++++---------------------- aya/src/maps/mod.rs | 3 +++ 2 files changed, 17 insertions(+), 22 deletions(-) diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index 65c0fee4..9fc791dd 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -148,38 +148,30 @@ impl Bpf { }) } - pub fn map>( - &self, - name: &str, - ) -> Result, >::Error> - where - >::Error: From, - { + pub fn map(&self, name: &str) -> Result { 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>( - &self, - name: &str, - ) -> Result, >::Error> - where - >::Error: From, - { + pub fn map_mut(&self, name: &str) -> Result { 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)> + 'a { diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index c730bf53..324e4ef5 100644 --- a/aya/src/maps/mod.rs +++ b/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 },