@@ -7,7 +7,7 @@ use std::os::raw::c_int;
77use std:: string:: String as StdString ;
88
99use crate :: error:: { Error , Result } ;
10- use crate :: state:: Lua ;
10+ use crate :: state:: { Lua , RawLua } ;
1111use crate :: types:: { Callback , MaybeSend } ;
1212use crate :: userdata:: {
1313 AnyUserData , MetaMethod , UserData , UserDataFields , UserDataMethods , UserDataRef , UserDataRefMut ,
@@ -23,13 +23,15 @@ use {
2323 std:: future:: { self , Future } ,
2424} ;
2525
26+ type StaticFieldCallback = Box < dyn FnOnce ( & RawLua ) -> Result < ( ) > + ' static > ;
27+
2628/// Handle to registry for userdata methods and metamethods.
2729pub struct UserDataRegistry < T : ' static > {
2830 // Fields
29- pub ( crate ) fields : Vec < ( String , Callback ) > ,
31+ pub ( crate ) fields : Vec < ( String , StaticFieldCallback ) > ,
3032 pub ( crate ) field_getters : Vec < ( String , Callback ) > ,
3133 pub ( crate ) field_setters : Vec < ( String , Callback ) > ,
32- pub ( crate ) meta_fields : Vec < ( String , Callback ) > ,
34+ pub ( crate ) meta_fields : Vec < ( String , StaticFieldCallback ) > ,
3335
3436 // Methods
3537 pub ( crate ) methods : Vec < ( String , Callback ) > ,
@@ -283,11 +285,13 @@ fn get_function_name<T>(name: &str) -> StdString {
283285impl < T : ' static > UserDataFields < T > for UserDataRegistry < T > {
284286 fn add_field < V > ( & mut self , name : impl ToString , value : V )
285287 where
286- V : IntoLua + Clone + ' static ,
288+ V : IntoLua + ' static ,
287289 {
288290 let name = name. to_string ( ) ;
289- let callback: Callback = Box :: new ( move |lua, _| unsafe { value. clone ( ) . push_into_stack_multi ( lua) } ) ;
290- self . fields . push ( ( name, callback) ) ;
291+ self . fields . push ( (
292+ name,
293+ Box :: new ( move |rawlua| unsafe { value. push_into_stack ( rawlua) } ) ,
294+ ) ) ;
291295 }
292296
293297 fn add_field_method_get < M , R > ( & mut self , name : impl ToString , method : M )
@@ -332,28 +336,28 @@ impl<T: 'static> UserDataFields<T> for UserDataRegistry<T> {
332336
333337 fn add_meta_field < V > ( & mut self , name : impl ToString , value : V )
334338 where
335- V : IntoLua + Clone + ' static ,
339+ V : IntoLua + ' static ,
336340 {
337341 let name = name. to_string ( ) ;
338342 self . meta_fields . push ( (
339343 name. clone ( ) ,
340- Box :: new ( move |lua , _ | unsafe {
341- Self :: check_meta_field ( lua . lua ( ) , & name, value. clone ( ) ) ? . push_into_stack_multi ( lua )
344+ Box :: new ( move |rawlua | unsafe {
345+ Self :: check_meta_field ( rawlua . lua ( ) , & name, value) ? . push_into_stack ( rawlua )
342346 } ) ,
343347 ) ) ;
344348 }
345349
346350 fn add_meta_field_with < F , R > ( & mut self , name : impl ToString , f : F )
347351 where
348- F : Fn ( & Lua ) -> Result < R > + MaybeSend + ' static ,
352+ F : FnOnce ( & Lua ) -> Result < R > + ' static ,
349353 R : IntoLua ,
350354 {
351355 let name = name. to_string ( ) ;
352356 self . meta_fields . push ( (
353357 name. clone ( ) ,
354- Box :: new ( move |rawlua, _ | unsafe {
358+ Box :: new ( move |rawlua| unsafe {
355359 let lua = rawlua. lua ( ) ;
356- Self :: check_meta_field ( lua, & name, f ( lua) ?) ?. push_into_stack_multi ( rawlua)
360+ Self :: check_meta_field ( lua, & name, f ( lua) ?) ?. push_into_stack ( rawlua)
357361 } ) ,
358362 ) ) ;
359363 }
0 commit comments