1
- use std:: any:: { Any , TypeId } ;
2
- use std:: cell:: { Cell , Ref , RefCell , RefMut , UnsafeCell } ;
1
+ use std:: cell:: UnsafeCell ;
3
2
use std:: hash:: { Hash , Hasher } ;
4
- use std:: ops:: { Deref , DerefMut } ;
5
3
use std:: os:: raw:: { c_int, c_void} ;
6
4
use std:: rc:: Rc ;
7
- use std:: result:: Result as StdResult ;
8
5
use std:: sync:: atomic:: { AtomicI32 , Ordering } ;
9
6
use std:: sync:: Arc ;
10
7
use std:: { fmt, mem, ptr} ;
11
8
12
9
use parking_lot:: Mutex ;
13
- use rustc_hash:: FxHashMap ;
14
10
15
11
use crate :: error:: Result ;
16
12
#[ cfg( not( feature = "luau" ) ) ]
17
13
use crate :: hook:: Debug ;
18
- use crate :: state:: { ExtraData , Lua , LuaGuard , RawLua , WeakLua } ;
14
+ use crate :: state:: { ExtraData , Lua , RawLua , WeakLua } ;
19
15
20
16
#[ cfg( feature = "async" ) ]
21
17
use { crate :: value:: MultiValue , futures_util:: future:: LocalBoxFuture } ;
@@ -24,6 +20,7 @@ use {crate::value::MultiValue, futures_util::future::LocalBoxFuture};
24
20
use serde:: ser:: { Serialize , SerializeTupleStruct , Serializer } ;
25
21
26
22
// Re-export mutex wrappers
23
+ pub use app_data:: { AppData , AppDataRef , AppDataRefMut } ;
27
24
pub ( crate ) use sync:: { ArcReentrantMutexGuard , ReentrantMutex , ReentrantMutexGuard , XRc , XWeak } ;
28
25
29
26
/// Type of Lua integer numbers.
@@ -49,7 +46,7 @@ pub(crate) type Callback<'a> = Box<dyn Fn(&'a RawLua, c_int) -> Result<c_int> +
49
46
50
47
pub ( crate ) struct Upvalue < T > {
51
48
pub ( crate ) data : T ,
52
- pub ( crate ) extra : Rc < UnsafeCell < ExtraData > > ,
49
+ pub ( crate ) extra : XRc < UnsafeCell < ExtraData > > ,
53
50
}
54
51
55
52
pub ( crate ) type CallbackUpvalue = Upvalue < Callback < ' static > > ;
@@ -331,157 +328,7 @@ impl PartialEq for ValueRef {
331
328
}
332
329
}
333
330
334
- #[ derive( Debug , Default ) ]
335
- pub ( crate ) struct AppData {
336
- #[ cfg( not( feature = "send" ) ) ]
337
- container : UnsafeCell < FxHashMap < TypeId , RefCell < Box < dyn Any > > > > ,
338
- #[ cfg( feature = "send" ) ]
339
- container : UnsafeCell < FxHashMap < TypeId , RefCell < Box < dyn Any + Send > > > > ,
340
- borrow : Cell < usize > ,
341
- }
342
-
343
- impl AppData {
344
- #[ track_caller]
345
- pub ( crate ) fn insert < T : MaybeSend + ' static > ( & self , data : T ) -> Option < T > {
346
- match self . try_insert ( data) {
347
- Ok ( data) => data,
348
- Err ( _) => panic ! ( "cannot mutably borrow app data container" ) ,
349
- }
350
- }
351
-
352
- pub ( crate ) fn try_insert < T : MaybeSend + ' static > ( & self , data : T ) -> StdResult < Option < T > , T > {
353
- if self . borrow . get ( ) != 0 {
354
- return Err ( data) ;
355
- }
356
- // SAFETY: we checked that there are no other references to the container
357
- Ok ( unsafe { & mut * self . container . get ( ) }
358
- . insert ( TypeId :: of :: < T > ( ) , RefCell :: new ( Box :: new ( data) ) )
359
- . and_then ( |data| data. into_inner ( ) . downcast :: < T > ( ) . ok ( ) . map ( |data| * data) ) )
360
- }
361
-
362
- #[ track_caller]
363
- pub ( crate ) fn borrow < T : ' static > ( & self , guard : Option < LuaGuard > ) -> Option < AppDataRef < T > > {
364
- let data = unsafe { & * self . container . get ( ) }
365
- . get ( & TypeId :: of :: < T > ( ) ) ?
366
- . borrow ( ) ;
367
- self . borrow . set ( self . borrow . get ( ) + 1 ) ;
368
- Some ( AppDataRef {
369
- data : Ref :: filter_map ( data, |data| data. downcast_ref ( ) ) . ok ( ) ?,
370
- borrow : & self . borrow ,
371
- _guard : guard,
372
- } )
373
- }
374
-
375
- #[ track_caller]
376
- pub ( crate ) fn borrow_mut < T : ' static > (
377
- & self ,
378
- guard : Option < LuaGuard > ,
379
- ) -> Option < AppDataRefMut < T > > {
380
- let data = unsafe { & * self . container . get ( ) }
381
- . get ( & TypeId :: of :: < T > ( ) ) ?
382
- . borrow_mut ( ) ;
383
- self . borrow . set ( self . borrow . get ( ) + 1 ) ;
384
- Some ( AppDataRefMut {
385
- data : RefMut :: filter_map ( data, |data| data. downcast_mut ( ) ) . ok ( ) ?,
386
- borrow : & self . borrow ,
387
- _guard : guard,
388
- } )
389
- }
390
-
391
- #[ track_caller]
392
- pub ( crate ) fn remove < T : ' static > ( & self ) -> Option < T > {
393
- if self . borrow . get ( ) != 0 {
394
- panic ! ( "cannot mutably borrow app data container" ) ;
395
- }
396
- // SAFETY: we checked that there are no other references to the container
397
- unsafe { & mut * self . container . get ( ) }
398
- . remove ( & TypeId :: of :: < T > ( ) ) ?
399
- . into_inner ( )
400
- . downcast :: < T > ( )
401
- . ok ( )
402
- . map ( |data| * data)
403
- }
404
- }
405
-
406
- /// A wrapper type for an immutably borrowed value from an app data container.
407
- ///
408
- /// This type is similar to [`Ref`].
409
- pub struct AppDataRef < ' a , T : ?Sized + ' a > {
410
- data : Ref < ' a , T > ,
411
- borrow : & ' a Cell < usize > ,
412
- _guard : Option < LuaGuard > ,
413
- }
414
-
415
- impl < T : ?Sized > Drop for AppDataRef < ' _ , T > {
416
- fn drop ( & mut self ) {
417
- self . borrow . set ( self . borrow . get ( ) - 1 ) ;
418
- }
419
- }
420
-
421
- impl < T : ?Sized > Deref for AppDataRef < ' _ , T > {
422
- type Target = T ;
423
-
424
- #[ inline]
425
- fn deref ( & self ) -> & Self :: Target {
426
- & self . data
427
- }
428
- }
429
-
430
- impl < T : ?Sized + fmt:: Display > fmt:: Display for AppDataRef < ' _ , T > {
431
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
432
- ( * * self ) . fmt ( f)
433
- }
434
- }
435
-
436
- impl < T : ?Sized + fmt:: Debug > fmt:: Debug for AppDataRef < ' _ , T > {
437
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
438
- ( * * self ) . fmt ( f)
439
- }
440
- }
441
-
442
- /// A wrapper type for a mutably borrowed value from an app data container.
443
- ///
444
- /// This type is similar to [`RefMut`].
445
- pub struct AppDataRefMut < ' a , T : ?Sized + ' a > {
446
- data : RefMut < ' a , T > ,
447
- borrow : & ' a Cell < usize > ,
448
- _guard : Option < LuaGuard > ,
449
- }
450
-
451
- impl < T : ?Sized > Drop for AppDataRefMut < ' _ , T > {
452
- fn drop ( & mut self ) {
453
- self . borrow . set ( self . borrow . get ( ) - 1 ) ;
454
- }
455
- }
456
-
457
- impl < T : ?Sized > Deref for AppDataRefMut < ' _ , T > {
458
- type Target = T ;
459
-
460
- #[ inline]
461
- fn deref ( & self ) -> & Self :: Target {
462
- & self . data
463
- }
464
- }
465
-
466
- impl < T : ?Sized > DerefMut for AppDataRefMut < ' _ , T > {
467
- #[ inline]
468
- fn deref_mut ( & mut self ) -> & mut Self :: Target {
469
- & mut self . data
470
- }
471
- }
472
-
473
- impl < T : ?Sized + fmt:: Display > fmt:: Display for AppDataRefMut < ' _ , T > {
474
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
475
- ( * * self ) . fmt ( f)
476
- }
477
- }
478
-
479
- impl < T : ?Sized + fmt:: Debug > fmt:: Debug for AppDataRefMut < ' _ , T > {
480
- fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
481
- ( * * self ) . fmt ( f)
482
- }
483
- }
484
-
331
+ mod app_data;
485
332
mod sync;
486
333
487
334
#[ cfg( test) ]
0 commit comments