@@ -56,6 +56,8 @@ pub type MemberScope = BNMemberScope;
5656////////////////
5757// Confidence
5858
59+ /// Compatible with the `BNType*WithConfidence` types
60+ #[ repr( C ) ]
5961pub struct Conf < T > {
6062 pub contents : T ,
6163 pub confidence : u8 ,
@@ -698,6 +700,7 @@ impl Drop for TypeBuilder {
698700//////////
699701// Type
700702
703+ #[ repr( transparent) ]
701704pub struct Type {
702705 pub ( crate ) handle : * mut BNType ,
703706}
@@ -2447,12 +2450,10 @@ unsafe impl<'a> CoreArrayWrapper<'a> for QualifiedNameTypeAndId {
24472450//////////////////////////
24482451// NameAndType
24492452
2450- pub struct NameAndType < S : BnStrCompatible > {
2451- pub name : S ,
2452- pub t : Conf < Ref < Type > > ,
2453- }
2453+ #[ repr( transparent) ]
2454+ pub struct NameAndType ( pub ( crate ) BNNameAndType ) ;
24542455
2455- impl NameAndType < String > {
2456+ impl NameAndType {
24562457 pub ( crate ) fn from_raw ( raw : & BNNameAndType ) -> Self {
24572458 Self :: new (
24582459 raw_to_string ( raw. name ) . unwrap ( ) ,
@@ -2462,43 +2463,56 @@ impl NameAndType<String> {
24622463 }
24632464}
24642465
2465- impl < S : BnStrCompatible > NameAndType < S > {
2466- pub fn new ( name : S , t : & Ref < Type > , confidence : u8 ) -> Self {
2467- Self {
2468- name,
2469- t : Conf :: new ( t. clone ( ) , confidence) ,
2470- }
2466+ impl NameAndType {
2467+ pub fn new < S : BnStrCompatible > ( name : S , t : & Ref < Type > , confidence : u8 ) -> Self {
2468+ Self ( BNNameAndType {
2469+ name : unsafe { BNAllocString ( name. into_bytes_with_nul ( ) . as_ref ( ) . as_ptr ( ) as * mut _ ) } ,
2470+ type_ : unsafe { Ref :: into_raw ( t. to_owned ( ) ) . handle } ,
2471+ typeConfidence : confidence,
2472+ } )
24712473 }
24722474
24732475 pub ( crate ) fn into_raw ( self ) -> BNNameAndType {
2474- let t = self . t . clone ( ) ;
2475- let res = BNNameAndType {
2476- name : BnString :: new ( self . name ) . into_raw ( ) ,
2477- type_ : t. contents . handle ,
2478- typeConfidence : self . t . confidence ,
2479- } ;
2480- mem:: forget ( t) ;
2481- res
2476+ self . 0
24822477 }
24832478
2484- pub fn type_with_confidence ( & self ) -> Conf < Ref < Type > > {
2485- self . t . clone ( )
2479+ pub fn name ( & self ) -> & str {
2480+ let c_str = unsafe { CStr :: from_ptr ( self . 0 . name ) } ;
2481+ c_str. to_str ( ) . unwrap ( )
2482+ }
2483+
2484+ pub fn t ( & self ) -> & Type {
2485+ unsafe { mem:: transmute :: < _ , & Type > ( & self . 0 . type_ ) }
2486+ }
2487+
2488+ pub fn type_with_confidence ( & self ) -> & Conf < Type > {
2489+ // the struct BNNameAndType contains a Conf inside of it, so this is safe
2490+ unsafe { mem:: transmute :: < _ , & Conf < Type > > ( & self . 0 . type_ ) }
2491+ }
2492+ }
2493+
2494+ impl Drop for NameAndType {
2495+ fn drop ( & mut self ) {
2496+ unsafe {
2497+ BNFreeString ( self . 0 . name ) ;
2498+ BNFreeType ( self . 0 . type_ ) ;
2499+ }
24862500 }
24872501}
24882502
2489- impl < S : BnStrCompatible > CoreArrayProvider for NameAndType < S > {
2503+ impl CoreArrayProvider for NameAndType {
24902504 type Raw = BNNameAndType ;
24912505 type Context = ( ) ;
24922506}
24932507
2494- unsafe impl < S : BnStrCompatible > CoreOwnedArrayProvider for NameAndType < S > {
2508+ unsafe impl CoreOwnedArrayProvider for NameAndType {
24952509 unsafe fn free ( raw : * mut Self :: Raw , count : usize , _context : & Self :: Context ) {
24962510 BNFreeNameAndTypeList ( raw, count) ;
24972511 }
24982512}
24992513
2500- unsafe impl < ' a , S : ' a + BnStrCompatible > CoreArrayWrapper < ' a > for NameAndType < S > {
2501- type Wrapped = & ' a NameAndType < S > ;
2514+ unsafe impl < ' a > CoreArrayWrapper < ' a > for NameAndType {
2515+ type Wrapped = & ' a NameAndType ;
25022516
25032517 unsafe fn wrap_raw ( raw : & ' a Self :: Raw , _context : & ' a Self :: Context ) -> Self :: Wrapped {
25042518 mem:: transmute ( raw)
@@ -2508,11 +2522,8 @@ unsafe impl<'a, S: 'a + BnStrCompatible> CoreArrayWrapper<'a> for NameAndType<S>
25082522//////////////////
25092523// DataVariable
25102524
2511- pub struct DataVariable {
2512- pub address : u64 ,
2513- pub t : Conf < Ref < Type > > ,
2514- pub auto_discovered : bool ,
2515- }
2525+ #[ repr( transparent) ]
2526+ pub struct DataVariable ( pub ( crate ) BNDataVariable ) ;
25162527
25172528// impl DataVariable {
25182529// pub(crate) fn from_raw(var: &BNDataVariable) -> Self {
@@ -2525,12 +2536,26 @@ pub struct DataVariable {
25252536// }
25262537
25272538impl DataVariable {
2539+ pub fn address ( & self ) -> u64 {
2540+ self . 0 . address
2541+ }
2542+
2543+ pub fn auto_discovered ( & self ) -> & bool {
2544+ unsafe { mem:: transmute ( & self . 0 . autoDiscovered ) }
2545+ }
2546+
2547+ pub fn t ( & self ) -> & Type {
2548+ unsafe { mem:: transmute ( & self . 0 . type_ ) }
2549+ }
2550+
25282551 pub fn type_with_confidence ( & self ) -> Conf < Ref < Type > > {
2529- Conf :: new ( self . t . contents . clone ( ) , self . t . confidence )
2552+ // if it was not for the `autoDiscovered: bool` between `type_` and
2553+ // `typeConfidence` this could have being a reference, like NameAndType
2554+ Conf :: new ( self . t ( ) . to_owned ( ) , self . 0 . typeConfidence )
25302555 }
25312556
25322557 pub fn symbol ( & self , bv : & BinaryView ) -> Option < Ref < Symbol > > {
2533- bv. symbol_by_address ( self . address ) . ok ( )
2558+ bv. symbol_by_address ( self . 0 . address ) . ok ( )
25342559 }
25352560}
25362561
0 commit comments