@@ -88,11 +88,9 @@ impl<'scope, 'env: 'scope> Scope<'scope, 'env> {
8888 where
8989 T : UserData + ' static ,
9090 {
91- unsafe {
92- let ud = self . lua . make_userdata ( UserDataStorage :: new_ref ( data) ) ?;
93- self . seal_userdata :: < T > ( & ud) ?;
94- Ok ( ud)
95- }
91+ let ud = unsafe { self . lua . make_userdata ( UserDataStorage :: new_ref ( data) ) } ?;
92+ self . seal_userdata :: < T > ( & ud) ;
93+ Ok ( ud)
9694 }
9795
9896 /// Creates a Lua userdata object from a mutable reference to custom userdata type.
@@ -104,11 +102,9 @@ impl<'scope, 'env: 'scope> Scope<'scope, 'env> {
104102 where
105103 T : UserData + ' static ,
106104 {
107- unsafe {
108- let ud = self . lua . make_userdata ( UserDataStorage :: new_ref_mut ( data) ) ?;
109- self . seal_userdata :: < T > ( & ud) ?;
110- Ok ( ud)
111- }
105+ let ud = unsafe { self . lua . make_userdata ( UserDataStorage :: new_ref_mut ( data) ) } ?;
106+ self . seal_userdata :: < T > ( & ud) ;
107+ Ok ( ud)
112108 }
113109
114110 /// Creates a Lua userdata object from a reference to custom Rust type.
@@ -122,11 +118,9 @@ impl<'scope, 'env: 'scope> Scope<'scope, 'env> {
122118 where
123119 T : ' static ,
124120 {
125- unsafe {
126- let ud = self . lua . make_any_userdata ( UserDataStorage :: new_ref ( data) ) ?;
127- self . seal_userdata :: < T > ( & ud) ?;
128- Ok ( ud)
129- }
121+ let ud = unsafe { self . lua . make_any_userdata ( UserDataStorage :: new_ref ( data) ) } ?;
122+ self . seal_userdata :: < T > ( & ud) ;
123+ Ok ( ud)
130124 }
131125
132126 /// Creates a Lua userdata object from a mutable reference to custom Rust type.
@@ -138,11 +132,9 @@ impl<'scope, 'env: 'scope> Scope<'scope, 'env> {
138132 where
139133 T : ' static ,
140134 {
141- unsafe {
142- let ud = self . lua . make_any_userdata ( UserDataStorage :: new_ref_mut ( data) ) ?;
143- self . seal_userdata :: < T > ( & ud) ?;
144- Ok ( ud)
145- }
135+ let ud = unsafe { self . lua . make_any_userdata ( UserDataStorage :: new_ref_mut ( data) ) } ?;
136+ self . seal_userdata :: < T > ( & ud) ;
137+ Ok ( ud)
146138 }
147139
148140 /// Creates a Lua userdata object from a custom userdata type.
@@ -194,7 +186,7 @@ impl<'scope, 'env: 'scope> Scope<'scope, 'env> {
194186 ffi:: lua_setmetatable ( state, -2 ) ;
195187
196188 let ud = AnyUserData ( self . lua . pop_ref ( ) ) ;
197- self . attach_destructor :: < T > ( & ud) ;
189+ self . seal_userdata :: < T > ( & ud) ;
198190
199191 Ok ( ud)
200192 }
@@ -243,32 +235,10 @@ impl<'scope, 'env: 'scope> Scope<'scope, 'env> {
243235
244236 AnyUserData ( self . lua . pop_ref ( ) )
245237 } ;
246- self . attach_destructor :: < T > ( & ud) ;
238+ self . seal_userdata :: < T > ( & ud) ;
247239 Ok ( ud)
248240 }
249241
250- fn attach_destructor < T : ' env > ( & ' scope self , ud : & AnyUserData ) {
251- let destructor: DestructorCallback = Box :: new ( |rawlua, vref| unsafe {
252- let state = rawlua. state ( ) ;
253- let _sg = StackGuard :: new ( state) ;
254- assert_stack ( state, 2 ) ;
255-
256- // Check that userdata is valid (very likely)
257- if rawlua. push_userdata_ref ( & vref) . is_err ( ) {
258- return vec ! [ ] ;
259- }
260-
261- // Deregister metatable
262- let mt_ptr = get_metatable_ptr ( state, -1 ) ;
263- rawlua. deregister_userdata_metatable ( mt_ptr) ;
264-
265- let ud = take_userdata :: < UserDataStorage < T > > ( state) ;
266-
267- vec ! [ Box :: new( move || drop( ud) ) ]
268- } ) ;
269- self . destructors . 0 . borrow_mut ( ) . push ( ( ud. 0 . clone ( ) , destructor) ) ;
270- }
271-
272242 /// Adds a destructor function to be run when the scope ends.
273243 ///
274244 /// This functionality is useful for cleaning up any resources after the scope ends.
@@ -312,23 +282,27 @@ impl<'scope, 'env: 'scope> Scope<'scope, 'env> {
312282 }
313283
314284 /// Shortens the lifetime of the userdata to the lifetime of the scope.
315- unsafe fn seal_userdata < T : ' static > ( & self , ud : & AnyUserData ) -> Result < ( ) > {
316- let destructor: DestructorCallback = Box :: new ( |rawlua, vref| {
285+ fn seal_userdata < T : ' env > ( & self , ud : & AnyUserData ) {
286+ let destructor: DestructorCallback = Box :: new ( |rawlua, vref| unsafe {
317287 let state = rawlua. state ( ) ;
318288 let _sg = StackGuard :: new ( state) ;
319289 assert_stack ( state, 2 ) ;
320290
321291 // Ensure that userdata is not destructed
322- if rawlua. push_userdata_ref ( & vref) . is_err ( ) {
323- return vec ! [ ] ;
292+ match rawlua. push_userdata_ref ( & vref) {
293+ Ok ( Some ( _) ) => { }
294+ Ok ( None ) => {
295+ // Deregister metatable
296+ let mt_ptr = get_metatable_ptr ( state, -1 ) ;
297+ rawlua. deregister_userdata_metatable ( mt_ptr) ;
298+ }
299+ Err ( _) => return vec ! [ ] ,
324300 }
325301
326302 let data = take_userdata :: < UserDataStorage < T > > ( state) ;
327303 vec ! [ Box :: new( move || drop( data) ) ]
328304 } ) ;
329305 self . destructors . 0 . borrow_mut ( ) . push ( ( ud. 0 . clone ( ) , destructor) ) ;
330-
331- Ok ( ( ) )
332306 }
333307}
334308
0 commit comments