@@ -3,7 +3,7 @@ use std::time::{Duration, SystemTime, UNIX_EPOCH};
33
44use binaryninjacore_sys:: * ;
55
6- use crate :: rc:: { Array , CoreArrayProvider , CoreArrayProviderInner } ;
6+ use crate :: rc:: { Array , CoreArrayProvider , CoreArrayProviderInner , Guard , Ref , RefCountable } ;
77use crate :: string:: { BnStrCompatible , BnString } ;
88
99pub type PluginType = BNPluginType ;
@@ -16,45 +16,47 @@ pub struct RepositoryManager {
1616 handle : ptr:: NonNull < BNRepositoryManager > ,
1717}
1818
19- impl Drop for RepositoryManager {
20- fn drop ( & mut self ) {
21- unsafe { BNFreeRepositoryManager ( self . as_raw ( ) ) }
19+ impl ToOwned for RepositoryManager {
20+ type Owned = Ref < Self > ;
21+
22+ fn to_owned ( & self ) -> Self :: Owned {
23+ unsafe { <Self as RefCountable >:: inc_ref ( self ) }
2224 }
2325}
2426
25- impl Clone for RepositoryManager {
26- fn clone ( & self ) -> Self {
27- unsafe {
28- Self :: from_raw (
29- ptr:: NonNull :: new ( BNNewRepositoryManagerReference ( self . as_raw ( ) ) ) . unwrap ( ) ,
30- )
31- }
27+ unsafe impl RefCountable for RepositoryManager {
28+ unsafe fn inc_ref ( handle : & Self ) -> crate :: rc:: Ref < Self > {
29+ Self :: ref_from_raw (
30+ ptr:: NonNull :: new ( BNNewRepositoryManagerReference ( handle. as_raw ( ) ) ) . unwrap ( ) ,
31+ )
3232 }
33- }
3433
35- impl Default for RepositoryManager {
36- fn default ( ) -> Self {
37- let result = unsafe { BNGetRepositoryManager ( ) } ;
38- unsafe { Self :: from_raw ( ptr:: NonNull :: new ( result) . unwrap ( ) ) }
34+ unsafe fn dec_ref ( handle : & Self ) {
35+ BNFreeRepositoryManager ( handle. as_raw ( ) )
3936 }
4037}
4138
4239impl RepositoryManager {
43- pub ( crate ) unsafe fn from_raw ( handle : ptr:: NonNull < BNRepositoryManager > ) -> Self {
44- Self { handle }
40+ pub fn default ( ) -> Ref < Self > {
41+ let result = unsafe { BNGetRepositoryManager ( ) } ;
42+ unsafe { Self :: ref_from_raw ( ptr:: NonNull :: new ( result) . unwrap ( ) ) }
43+ }
44+
45+ pub ( crate ) unsafe fn ref_from_raw ( handle : ptr:: NonNull < BNRepositoryManager > ) -> Ref < Self > {
46+ Ref :: new ( Self { handle } )
4547 }
4648
4749 #[ allow( clippy:: mut_from_ref) ]
4850 pub ( crate ) unsafe fn as_raw ( & self ) -> & mut BNRepositoryManager {
4951 & mut * self . handle . as_ptr ( )
5052 }
5153
52- pub fn new < S : BnStrCompatible > ( plugins_path : S ) -> Self {
54+ pub fn new < S : BnStrCompatible > ( plugins_path : S ) -> Ref < Self > {
5355 let plugins_path = plugins_path. into_bytes_with_nul ( ) ;
5456 let result = unsafe {
5557 BNCreateRepositoryManager ( plugins_path. as_ref ( ) . as_ptr ( ) as * const ffi:: c_char )
5658 } ;
57- unsafe { Self :: from_raw ( ptr:: NonNull :: new ( result) . unwrap ( ) ) }
59+ unsafe { Self :: ref_from_raw ( ptr:: NonNull :: new ( result) . unwrap ( ) ) }
5860 }
5961
6062 /// Check for updates for all managed Repository objects
@@ -109,12 +111,12 @@ impl RepositoryManager {
109111 }
110112
111113 /// Gets the default Repository
112- pub fn default_repository ( & self ) -> Repository {
114+ pub fn default_repository ( & self ) -> Ref < Repository > {
113115 let result = unsafe { BNRepositoryManagerGetDefaultRepository ( self . as_raw ( ) ) } ;
114116 assert ! ( !result. is_null( ) ) ;
115117 // NOTE result is not onwed, we need to clone it
116- let default = unsafe { Repository :: ref_from_raw ( & result) } ;
117- default. clone ( )
118+ let default = unsafe { Repository :: from_raw ( ptr :: NonNull :: new ( result) . unwrap ( ) ) } ;
119+ default. to_owned ( )
118120 }
119121}
120122
@@ -123,17 +125,21 @@ pub struct Repository {
123125 handle : ptr:: NonNull < BNRepository > ,
124126}
125127
126- impl Drop for Repository {
127- fn drop ( & mut self ) {
128- unsafe { BNFreeRepository ( self . as_raw ( ) ) }
128+ impl ToOwned for Repository {
129+ type Owned = Ref < Self > ;
130+
131+ fn to_owned ( & self ) -> Self :: Owned {
132+ unsafe { <Self as RefCountable >:: inc_ref ( self ) }
129133 }
130134}
131135
132- impl Clone for Repository {
133- fn clone ( & self ) -> Self {
134- unsafe {
135- Self :: from_raw ( ptr:: NonNull :: new ( BNNewRepositoryReference ( self . as_raw ( ) ) ) . unwrap ( ) )
136- }
136+ unsafe impl RefCountable for Repository {
137+ unsafe fn inc_ref ( handle : & Self ) -> Ref < Self > {
138+ Self :: ref_from_raw ( ptr:: NonNull :: new ( BNNewRepositoryReference ( handle. as_raw ( ) ) ) . unwrap ( ) )
139+ }
140+
141+ unsafe fn dec_ref ( handle : & Self ) {
142+ BNFreeRepository ( handle. as_raw ( ) )
137143 }
138144}
139145
@@ -142,9 +148,8 @@ impl Repository {
142148 Self { handle }
143149 }
144150
145- pub ( crate ) unsafe fn ref_from_raw ( handle : & * mut BNRepository ) -> & Self {
146- assert ! ( !handle. is_null( ) ) ;
147- mem:: transmute ( handle)
151+ pub ( crate ) unsafe fn ref_from_raw ( handle : ptr:: NonNull < BNRepository > ) -> Ref < Self > {
152+ Ref :: new ( Self { handle } )
148153 }
149154
150155 #[ allow( clippy:: mut_from_ref) ]
@@ -193,16 +198,16 @@ impl Repository {
193198impl CoreArrayProvider for Repository {
194199 type Raw = * mut BNRepository ;
195200 type Context = ( ) ;
196- type Wrapped < ' a > = & ' a Self ;
201+ type Wrapped < ' a > = Guard < ' a , Self > ;
197202}
198203
199204unsafe impl CoreArrayProviderInner for Repository {
200205 unsafe fn free ( raw : * mut Self :: Raw , _count : usize , _context : & Self :: Context ) {
201206 BNFreeRepositoryManagerRepositoriesList ( raw)
202207 }
203208
204- unsafe fn wrap_raw < ' a > ( raw : & ' a Self :: Raw , _context : & ' a Self :: Context ) -> Self :: Wrapped < ' a > {
205- Self :: ref_from_raw ( raw)
209+ unsafe fn wrap_raw < ' a > ( raw : & ' a Self :: Raw , context : & ' a Self :: Context ) -> Self :: Wrapped < ' a > {
210+ Guard :: new ( Self :: from_raw ( ptr :: NonNull :: new ( * raw) . unwrap ( ) ) , context )
206211 }
207212}
208213
@@ -211,15 +216,21 @@ pub struct RepoPlugin {
211216 handle : ptr:: NonNull < BNRepoPlugin > ,
212217}
213218
214- impl Drop for RepoPlugin {
215- fn drop ( & mut self ) {
216- unsafe { BNFreePlugin ( self . as_raw ( ) ) }
219+ impl ToOwned for RepoPlugin {
220+ type Owned = Ref < Self > ;
221+
222+ fn to_owned ( & self ) -> Self :: Owned {
223+ unsafe { <Self as RefCountable >:: inc_ref ( self ) }
217224 }
218225}
219226
220- impl Clone for RepoPlugin {
221- fn clone ( & self ) -> Self {
222- unsafe { Self :: from_raw ( ptr:: NonNull :: new ( BNNewPluginReference ( self . as_raw ( ) ) ) . unwrap ( ) ) }
227+ unsafe impl RefCountable for RepoPlugin {
228+ unsafe fn inc_ref ( handle : & Self ) -> Ref < Self > {
229+ Self :: ref_from_raw ( ptr:: NonNull :: new ( BNNewPluginReference ( handle. as_raw ( ) ) ) . unwrap ( ) )
230+ }
231+
232+ unsafe fn dec_ref ( handle : & Self ) {
233+ BNFreePlugin ( handle. as_raw ( ) )
223234 }
224235}
225236
@@ -228,9 +239,8 @@ impl RepoPlugin {
228239 Self { handle }
229240 }
230241
231- pub ( crate ) unsafe fn ref_from_raw ( handle : & * mut BNRepoPlugin ) -> & Self {
232- assert ! ( !handle. is_null( ) ) ;
233- mem:: transmute ( handle)
242+ pub ( crate ) unsafe fn ref_from_raw ( handle : ptr:: NonNull < BNRepoPlugin > ) -> Ref < Self > {
243+ Ref :: new ( Self { handle } )
234244 }
235245
236246 #[ allow( clippy:: mut_from_ref) ]
@@ -469,16 +479,16 @@ impl RepoPlugin {
469479impl CoreArrayProvider for RepoPlugin {
470480 type Raw = * mut BNRepoPlugin ;
471481 type Context = ( ) ;
472- type Wrapped < ' a > = & ' a Self ;
482+ type Wrapped < ' a > = Guard < ' a , Self > ;
473483}
474484
475485unsafe impl CoreArrayProviderInner for RepoPlugin {
476486 unsafe fn free ( raw : * mut Self :: Raw , _count : usize , _context : & Self :: Context ) {
477487 BNFreeRepositoryPluginList ( raw)
478488 }
479489
480- unsafe fn wrap_raw < ' a > ( raw : & ' a Self :: Raw , _context : & ' a Self :: Context ) -> Self :: Wrapped < ' a > {
481- Self :: ref_from_raw ( raw)
490+ unsafe fn wrap_raw < ' a > ( raw : & ' a Self :: Raw , context : & ' a Self :: Context ) -> Self :: Wrapped < ' a > {
491+ Guard :: new ( Self :: from_raw ( ptr :: NonNull :: new ( * raw) . unwrap ( ) ) , context )
482492 }
483493}
484494
0 commit comments