@@ -6,13 +6,13 @@ use binaryninjacore_sys::*;
66use crate :: basic_block:: BasicBlock ;
77use crate :: binary_view:: BinaryView ;
88use crate :: function:: { Function , NativeBlock } ;
9- use crate :: rc:: { Array , CoreArrayProvider , CoreArrayProviderInner } ;
9+ use crate :: rc:: { Array , CoreArrayProvider , CoreArrayProviderInner , Ref , RefCountable } ;
1010use crate :: string:: { BnStrCompatible , BnString } ;
1111
1212pub type ScriptingProviderExecuteResult = BNScriptingProviderExecuteResult ;
1313pub type ScriptingProviderInputReadyState = BNScriptingProviderInputReadyState ;
1414
15- #[ derive( Clone , Copy ) ]
15+ #[ derive( Clone , Copy , Eq , PartialEq , Hash ) ]
1616#[ repr( transparent) ]
1717pub struct ScriptingProvider {
1818 handle : ptr:: NonNull < BNScriptingProvider > ,
@@ -143,16 +143,22 @@ pub struct ScriptingInstance {
143143 handle : ptr:: NonNull < BNScriptingInstance > ,
144144}
145145
146- impl Clone for ScriptingInstance {
147- fn clone ( & self ) -> Self {
148- let result = unsafe { BNNewScriptingInstanceReference ( self . as_raw ( ) ) } ;
149- unsafe { Self :: from_raw ( ptr:: NonNull :: new ( result) . unwrap ( ) ) }
146+ impl ToOwned for ScriptingInstance {
147+ type Owned = Ref < Self > ;
148+
149+ fn to_owned ( & self ) -> Self :: Owned {
150+ unsafe { Self :: inc_ref ( self ) }
150151 }
151152}
152153
153- impl Drop for ScriptingInstance {
154- fn drop ( & mut self ) {
155- unsafe { BNFreeScriptingInstance ( self . as_raw ( ) ) }
154+ unsafe impl RefCountable for ScriptingInstance {
155+ unsafe fn inc_ref ( handle : & Self ) -> Ref < Self > {
156+ let result = unsafe { BNNewScriptingInstanceReference ( handle. as_raw ( ) ) } ;
157+ unsafe { Self :: ref_from_raw ( ptr:: NonNull :: new ( result) . unwrap ( ) ) }
158+ }
159+
160+ unsafe fn dec_ref ( handle : & Self ) {
161+ unsafe { BNFreeScriptingInstance ( handle. as_raw ( ) ) }
156162 }
157163}
158164
@@ -161,6 +167,10 @@ impl ScriptingInstance {
161167 Self { handle }
162168 }
163169
170+ pub ( crate ) unsafe fn ref_from_raw ( handle : ptr:: NonNull < BNScriptingInstance > ) -> Ref < Self > {
171+ Ref :: new ( Self { handle } )
172+ }
173+
164174 pub ( crate ) unsafe fn into_raw ( self ) -> * mut BNScriptingInstance {
165175 mem:: ManuallyDrop :: new ( self ) . handle . as_ptr ( )
166176 }
0 commit comments