pub struct GILProtected<T> {
value: T,
}
Available on non-
Py_GIL_DISABLED
only.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.
This type is not defined for extensions built against the free-threaded CPython ABI.
§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);
});
Fields§
§value: T
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> AssertNotZeroSized for T
impl<T> AssertNotZeroSized for T
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
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more