Creates an immutable reference which is not bound to lifetime, returning an error if the value is currently mutably borrowed.
(&self)
| 190 | /// Creates an immutable reference which is not bound to lifetime, |
| 191 | /// returning an error if the value is currently mutably borrowed. |
| 192 | pub fn try_leak_immutable(&self) -> result::Result<UnsafePyLeaked<&'static T>, BorrowError> { |
| 193 | // make sure self.data isn't mutably borrowed; otherwise the |
| 194 | // generation number wouldn't be trusted. |
| 195 | let data_ref = self.try_borrow()?; |
| 196 | |
| 197 | // keep reference to the owner so the data and state are alive, |
| 198 | // but the data pointer can be invalidated by borrow_mut(). |
| 199 | // the state wouldn't since it is immutable. |
| 200 | let state_ptr: *const PySharedState = self.state; |
| 201 | let data_ptr: *const T = &*data_ref; |
| 202 | Ok(UnsafePyLeaked::<&'static T> { |
| 203 | owner: self.owner.clone_ref(self.py), |
| 204 | state: unsafe { &*state_ptr }, |
| 205 | generation: self.state.current_generation(self.py), |
| 206 | data: unsafe { &*data_ptr }, |
| 207 | }) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /// The shared state between Python and Rust |
no test coverage detected