@@ -34,9 +34,6 @@ use std::path::Path;
3434use std:: ptr;
3535use std:: ptr:: NonNull ;
3636
37- use num_enum:: IntoPrimitive ;
38- use num_enum:: TryFromPrimitive ;
39-
4037use crate :: util:: create_bpf_entity_checked;
4138use crate :: util:: create_bpf_entity_checked_opt;
4239use crate :: util:: parse_ret_i32;
@@ -47,7 +44,7 @@ use crate::Result;
4744use self :: types:: Composite ;
4845
4946/// The various btf types.
50- #[ derive( IntoPrimitive , TryFromPrimitive , Debug , PartialEq , Eq , Clone , Copy ) ]
47+ #[ derive( Debug , PartialEq , Eq , Clone , Copy ) ]
5148#[ repr( u32 ) ]
5249pub enum BtfKind {
5350 /// [Void](types::Void)
@@ -92,6 +89,38 @@ pub enum BtfKind {
9289 Enum64 ,
9390}
9491
92+ impl TryFrom < u32 > for BtfKind {
93+ type Error = u32 ;
94+
95+ fn try_from ( value : u32 ) -> Result < Self , Self :: Error > {
96+ use BtfKind :: * ;
97+
98+ Ok ( match value {
99+ x if x == Void as u32 => Void ,
100+ x if x == Int as u32 => Int ,
101+ x if x == Ptr as u32 => Ptr ,
102+ x if x == Array as u32 => Array ,
103+ x if x == Struct as u32 => Struct ,
104+ x if x == Union as u32 => Union ,
105+ x if x == Enum as u32 => Enum ,
106+ x if x == Fwd as u32 => Fwd ,
107+ x if x == Typedef as u32 => Typedef ,
108+ x if x == Volatile as u32 => Volatile ,
109+ x if x == Const as u32 => Const ,
110+ x if x == Restrict as u32 => Restrict ,
111+ x if x == Func as u32 => Func ,
112+ x if x == FuncProto as u32 => FuncProto ,
113+ x if x == Var as u32 => Var ,
114+ x if x == DataSec as u32 => DataSec ,
115+ x if x == Float as u32 => Float ,
116+ x if x == DeclTag as u32 => DeclTag ,
117+ x if x == TypeTag as u32 => TypeTag ,
118+ x if x == Enum64 as u32 => Enum64 ,
119+ v => return Err ( v) ,
120+ } )
121+ }
122+ }
123+
95124/// The id of a btf type.
96125#[ derive( Debug , Clone , Copy , PartialEq , Eq , PartialOrd , Ord , Hash ) ]
97126pub struct TypeId ( u32 ) ;
@@ -679,8 +708,26 @@ mod sealed {
679708mod tests {
680709 use super :: * ;
681710
711+ use std:: mem:: discriminant;
712+
682713 #[ test]
683714 fn from_vmlinux ( ) {
684715 assert ! ( Btf :: from_vmlinux( ) . is_ok( ) ) ;
685716 }
717+
718+ #[ test]
719+ fn btf_kind ( ) {
720+ use BtfKind :: * ;
721+
722+ for t in [
723+ Void , Int , Ptr , Array , Struct , Union , Enum , Fwd , Typedef , Volatile , Const , Restrict ,
724+ Func , FuncProto , Var , DataSec , Float , DeclTag , TypeTag , Enum64 ,
725+ ] {
726+ // check if discriminants match after a roundtrip conversion
727+ assert_eq ! (
728+ discriminant( & t) ,
729+ discriminant( & BtfKind :: try_from( t as u32 ) . unwrap( ) )
730+ ) ;
731+ }
732+ }
686733}
0 commit comments