@@ -220,6 +220,15 @@ impl<T: UserDataTrait> ScopedBorrowedUserData<T> {
220220 fn alive ( & self ) -> bool {
221221 self . active . get ( )
222222 }
223+
224+ #[ allow( clippy:: mut_from_ref) ]
225+ fn mutate ( & self ) -> Option < & mut T > {
226+ if self . alive ( ) {
227+ Some ( unsafe { & mut * self . ptr } )
228+ } else {
229+ None
230+ }
231+ }
223232}
224233
225234impl < T : UserDataTrait > UserDataTrait for ScopedBorrowedUserData < T > {
@@ -232,129 +241,83 @@ impl<T: UserDataTrait> UserDataTrait for ScopedBorrowedUserData<T> {
232241 }
233242
234243 fn get_field ( & self , key : & str ) -> Option < UdValue > {
235- if !self . alive ( ) {
236- return None ;
237- }
238- unsafe { & * self . ptr } . get_field ( key)
244+ self . mutate ( ) ?. get_field ( key)
239245 }
240246
241247 fn set_field ( & mut self , key : & str , value : UdValue ) -> Option < Result < ( ) , String > > {
242248 if !self . alive ( ) {
243249 return Some ( Err ( scoped_expired_error ( ) . to_owned ( ) ) ) ;
244250 }
245- unsafe { & mut * self . ptr } . set_field ( key, value)
251+ self . mutate ( ) ? . set_field ( key, value)
246252 }
247253
248254 fn lua_tostring ( & self ) -> Option < String > {
249- if !self . alive ( ) {
250- return Some ( scoped_expired_error ( ) . to_owned ( ) ) ;
251- }
252- unsafe { & * self . ptr } . lua_tostring ( )
255+ self . mutate ( ) ?. lua_tostring ( )
253256 }
254257
255258 fn lua_eq ( & self , other : & dyn UserDataTrait ) -> Option < bool > {
256- if !self . alive ( ) {
257- return Some ( false ) ;
258- }
259- unsafe { & * self . ptr } . lua_eq ( other)
259+ self . mutate ( ) ?. lua_eq ( other)
260260 }
261261
262262 fn lua_lt ( & self , other : & dyn UserDataTrait ) -> Option < bool > {
263- if !self . alive ( ) {
264- return None ;
265- }
266- unsafe { & * self . ptr } . lua_lt ( other)
263+ self . mutate ( ) ?. lua_lt ( other)
267264 }
268265
269266 fn lua_le ( & self , other : & dyn UserDataTrait ) -> Option < bool > {
270- if !self . alive ( ) {
271- return None ;
272- }
273- unsafe { & * self . ptr } . lua_le ( other)
267+ self . mutate ( ) ?. lua_le ( other)
274268 }
275269
276270 fn lua_len ( & self ) -> Option < UdValue > {
277- if !self . alive ( ) {
278- return None ;
279- }
280- unsafe { & * self . ptr } . lua_len ( )
271+ self . mutate ( ) ?. lua_len ( )
281272 }
282273
283274 fn lua_unm ( & self ) -> Option < UdValue > {
284- if !self . alive ( ) {
285- return None ;
286- }
287- unsafe { & * self . ptr } . lua_unm ( )
275+ self . mutate ( ) ?. lua_unm ( )
288276 }
289277
290278 fn lua_add ( & self , other : & UdValue ) -> Option < UdValue > {
291- if !self . alive ( ) {
292- return None ;
293- }
294- unsafe { & * self . ptr } . lua_add ( other)
279+ self . mutate ( ) ?. lua_add ( other)
295280 }
296281
297282 fn lua_sub ( & self , other : & UdValue ) -> Option < UdValue > {
298- if !self . alive ( ) {
299- return None ;
300- }
301- unsafe { & * self . ptr } . lua_sub ( other)
283+ self . mutate ( ) ?. lua_sub ( other)
302284 }
303285
304286 fn lua_mul ( & self , other : & UdValue ) -> Option < UdValue > {
305- if !self . alive ( ) {
306- return None ;
307- }
308- unsafe { & * self . ptr } . lua_mul ( other)
287+ self . mutate ( ) ?. lua_mul ( other)
309288 }
310289
311290 fn lua_div ( & self , other : & UdValue ) -> Option < UdValue > {
312- if !self . alive ( ) {
313- return None ;
314- }
315- unsafe { & * self . ptr } . lua_div ( other)
291+ self . mutate ( ) ?. lua_div ( other)
316292 }
317293
318294 fn lua_mod ( & self , other : & UdValue ) -> Option < UdValue > {
319- if !self . alive ( ) {
320- return None ;
321- }
322- unsafe { & * self . ptr } . lua_mod ( other)
295+ self . mutate ( ) ?. lua_mod ( other)
323296 }
324297
325298 fn lua_concat ( & self , other : & UdValue ) -> Option < UdValue > {
326- if !self . alive ( ) {
327- return None ;
328- }
329- unsafe { & * self . ptr } . lua_concat ( other)
299+ self . mutate ( ) ?. lua_concat ( other)
330300 }
331301
332302 fn lua_call ( & self ) -> Option < crate :: CFunction > {
333- if !self . alive ( ) {
334- return None ;
335- }
336- unsafe { & * self . ptr } . lua_call ( )
303+ self . mutate ( ) ?. lua_call ( )
337304 }
338305
339306 fn lua_next ( & self , control : & UdValue ) -> Option < ( UdValue , UdValue ) > {
340- if !self . alive ( ) {
341- return None ;
342- }
343- unsafe { & * self . ptr } . lua_next ( control)
307+ self . mutate ( ) ?. lua_next ( control)
344308 }
345309
346310 fn field_names ( & self ) -> & ' static [ & ' static str ] {
347- if !self . alive ( ) {
348- return & [ ] ;
311+ match self . mutate ( ) {
312+ Some ( m) => m. field_names ( ) ,
313+ None => & [ ] ,
349314 }
350- unsafe { & * self . ptr } . field_names ( )
351315 }
352316
353317 fn as_any ( & self ) -> & dyn Any {
354- if self . alive ( ) {
355- unsafe { & * self . ptr } . as_any ( )
356- } else {
357- self
318+ match self . mutate ( ) {
319+ Some ( m) => m. as_any ( ) ,
320+ None => self ,
358321 }
359322 }
360323
0 commit comments