@@ -23,8 +23,6 @@ use std::slice::from_raw_parts;
2323use bitflags:: bitflags;
2424use libbpf_sys:: bpf_map_info;
2525use libbpf_sys:: bpf_obj_get_info_by_fd;
26- use num_enum:: IntoPrimitive ;
27- use num_enum:: TryFromPrimitive ;
2826use strum_macros:: Display ;
2927
3028use crate :: util;
@@ -67,10 +65,7 @@ impl OpenMap {
6765 /// Retrieve type of the map.
6866 pub fn map_type ( & self ) -> MapType {
6967 let ty = unsafe { libbpf_sys:: bpf_map__type ( self . ptr . as_ptr ( ) ) } ;
70- match MapType :: try_from ( ty) {
71- Ok ( t) => t,
72- Err ( _) => MapType :: Unknown ,
73- }
68+ MapType :: from ( ty)
7469 }
7570
7671 fn initial_value_raw ( & self ) -> ( * mut u8 , usize ) {
@@ -266,8 +261,7 @@ impl Map {
266261 let fd = unsafe { libbpf_sys:: bpf_map__fd ( ptr. as_ptr ( ) ) } ;
267262 let fd = util:: parse_ret_i32 ( fd) ?;
268263
269- let ty = MapType :: try_from ( unsafe { libbpf_sys:: bpf_map__type ( ptr. as_ptr ( ) ) } )
270- . unwrap_or ( MapType :: Unknown ) ;
264+ let ty = MapType :: from ( unsafe { libbpf_sys:: bpf_map__type ( ptr. as_ptr ( ) ) } ) ;
271265 let key_size = unsafe { libbpf_sys:: bpf_map__key_size ( ptr. as_ptr ( ) ) } ;
272266 let value_size = unsafe { libbpf_sys:: bpf_map__value_size ( ptr. as_ptr ( ) ) } ;
273267
@@ -987,7 +981,7 @@ bitflags! {
987981// If you add a new per-cpu map, also update `is_percpu`.
988982#[ non_exhaustive]
989983#[ repr( u32 ) ]
990- #[ derive( Copy , Clone , TryFromPrimitive , IntoPrimitive , PartialEq , Eq , Display , Debug ) ]
984+ #[ derive( Copy , Clone , PartialEq , Eq , Display , Debug ) ]
991985// TODO: Document members.
992986#[ allow( missing_docs) ]
993987pub enum MapType {
@@ -1066,6 +1060,54 @@ impl MapType {
10661060 }
10671061}
10681062
1063+ impl From < u32 > for MapType {
1064+ fn from ( value : u32 ) -> Self {
1065+ use MapType :: * ;
1066+
1067+ match value {
1068+ x if x == Unspec as u32 => Unspec ,
1069+ x if x == Hash as u32 => Hash ,
1070+ x if x == Array as u32 => Array ,
1071+ x if x == ProgArray as u32 => ProgArray ,
1072+ x if x == PerfEventArray as u32 => PerfEventArray ,
1073+ x if x == PercpuHash as u32 => PercpuHash ,
1074+ x if x == PercpuArray as u32 => PercpuArray ,
1075+ x if x == StackTrace as u32 => StackTrace ,
1076+ x if x == CgroupArray as u32 => CgroupArray ,
1077+ x if x == LruHash as u32 => LruHash ,
1078+ x if x == LruPercpuHash as u32 => LruPercpuHash ,
1079+ x if x == LpmTrie as u32 => LpmTrie ,
1080+ x if x == ArrayOfMaps as u32 => ArrayOfMaps ,
1081+ x if x == HashOfMaps as u32 => HashOfMaps ,
1082+ x if x == Devmap as u32 => Devmap ,
1083+ x if x == Sockmap as u32 => Sockmap ,
1084+ x if x == Cpumap as u32 => Cpumap ,
1085+ x if x == Xskmap as u32 => Xskmap ,
1086+ x if x == Sockhash as u32 => Sockhash ,
1087+ x if x == CgroupStorage as u32 => CgroupStorage ,
1088+ x if x == ReuseportSockarray as u32 => ReuseportSockarray ,
1089+ x if x == PercpuCgroupStorage as u32 => PercpuCgroupStorage ,
1090+ x if x == Queue as u32 => Queue ,
1091+ x if x == Stack as u32 => Stack ,
1092+ x if x == SkStorage as u32 => SkStorage ,
1093+ x if x == DevmapHash as u32 => DevmapHash ,
1094+ x if x == StructOps as u32 => StructOps ,
1095+ x if x == RingBuf as u32 => RingBuf ,
1096+ x if x == InodeStorage as u32 => InodeStorage ,
1097+ x if x == TaskStorage as u32 => TaskStorage ,
1098+ x if x == BloomFilter as u32 => BloomFilter ,
1099+ x if x == UserRingBuf as u32 => UserRingBuf ,
1100+ _ => Unknown ,
1101+ }
1102+ }
1103+ }
1104+
1105+ impl From < MapType > for u32 {
1106+ fn from ( value : MapType ) -> Self {
1107+ value as u32
1108+ }
1109+ }
1110+
10691111#[ derive( Debug ) ]
10701112pub struct MapKeyIter < ' a > {
10711113 map : & ' a MapHandle ,
@@ -1160,3 +1202,54 @@ impl MapInfo {
11601202 MapFlags :: from_bits_truncate ( self . info . map_flags as u64 )
11611203 }
11621204}
1205+
1206+ #[ cfg( test) ]
1207+ mod tests {
1208+ use super :: * ;
1209+
1210+ use std:: mem:: discriminant;
1211+
1212+ #[ test]
1213+ fn map_type ( ) {
1214+ use MapType :: * ;
1215+
1216+ for t in [
1217+ Unspec ,
1218+ Hash ,
1219+ Array ,
1220+ ProgArray ,
1221+ PerfEventArray ,
1222+ PercpuHash ,
1223+ PercpuArray ,
1224+ StackTrace ,
1225+ CgroupArray ,
1226+ LruHash ,
1227+ LruPercpuHash ,
1228+ LpmTrie ,
1229+ ArrayOfMaps ,
1230+ HashOfMaps ,
1231+ Devmap ,
1232+ Sockmap ,
1233+ Cpumap ,
1234+ Xskmap ,
1235+ Sockhash ,
1236+ CgroupStorage ,
1237+ ReuseportSockarray ,
1238+ PercpuCgroupStorage ,
1239+ Queue ,
1240+ Stack ,
1241+ SkStorage ,
1242+ DevmapHash ,
1243+ StructOps ,
1244+ RingBuf ,
1245+ InodeStorage ,
1246+ TaskStorage ,
1247+ BloomFilter ,
1248+ UserRingBuf ,
1249+ Unknown ,
1250+ ] {
1251+ // check if discriminants match after a roundtrip conversion
1252+ assert_eq ! ( discriminant( & t) , discriminant( & MapType :: from( t as u32 ) ) ) ;
1253+ }
1254+ }
1255+ }
0 commit comments