@@ -33,6 +33,18 @@ pub struct AsyncMethod {
3333 pub callback_param_name : String ,
3434}
3535
36+ #[ derive( Debug , Copy , Clone , PartialEq , PartialOrd ) ]
37+ pub enum DestructionMode {
38+ /// Object is automatically deleted by the GC
39+ Automatic ,
40+ /// Object is disposed of manually by calling a dispose()/close() method
41+ ///
42+ /// For safety, if the user never calls the destruction method, the object
43+ /// will still be deleted by the GC at some point. However, it is
44+ /// strongly advised to take care of the destruction manually.
45+ Manual ,
46+ }
47+
3648/// Object-oriented class definition
3749#[ derive( Debug ) ]
3850pub struct Class {
@@ -43,6 +55,7 @@ pub struct Class {
4355 pub static_methods : Vec < Method > ,
4456 pub async_methods : Vec < AsyncMethod > ,
4557 pub doc : Doc ,
58+ pub destruction_mode : DestructionMode ,
4659}
4760
4861impl Class {
@@ -54,6 +67,10 @@ impl Class {
5467 self . declaration . clone ( )
5568 }
5669
70+ pub fn is_manual_destruction ( & self ) -> bool {
71+ self . destruction_mode == DestructionMode :: Manual && self . destructor . is_some ( )
72+ }
73+
5774 pub fn find_method < T : AsRef < str > > ( & self , method_name : T ) -> Option < & NativeFunctionHandle > {
5875 for method in & self . methods {
5976 if method. name == method_name. as_ref ( ) {
@@ -88,6 +105,7 @@ pub struct ClassBuilder<'a> {
88105 static_methods : Vec < Method > ,
89106 async_methods : Vec < AsyncMethod > ,
90107 doc : Option < Doc > ,
108+ destruction_mode : DestructionMode ,
91109}
92110
93111impl < ' a > ClassBuilder < ' a > {
@@ -101,6 +119,7 @@ impl<'a> ClassBuilder<'a> {
101119 static_methods : Vec :: new ( ) ,
102120 async_methods : Vec :: new ( ) ,
103121 doc : None ,
122+ destruction_mode : DestructionMode :: Automatic ,
104123 }
105124 }
106125
@@ -272,6 +291,11 @@ impl<'a> ClassBuilder<'a> {
272291 Ok ( self )
273292 }
274293
294+ pub fn manual_destroy ( mut self ) -> Result < Self > {
295+ self . destruction_mode = DestructionMode :: Manual ;
296+ Ok ( self )
297+ }
298+
275299 pub fn build ( self ) -> Result < ClassHandle > {
276300 let doc = match self . doc {
277301 Some ( doc) => doc,
@@ -290,6 +314,7 @@ impl<'a> ClassBuilder<'a> {
290314 static_methods : self . static_methods ,
291315 async_methods : self . async_methods ,
292316 doc,
317+ destruction_mode : self . destruction_mode ,
293318 } ) ;
294319
295320 self . lib
0 commit comments