pub struct GILProtected<T> { /* private fields */ }
Expand description
Value with concurrent access protected by the GIL.
This is a synchronization primitive based on Python’s global interpreter lock (GIL). It ensures that only one thread at a time can access the inner value via shared references. It can be combined with interior mutability to obtain mutable references.
§Example
Combining GILProtected
with RefCell
enables mutable access to static data:
use pyo3::sync::GILProtected;
use std::cell::RefCell;
static NUMBERS: GILProtected<RefCell<Vec<i32>>> = GILProtected::new(RefCell::new(Vec::new()));
Python::with_gil(|py| {
NUMBERS.get(py).borrow_mut().push(42);
});
Implementations§
Trait Implementations§
impl<T> Sync for GILProtected<T>where
T: Send,
Auto Trait Implementations§
impl<T> Freeze for GILProtected<T>where
T: Freeze,
impl<T> RefUnwindSafe for GILProtected<T>where
T: RefUnwindSafe,
impl<T> Send for GILProtected<T>where
T: Send,
impl<T> Unpin for GILProtected<T>where
T: Unpin,
impl<T> UnwindSafe for GILProtected<T>where
T: UnwindSafe,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more