Trait pyo3::PyNativeType
source · pub unsafe trait PyNativeType: Sized {
type AsRefSource: HasPyGilRef<AsRefTarget = Self>;
// Provided methods
fn as_borrowed(&self) -> Borrowed<'_, '_, Self::AsRefSource> { ... }
fn py(&self) -> Python<'_> { ... }
unsafe fn unchecked_downcast(obj: &PyAny) -> &Self { ... }
}
Expand description
Types that are built into the Python interpreter.
PyO3 is designed in a way that all references to those types are bound to the GIL, which is why you can get a token from all references of those types.
§Safety
This trait must only be implemented for types which cannot be accessed without the GIL.
Required Associated Types§
sourcetype AsRefSource: HasPyGilRef<AsRefTarget = Self>
type AsRefSource: HasPyGilRef<AsRefTarget = Self>
The form of this which is stored inside a Py<T>
smart pointer.
Provided Methods§
sourcefn as_borrowed(&self) -> Borrowed<'_, '_, Self::AsRefSource>
fn as_borrowed(&self) -> Borrowed<'_, '_, Self::AsRefSource>
Cast &self
to a Borrowed
smart pointer.
Borrowed<T>
implements Deref<Target=Bound<T>>
, so can also be used in locations
where Bound<T>
is expected.
This is available as a migration tool to adjust code from the deprecated “GIL Refs”
API to the Bound
smart pointer API.
sourceunsafe fn unchecked_downcast(obj: &PyAny) -> &Self
unsafe fn unchecked_downcast(obj: &PyAny) -> &Self
Cast &PyAny
to &Self
without no type checking.
§Safety
obj
must have the same layout as *const ffi::PyObject
and must be
an instance of a type corresponding to Self
.