@@ -56,6 +56,7 @@ pub type MemberScope = BNMemberScope;
5656////////////////
5757// Confidence
5858
59+ /// Compatible with the `BNType*WithConfidence` types
5960pub struct Conf < T > {
6061 pub contents : T ,
6162 pub confidence : u8 ,
@@ -698,6 +699,7 @@ impl Drop for TypeBuilder {
698699//////////
699700// Type
700701
702+ #[ repr( transparent) ]
701703pub struct Type {
702704 pub ( crate ) handle : * mut BNType ,
703705}
@@ -2447,13 +2449,11 @@ unsafe impl CoreArrayWrapper for QualifiedNameTypeAndId {
24472449//////////////////////////
24482450// NameAndType
24492451
2450- pub struct NameAndType < S : BnStrCompatible > {
2451- pub name : S ,
2452- pub t : Conf < Ref < Type > > ,
2453- }
2452+ #[ repr( transparent) ]
2453+ pub struct NameAndType ( pub ( crate ) BNNameAndType ) ;
24542454
2455- impl NameAndType < String > {
2456- pub ( crate ) fn from_raw ( raw : & BNNameAndType ) -> Self {
2455+ impl NameAndType {
2456+ pub ( crate ) fn from_raw ( raw : & BNNameAndType ) -> Ref < Self > {
24572457 Self :: new (
24582458 raw_to_string ( raw. name ) . unwrap ( ) ,
24592459 unsafe { & Type :: ref_from_raw ( raw. type_ ) } ,
@@ -2462,43 +2462,73 @@ impl NameAndType<String> {
24622462 }
24632463}
24642464
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) ,
2465+ impl NameAndType {
2466+ pub fn new < S : BnStrCompatible > ( name : S , t : & Type , confidence : u8 ) -> Ref < Self > {
2467+ unsafe {
2468+ Ref :: new ( Self ( BNNameAndType {
2469+ name : BNAllocString ( name. into_bytes_with_nul ( ) . as_ref ( ) . as_ptr ( ) as * mut _ ) ,
2470+ type_ : Ref :: into_raw ( t. to_owned ( ) ) . handle ,
2471+ typeConfidence : confidence,
2472+ } ) )
24702473 }
24712474 }
24722475
24732476 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
2477+ self . 0
24822478 }
24832479
2484- pub fn type_with_confidence ( & self ) -> Conf < Ref < Type > > {
2485- self . t . clone ( )
2480+ pub fn name ( & self ) -> & str {
2481+ let c_str = unsafe { CStr :: from_ptr ( self . 0 . name ) } ;
2482+ c_str. to_str ( ) . unwrap ( )
2483+ }
2484+
2485+ pub fn t ( & self ) -> & Type {
2486+ unsafe { mem:: transmute :: < _ , & Type > ( & self . 0 . type_ ) }
2487+ }
2488+
2489+ pub fn type_with_confidence ( & self ) -> Conf < & Type > {
2490+ Conf :: new ( self . t ( ) , self . 0 . typeConfidence )
2491+ }
2492+ }
2493+
2494+ impl ToOwned for NameAndType {
2495+ type Owned = Ref < Self > ;
2496+
2497+ fn to_owned ( & self ) -> Self :: Owned {
2498+ unsafe { RefCountable :: inc_ref ( self ) }
2499+ }
2500+ }
2501+
2502+ unsafe impl RefCountable for NameAndType {
2503+ unsafe fn inc_ref ( handle : & Self ) -> Ref < Self > {
2504+ Self :: new (
2505+ CStr :: from_ptr ( handle. 0 . name ) ,
2506+ handle. t ( ) ,
2507+ handle. type_with_confidence ( ) . confidence ,
2508+ )
2509+ }
2510+
2511+ unsafe fn dec_ref ( handle : & Self ) {
2512+ unsafe {
2513+ BNFreeString ( handle. 0 . name ) ;
2514+ RefCountable :: dec_ref ( handle. t ( ) ) ;
2515+ }
24862516 }
24872517}
24882518
2489- impl < S : BnStrCompatible > CoreArrayProvider for NameAndType < S > {
2519+ impl CoreArrayProvider for NameAndType {
24902520 type Raw = BNNameAndType ;
24912521 type Context = ( ) ;
24922522}
24932523
2494- unsafe impl < S : BnStrCompatible > CoreOwnedArrayProvider for NameAndType < S > {
2524+ unsafe impl CoreOwnedArrayProvider for NameAndType {
24952525 unsafe fn free ( raw : * mut Self :: Raw , count : usize , _context : & Self :: Context ) {
24962526 BNFreeNameAndTypeList ( raw, count) ;
24972527 }
24982528}
24992529
2500- unsafe impl < S : BnStrCompatible > CoreArrayWrapper for NameAndType < S > {
2501- type Wrapped < ' a > = & ' a NameAndType < S > where S : ' a ;
2530+ unsafe impl CoreArrayWrapper for NameAndType {
2531+ type Wrapped < ' a > = & ' a NameAndType ;
25022532
25032533 unsafe fn wrap_raw < ' a > ( raw : & ' a Self :: Raw , _context : & ' a Self :: Context ) -> Self :: Wrapped < ' a > {
25042534 mem:: transmute ( raw)
@@ -2508,29 +2538,61 @@ unsafe impl<S: BnStrCompatible> CoreArrayWrapper for NameAndType<S> {
25082538//////////////////
25092539// DataVariable
25102540
2511- pub struct DataVariable {
2512- pub address : u64 ,
2513- pub t : Conf < Ref < Type > > ,
2514- pub auto_discovered : bool ,
2515- }
2541+ #[ repr( transparent) ]
2542+ pub struct DataVariable ( pub ( crate ) BNDataVariable ) ;
25162543
25172544// impl DataVariable {
25182545// pub(crate) fn from_raw(var: &BNDataVariable) -> Self {
2519- // Self {
2520- // address: var.address,
2521- // t: Conf::new( unsafe { Type::ref_from_raw (var.type_) }, var.typeConfidence) ,
2522- // auto_discovered: var.autoDiscovered,
2523- // }
2546+ // let var = DataVariable(*var);
2547+ // Self(BNDataVariable {
2548+ // type_: unsafe { Ref::into_raw (var.t().to_owned()).handle } ,
2549+ // .. var.0
2550+ // })
25242551// }
25252552// }
25262553
25272554impl DataVariable {
2528- pub fn type_with_confidence ( & self ) -> Conf < Ref < Type > > {
2529- Conf :: new ( self . t . contents . clone ( ) , self . t . confidence )
2555+ pub fn address ( & self ) -> u64 {
2556+ self . 0 . address
2557+ }
2558+
2559+ pub fn auto_discovered ( & self ) -> bool {
2560+ self . 0 . autoDiscovered
2561+ }
2562+
2563+ pub fn t ( & self ) -> & Type {
2564+ unsafe { mem:: transmute ( & self . 0 . type_ ) }
2565+ }
2566+
2567+ pub fn type_with_confidence ( & self ) -> Conf < & Type > {
2568+ Conf :: new ( self . t ( ) , self . 0 . typeConfidence )
25302569 }
25312570
25322571 pub fn symbol ( & self , bv : & BinaryView ) -> Option < Ref < Symbol > > {
2533- bv. symbol_by_address ( self . address ) . ok ( )
2572+ bv. symbol_by_address ( self . 0 . address ) . ok ( )
2573+ }
2574+ }
2575+
2576+ impl ToOwned for DataVariable {
2577+ type Owned = Ref < Self > ;
2578+
2579+ fn to_owned ( & self ) -> Self :: Owned {
2580+ unsafe { RefCountable :: inc_ref ( self ) }
2581+ }
2582+ }
2583+
2584+ unsafe impl RefCountable for DataVariable {
2585+ unsafe fn inc_ref ( handle : & Self ) -> Ref < Self > {
2586+ unsafe {
2587+ Ref :: new ( Self ( BNDataVariable {
2588+ type_ : Ref :: into_raw ( handle. t ( ) . to_owned ( ) ) . handle ,
2589+ ..handle. 0
2590+ } ) )
2591+ }
2592+ }
2593+
2594+ unsafe fn dec_ref ( handle : & Self ) {
2595+ unsafe { BNFreeType ( handle. 0 . type_ ) }
25342596 }
25352597}
25362598
0 commit comments