MCPcopy Create free account
hub / github.com/dgrunwald/rust-cpython / try_leak_immutable

Method try_leak_immutable

src/sharedref.rs:192–208  ·  view source on GitHub ↗

Creates an immutable reference which is not bound to lifetime, returning an error if the value is currently mutably borrowed.

(&self)

Source from the content-addressed store, hash-verified

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

Callers 1

leak_immutableMethod · 0.80

Calls 3

try_borrowMethod · 0.80
current_generationMethod · 0.80
clone_refMethod · 0.45

Tested by

no test coverage detected