aya: remove TryInto magic from program()/program_mut() too

For programs it's actually useful being able to get the underlying
Program enum, for example when iterating/loading all the programs
pull/1/head
Alessandro Decina 4 years ago
parent 42e0a659b2
commit a92bfebf50

@ -1,6 +1,5 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
convert::TryFrom,
error::Error, error::Error,
fs, io, fs, io,
path::{Path, PathBuf}, path::{Path, PathBuf},
@ -185,21 +184,20 @@ impl Bpf {
ret ret
} }
pub fn program<'a, T: TryFrom<&'a Program>>( pub fn program(&self, name: &str) -> Result<&Program, ProgramError> {
&'a self, self.programs
name: &str, .get(name)
) -> Result<Option<T>, <T as TryFrom<&'a Program>>::Error> { .ok_or_else(|| ProgramError::NotFound {
self.programs.get(name).map(|p| T::try_from(p)).transpose() name: name.to_owned(),
})
} }
pub fn program_mut<'a, T: TryFrom<&'a mut Program>>( pub fn program_mut(&mut self, name: &str) -> Result<&mut Program, ProgramError> {
&'a mut self,
name: &str,
) -> Result<Option<T>, <T as TryFrom<&'a mut Program>>::Error> {
self.programs self.programs
.get_mut(name) .get_mut(name)
.map(|p| T::try_from(p)) .ok_or_else(|| ProgramError::NotFound {
.transpose() name: name.to_owned(),
})
} }
pub fn programs(&self) -> impl Iterator<Item = &Program> { pub fn programs(&self) -> impl Iterator<Item = &Program> {

@ -21,6 +21,9 @@ use crate::{
}; };
#[derive(Debug, Error)] #[derive(Debug, Error)]
pub enum ProgramError { pub enum ProgramError {
#[error("program `{name}` not found")]
NotFound { name: String },
#[error("the program is already loaded")] #[error("the program is already loaded")]
AlreadyLoaded, AlreadyLoaded,

Loading…
Cancel
Save