Skip to content

Commit

Permalink
Fixups4
Browse files Browse the repository at this point in the history
Remove From method and replace with internal
helper function.

Signed-off-by: Andrew Stoycos <astoycos@redhat.com>
  • Loading branch information
astoycos committed Oct 19, 2022
1 parent 440097d commit 4ddf260
Showing 1 changed file with 29 additions and 36 deletions.
65 changes: 29 additions & 36 deletions aya/src/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,27 @@ pub enum Map {
Queue(MapData),
}

impl Map {
/// Returns the low level map type.
fn map_type(&self) -> u32 {
match self {
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(),
}
}
}

macro_rules! impl_try_from_map {
($($tx:ident from Map::$ty:ident),+ $(,)?) => {
$(
Expand All @@ -279,7 +300,7 @@ macro_rules! impl_try_from_map {
Map::$ty(m) => {
$tx::new(m)
},
_ => Err(MapError::InvalidMapType{ map_type: map.into()}),
_ => Err(MapError::InvalidMapType{ map_type: map.map_type()}),
}
}
}
Expand All @@ -292,7 +313,7 @@ macro_rules! impl_try_from_map {
Map::$ty(m) => {
$tx::new(m)
},
_ => Err(MapError::InvalidMapType{ map_type: map.into()}),
_ => Err(MapError::InvalidMapType{ map_type: map.map_type()}),
}
}
}
Expand All @@ -305,7 +326,7 @@ macro_rules! impl_try_from_map {
Map::$ty(m) => {
$tx::new(m)
},
_ => Err(MapError::InvalidMapType{ map_type: map.into()}),
_ => Err(MapError::InvalidMapType{ map_type: map.map_type()}),
}
}
}
Expand Down Expand Up @@ -337,7 +358,7 @@ macro_rules! impl_try_from_map_generic_key_or_value {
Map::$ty(m) => {
$ty::new(m)
},
_ => Err(MapError::InvalidMapType{ map_type: map.into()}),
_ => Err(MapError::InvalidMapType{ map_type: map.map_type()}),
}
}
}
Expand All @@ -350,7 +371,7 @@ macro_rules! impl_try_from_map_generic_key_or_value {
Map::$ty(m) => {
$ty::new(m)
},
_ => Err(MapError::InvalidMapType{ map_type: map.into()}),
_ => Err(MapError::InvalidMapType{ map_type: map.map_type()}),
}
}
}
Expand All @@ -363,7 +384,7 @@ macro_rules! impl_try_from_map_generic_key_or_value {
Map::$ty(m) => {
$ty::new(m)
},
_ => Err(MapError::InvalidMapType{ map_type: map.into()}),
_ => Err(MapError::InvalidMapType{ map_type: map.map_type()}),
}
}
}
Expand All @@ -384,7 +405,7 @@ macro_rules! impl_try_from_map_generic_key_and_value {
Map::$ty(m) => {
$ty::new(m)
},
_ => Err(MapError::InvalidMapType{ map_type: map.into()}),
_ => Err(MapError::InvalidMapType{ map_type: map.map_type()}),
}
}
}
Expand All @@ -397,7 +418,7 @@ macro_rules! impl_try_from_map_generic_key_and_value {
Map::$ty(m) => {
$ty::new(m)
},
_ => Err(MapError::InvalidMapType{ map_type: map.into()}),
_ => Err(MapError::InvalidMapType{ map_type: map.map_type()}),
}
}
}
Expand Down Expand Up @@ -758,34 +779,6 @@ impl TryFrom<u32> 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<u8>,
}
Expand Down

0 comments on commit 4ddf260

Please sign in to comment.