diff --git a/aya/src/bpf.rs b/aya/src/bpf.rs index f967c737..5d9a4051 100644 --- a/aya/src/bpf.rs +++ b/aya/src/bpf.rs @@ -702,12 +702,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), diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 4d8183e9..15626eb6 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -50,6 +50,7 @@ use std::{ use crate::util::KernelVersion; use libc::{getrlimit, rlimit, RLIMIT_MEMLOCK, RLIM_INFINITY}; use log::warn; +use obj::maps::InvalidMapTypeError; use thiserror::Error; use crate::{ @@ -176,6 +177,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 nostd and error_in_core is not stabilized +// (https://github.com/rust-lang/rust/issues/103765). +impl From for MapError { + fn from(e: InvalidMapTypeError) -> Self { + let InvalidMapTypeError { map_type } = e; + MapError::InvalidMapType { map_type } + } +} + /// A map file descriptor. pub struct MapFd(RawFd); diff --git a/xtask/public-api/aya.txt b/xtask/public-api/aya.txt index 9991dd35..df30ca03 100644 --- a/xtask/public-api/aya.txt +++ b/xtask/public-api/aya.txt @@ -997,6 +997,8 @@ impl core::convert::From for aya::BpfError pub fn aya::BpfError::from(source: aya::maps::MapError) -> Self impl core::convert::From for aya::programs::ProgramError pub fn aya::programs::ProgramError::from(source: aya::maps::MapError) -> Self +impl core::convert::From 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