diff --git a/aya/src/maps/bloom_filter.rs b/aya/src/maps/bloom_filter.rs index b97f8720..b19b9d5d 100644 --- a/aya/src/maps/bloom_filter.rs +++ b/aya/src/maps/bloom_filter.rs @@ -150,7 +150,7 @@ mod tests { assert!(matches!( BloomFilter::<_, u32>::try_from(&map), - Err(MapError::UnexpectedMapType) + Err(MapError::InvalidMapType { .. }) )); } diff --git a/aya/src/maps/hash_map/hash_map.rs b/aya/src/maps/hash_map/hash_map.rs index 42e5f596..53057c62 100644 --- a/aya/src/maps/hash_map/hash_map.rs +++ b/aya/src/maps/hash_map/hash_map.rs @@ -183,7 +183,7 @@ mod tests { let map = Map::Array(map_data); assert!(matches!( HashMap::<_, u8, u32>::try_from(&map), - Err(MapError::UnexpectedMapType) + Err(MapError::InvalidMapType { .. }) )); } diff --git a/aya/src/maps/lpm_trie.rs b/aya/src/maps/lpm_trie.rs index 047b36b1..5fb92d79 100644 --- a/aya/src/maps/lpm_trie.rs +++ b/aya/src/maps/lpm_trie.rs @@ -260,7 +260,7 @@ mod tests { assert!(matches!( LpmTrie::<_, u32, u32>::try_from(&map), - Err(MapError::UnexpectedMapType) + Err(MapError::InvalidMapType { .. }) )); } diff --git a/aya/src/maps/mod.rs b/aya/src/maps/mod.rs index 94484cc9..c5632808 100644 --- a/aya/src/maps/mod.rs +++ b/aya/src/maps/mod.rs @@ -191,10 +191,6 @@ pub enum MapError { #[source] error: PinError, }, - - /// The map is not of the expected type. - #[error("unexpected map type")] - UnexpectedMapType, } /// A map file descriptor. @@ -283,7 +279,7 @@ macro_rules! impl_try_from_map { Map::$ty(m) => { $tx::new(m) }, - _ => Err(MapError::UnexpectedMapType), + _ => Err(MapError::InvalidMapType{ map_type: map.into()}), } } } @@ -296,7 +292,7 @@ macro_rules! impl_try_from_map { Map::$ty(m) => { $tx::new(m) }, - _ => Err(MapError::UnexpectedMapType), + _ => Err(MapError::InvalidMapType{ map_type: map.into()}), } } } @@ -309,7 +305,7 @@ macro_rules! impl_try_from_map { Map::$ty(m) => { $tx::new(m) }, - _ => Err(MapError::UnexpectedMapType), + _ => Err(MapError::InvalidMapType{ map_type: map.into()}), } } } @@ -341,7 +337,7 @@ macro_rules! impl_try_from_map_generic_key_or_value { Map::$ty(m) => { $ty::new(m) }, - _ => Err(MapError::UnexpectedMapType), + _ => Err(MapError::InvalidMapType{ map_type: map.into()}), } } } @@ -354,7 +350,7 @@ macro_rules! impl_try_from_map_generic_key_or_value { Map::$ty(m) => { $ty::new(m) }, - _ => Err(MapError::UnexpectedMapType), + _ => Err(MapError::InvalidMapType{ map_type: map.into()}), } } } @@ -367,7 +363,7 @@ macro_rules! impl_try_from_map_generic_key_or_value { Map::$ty(m) => { $ty::new(m) }, - _ => Err(MapError::UnexpectedMapType), + _ => Err(MapError::InvalidMapType{ map_type: map.into()}), } } } @@ -388,7 +384,7 @@ macro_rules! impl_try_from_map_generic_key_and_value { Map::$ty(m) => { $ty::new(m) }, - _ => Err(MapError::UnexpectedMapType), + _ => Err(MapError::InvalidMapType{ map_type: map.into()}), } } } @@ -401,7 +397,7 @@ macro_rules! impl_try_from_map_generic_key_and_value { Map::$ty(m) => { $ty::new(m) }, - _ => Err(MapError::UnexpectedMapType), + _ => Err(MapError::InvalidMapType{ map_type: map.into()}), } } } @@ -761,6 +757,35 @@ impl TryFrom for bpf_map_type { }) } } + +macro_rules! impl_try_from_map_generic_key_or_value { + ($($typ:ty),+ $(,)?) => { + $( + impl From<$typ> for u32 { + fn from(map_type: $typ) -> u32 { + match map_type { + Map::Array(map) => map.obj.map_type(), + Map::PerCpuArray(map) => map.obj.map_type(), + Map::ProgramArray(map) => map.obj.map_type(), + Map::HashMap(map) => map.obj.map_type(), + Map::PerCpuHashMap(map) => map.obj.map_type(), + Map::PerfEventArray(map) => map.obj.map_type(), + Map::SockHash(map) => map.obj.map_type(), + Map::SockMap(map) => map.obj.map_type(), + Map::BloomFilter(map) => map.obj.map_type(), + Map::LpmTrie(map) => map.obj.map_type(), + Map::Stack(map) => map.obj.map_type(), + Map::StackTraceMap(map) => map.obj.map_type(), + Map::Queue(map) => map.obj.map_type(), + } + } + } + )+ + } +} + +impl_try_from_map_generic_key_or_value!(&Map, &mut Map, Map); + pub(crate) struct PerCpuKernelMem { bytes: Vec, }