@@ -235,12 +235,14 @@ fn test_scope_userdata_values() -> Result<()> {
235235
236236#[ test]
237237fn test_scope_userdata_mismatch ( ) -> Result < ( ) > {
238- struct MyUserData < ' a > ( & ' a Cell < i64 > ) ;
238+ struct MyUserData < ' a > ( & ' a mut i64 ) ;
239239
240240 impl < ' a > UserData for MyUserData < ' a > {
241241 fn register ( reg : & mut UserDataRegistry < Self > ) {
242- reg. add_method ( "inc" , |_, data, ( ) | {
243- data. 0 . set ( data. 0 . get ( ) + 1 ) ;
242+ reg. add_method ( "get" , |_, data, ( ) | Ok ( * data. 0 ) ) ;
243+
244+ reg. add_method_mut ( "inc" , |_, data, ( ) | {
245+ * data. 0 = data. 0 . wrapping_add ( 1 ) ;
244246 Ok ( ( ) )
245247 } ) ;
246248 }
@@ -251,48 +253,54 @@ fn test_scope_userdata_mismatch() -> Result<()> {
251253 lua. load (
252254 r#"
253255 function inc(a, b) a.inc(b) end
256+ function get(a, b) a.get(b) end
254257 "# ,
255258 )
256259 . exec ( ) ?;
257260
258- let a = Cell :: new ( 1 ) ;
259- let b = Cell :: new ( 1 ) ;
261+ let mut a = 1 ;
262+ let mut b = 1 ;
260263
261- let inc: Function = lua. globals ( ) . get ( "inc" ) ?;
262264 lua. scope ( |scope| {
263- let au = scope. create_userdata ( MyUserData ( & a) ) ?;
264- let bu = scope. create_userdata ( MyUserData ( & b) ) ?;
265- assert ! ( inc. call:: <( ) >( ( & au, & au) ) . is_ok( ) ) ;
266- match inc. call :: < ( ) > ( ( & au, & bu) ) {
267- Err ( Error :: CallbackError { ref cause, .. } ) => match cause. as_ref ( ) {
268- Error :: BadArgument { to, pos, name, cause } => {
269- assert_eq ! ( to. as_deref( ) , Some ( "MyUserData.inc" ) ) ;
270- assert_eq ! ( * pos, 1 ) ;
271- assert_eq ! ( name. as_deref( ) , Some ( "self" ) ) ;
272- assert ! ( matches!( * cause. as_ref( ) , Error :: UserDataTypeMismatch ) ) ;
273- }
274- other => panic ! ( "wrong error type {other:?}" ) ,
275- } ,
276- Err ( other) => panic ! ( "wrong error type {other:?}" ) ,
277- Ok ( _) => panic ! ( "incorrectly returned Ok" ) ,
278- }
279-
280- // Pass non-userdata type
281- let err = inc. call :: < ( ) > ( ( & au, 321 ) ) . err ( ) . unwrap ( ) ;
282- match err {
283- Error :: CallbackError { ref cause, .. } => match cause. as_ref ( ) {
284- Error :: BadArgument { to, pos, name, cause } => {
285- assert_eq ! ( to. as_deref( ) , Some ( "MyUserData.inc" ) ) ;
286- assert_eq ! ( * pos, 1 ) ;
287- assert_eq ! ( name. as_deref( ) , Some ( "self" ) ) ;
288- assert ! ( matches!( * cause. as_ref( ) , Error :: FromLuaConversionError { .. } ) ) ;
289- }
265+ let au = scope. create_userdata ( MyUserData ( & mut a) ) ?;
266+ let bu = scope. create_userdata ( MyUserData ( & mut b) ) ?;
267+ for method_name in [ "get" , "inc" ] {
268+ let f: Function = lua. globals ( ) . get ( method_name) ?;
269+ let full_name = format ! ( "MyUserData.{method_name}" ) ;
270+ let full_name = full_name. as_str ( ) ;
271+
272+ assert ! ( f. call:: <( ) >( ( & au, & au) ) . is_ok( ) ) ;
273+ match f. call :: < ( ) > ( ( & au, & bu) ) {
274+ Err ( Error :: CallbackError { ref cause, .. } ) => match cause. as_ref ( ) {
275+ Error :: BadArgument { to, pos, name, cause } => {
276+ assert_eq ! ( to. as_deref( ) , Some ( full_name) ) ;
277+ assert_eq ! ( * pos, 1 ) ;
278+ assert_eq ! ( name. as_deref( ) , Some ( "self" ) ) ;
279+ assert ! ( matches!( * cause. as_ref( ) , Error :: UserDataTypeMismatch ) ) ;
280+ }
281+ other => panic ! ( "wrong error type {other:?}" ) ,
282+ } ,
283+ Err ( other) => panic ! ( "wrong error type {other:?}" ) ,
284+ Ok ( _) => panic ! ( "incorrectly returned Ok" ) ,
285+ }
286+
287+ // Pass non-userdata type
288+ let err = f. call :: < ( ) > ( ( & au, 321 ) ) . err ( ) . unwrap ( ) ;
289+ match err {
290+ Error :: CallbackError { ref cause, .. } => match cause. as_ref ( ) {
291+ Error :: BadArgument { to, pos, name, cause } => {
292+ assert_eq ! ( to. as_deref( ) , Some ( full_name) ) ;
293+ assert_eq ! ( * pos, 1 ) ;
294+ assert_eq ! ( name. as_deref( ) , Some ( "self" ) ) ;
295+ assert ! ( matches!( * cause. as_ref( ) , Error :: FromLuaConversionError { .. } ) ) ;
296+ }
297+ other => panic ! ( "wrong error type {other:?}" ) ,
298+ } ,
290299 other => panic ! ( "wrong error type {other:?}" ) ,
291- } ,
292- other => panic ! ( "wrong error type {other:?}" ) ,
300+ }
301+ let err_msg = format ! ( "bad argument `self` to `{full_name}`: error converting Lua number to userdata (expected userdata of type 'MyUserData')" ) ;
302+ assert ! ( err. to_string( ) . contains( & err_msg) ) ;
293303 }
294- let err_msg = "bad argument `self` to `MyUserData.inc`: error converting Lua number to userdata (expected userdata of type 'MyUserData')" ;
295- assert ! ( err. to_string( ) . contains( err_msg) ) ;
296304 Ok ( ( ) )
297305 } ) ?;
298306
0 commit comments