|
1 | 1 | use std::any::{Any, TypeId}; |
2 | | -use std::cell::{Cell, Ref, RefCell, RefMut, UnsafeCell}; |
| 2 | +use std::cell::{BorrowError, BorrowMutError, Cell, Ref, RefCell, RefMut, UnsafeCell}; |
3 | 3 | use std::fmt; |
4 | 4 | use std::ops::{Deref, DerefMut}; |
5 | 5 | use std::result::Result as StdResult; |
@@ -41,30 +41,66 @@ impl AppData { |
41 | 41 | .and_then(|data| data.into_inner().downcast::<T>().ok().map(|data| *data))) |
42 | 42 | } |
43 | 43 |
|
| 44 | + #[inline] |
44 | 45 | #[track_caller] |
45 | 46 | pub(crate) fn borrow<T: 'static>(&self, guard: Option<LuaGuard>) -> Option<AppDataRef<T>> { |
| 47 | + match self.try_borrow(guard) { |
| 48 | + Ok(data) => data, |
| 49 | + Err(err) => panic!("already mutably borrowed: {err:?}"), |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + pub(crate) fn try_borrow<T: 'static>( |
| 54 | + &self, |
| 55 | + guard: Option<LuaGuard>, |
| 56 | + ) -> Result<Option<AppDataRef<T>>, BorrowError> { |
46 | 57 | let data = unsafe { &*self.container.get() } |
47 | | - .get(&TypeId::of::<T>())? |
48 | | - .borrow(); |
49 | | - self.borrow.set(self.borrow.get() + 1); |
50 | | - Some(AppDataRef { |
51 | | - data: Ref::filter_map(data, |data| data.downcast_ref()).ok()?, |
52 | | - borrow: &self.borrow, |
53 | | - _guard: guard, |
54 | | - }) |
| 58 | + .get(&TypeId::of::<T>()) |
| 59 | + .map(|c| c.try_borrow()) |
| 60 | + .transpose()? |
| 61 | + .and_then(|data| Ref::filter_map(data, |data| data.downcast_ref()).ok()); |
| 62 | + match data { |
| 63 | + Some(data) => { |
| 64 | + self.borrow.set(self.borrow.get() + 1); |
| 65 | + Ok(Some(AppDataRef { |
| 66 | + data, |
| 67 | + borrow: &self.borrow, |
| 68 | + _guard: guard, |
| 69 | + })) |
| 70 | + } |
| 71 | + None => Ok(None), |
| 72 | + } |
55 | 73 | } |
56 | 74 |
|
| 75 | + #[inline] |
57 | 76 | #[track_caller] |
58 | 77 | pub(crate) fn borrow_mut<T: 'static>(&self, guard: Option<LuaGuard>) -> Option<AppDataRefMut<T>> { |
| 78 | + match self.try_borrow_mut(guard) { |
| 79 | + Ok(data) => data, |
| 80 | + Err(err) => panic!("already borrowed: {err:?}"), |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + pub(crate) fn try_borrow_mut<T: 'static>( |
| 85 | + &self, |
| 86 | + guard: Option<LuaGuard>, |
| 87 | + ) -> Result<Option<AppDataRefMut<T>>, BorrowMutError> { |
59 | 88 | let data = unsafe { &*self.container.get() } |
60 | | - .get(&TypeId::of::<T>())? |
61 | | - .borrow_mut(); |
62 | | - self.borrow.set(self.borrow.get() + 1); |
63 | | - Some(AppDataRefMut { |
64 | | - data: RefMut::filter_map(data, |data| data.downcast_mut()).ok()?, |
65 | | - borrow: &self.borrow, |
66 | | - _guard: guard, |
67 | | - }) |
| 89 | + .get(&TypeId::of::<T>()) |
| 90 | + .map(|c| c.try_borrow_mut()) |
| 91 | + .transpose()? |
| 92 | + .and_then(|data| RefMut::filter_map(data, |data| data.downcast_mut()).ok()); |
| 93 | + match data { |
| 94 | + Some(data) => { |
| 95 | + self.borrow.set(self.borrow.get() + 1); |
| 96 | + Ok(Some(AppDataRefMut { |
| 97 | + data, |
| 98 | + borrow: &self.borrow, |
| 99 | + _guard: guard, |
| 100 | + })) |
| 101 | + } |
| 102 | + None => Ok(None), |
| 103 | + } |
68 | 104 | } |
69 | 105 |
|
70 | 106 | #[track_caller] |
|
0 commit comments