Skip to content

Commit

Permalink
aya: impl From<obj::InvalidMapTypeError> for MapTypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
ajwerner authored and tamird committed Oct 11, 2023
1 parent cb455fe commit b73c0a4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
8 changes: 2 additions & 6 deletions aya/src/bpf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -710,12 +710,8 @@ impl<'a> BpfLoader<'a> {
}

fn parse_map(data: (String, MapData)) -> Result<(String, Map), BpfError> {
let name = data.0;
let map = data.1;
let map_type =
bpf_map_type::try_from(map.obj().map_type()).map_err(|e| MapError::InvalidMapType {
map_type: e.map_type,
})?;
let (name, map) = data;
let map_type = bpf_map_type::try_from(map.obj().map_type()).map_err(MapError::from)?;
let map = match map_type {
BPF_MAP_TYPE_ARRAY => Map::Array(map),
BPF_MAP_TYPE_PERCPU_ARRAY => Map::PerCpuArray(map),
Expand Down
11 changes: 11 additions & 0 deletions aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ use std::{

use libc::{getrlimit, rlim_t, rlimit, RLIMIT_MEMLOCK, RLIM_INFINITY};
use log::warn;
use obj::maps::InvalidMapTypeError;
use thiserror::Error;

use crate::{
Expand Down Expand Up @@ -193,6 +194,16 @@ pub enum MapError {
},
}

// Note that this is not just derived using #[from] because InvalidMapTypeError cannot implement
// Error due the the fact that aya-obj is no_std and error_in_core is not stabilized
// (https://github.com/rust-lang/rust/issues/103765).
impl From<InvalidMapTypeError> for MapError {
fn from(e: InvalidMapTypeError) -> Self {
let InvalidMapTypeError { map_type } = e;
Self::InvalidMapType { map_type }
}
}

/// A map file descriptor.
#[derive(Debug)]
pub struct MapFd(OwnedFd);
Expand Down
2 changes: 2 additions & 0 deletions xtask/public-api/aya.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1251,6 +1251,8 @@ impl core::convert::From<aya::maps::MapError> for aya::maps::xdp::XdpMapError
pub fn aya::maps::xdp::XdpMapError::from(source: aya::maps::MapError) -> Self
impl core::convert::From<aya::maps::MapError> for aya::programs::ProgramError
pub fn aya::programs::ProgramError::from(source: aya::maps::MapError) -> Self
impl core::convert::From<aya_obj::maps::InvalidMapTypeError> for aya::maps::MapError
pub fn aya::maps::MapError::from(e: aya_obj::maps::InvalidMapTypeError) -> Self
impl core::error::Error for aya::maps::MapError
pub fn aya::maps::MapError::source(&self) -> core::option::Option<&(dyn core::error::Error + 'static)>
impl core::fmt::Display for aya::maps::MapError
Expand Down

0 comments on commit b73c0a4

Please sign in to comment.