Struct PyCapsule
pub struct PyCapsule(/* private fields */);
dep_pyo3
and std
only.Expand description
Represents a Python Capsule as described in Capsules:
This subtype of PyObject represents an opaque value, useful for C extension modules who need to pass an opaque value (as a void* pointer) through Python code to other C code. It is often used to make a C function pointer defined in one module available to other modules, so the regular import mechanism can be used to access C APIs defined in dynamically loaded modules.
Values of this type are accessed via PyO3’s smart pointers, e.g. as
Py<PyCapsule>
or Bound<'py, PyCapsule>
.
For APIs available on capsule objects, see the PyCapsuleMethods
trait which is implemented for
Bound<'py, PyCapsule>
.
§Example
use pyo3::{prelude::*, types::PyCapsule};
use std::ffi::CString;
#[repr(C)]
struct Foo {
pub val: u32,
}
let r = Python::with_gil(|py| -> PyResult<()> {
let foo = Foo { val: 123 };
let name = CString::new("builtins.capsule").unwrap();
let capsule = PyCapsule::new(py, foo, Some(name.clone()))?;
let module = PyModule::import(py, "builtins")?;
module.add("capsule", capsule)?;
let cap: &Foo = unsafe { PyCapsule::import(py, name.as_ref())? };
assert_eq!(cap.val, 123);
Ok(())
});
assert!(r.is_ok());
Implementations§
§impl PyCapsule
impl PyCapsule
pub fn new<T>(
py: Python<'_>,
value: T,
name: Option<CString>,
) -> Result<Bound<'_, PyCapsule>, PyErr> ⓘwhere
T: 'static + Send + AssertNotZeroSized,
pub fn new<T>(
py: Python<'_>,
value: T,
name: Option<CString>,
) -> Result<Bound<'_, PyCapsule>, PyErr> ⓘwhere
T: 'static + Send + AssertNotZeroSized,
Constructs a new capsule whose contents are value
, associated with name
.
name
is the identifier for the capsule; if it is stored as an attribute of a module,
the name should be in the format "modulename.attribute"
.
It is checked at compile time that the type T is not zero-sized. Rust function items
need to be cast to a function pointer (fn(args) -> result
) to be put into a capsule.
§Example
use pyo3::{prelude::*, types::PyCapsule};
use std::ffi::CString;
Python::with_gil(|py| {
let name = CString::new("foo").unwrap();
let capsule = PyCapsule::new(py, 123_u32, Some(name)).unwrap();
let val = unsafe { capsule.reference::<u32>() };
assert_eq!(*val, 123);
});
However, attempting to construct a PyCapsule
with a zero-sized type will not compile:
use pyo3::{prelude::*, types::PyCapsule};
use std::ffi::CString;
Python::with_gil(|py| {
let capsule = PyCapsule::new(py, (), None).unwrap(); // Oops! `()` is zero sized!
});
pub fn new_bound<T>(
py: Python<'_>,
value: T,
name: Option<CString>,
) -> Result<Bound<'_, PyCapsule>, PyErr> ⓘwhere
T: 'static + Send + AssertNotZeroSized,
👎Deprecated since 0.23.0: renamed to PyCapsule::new
pub fn new_bound<T>(
py: Python<'_>,
value: T,
name: Option<CString>,
) -> Result<Bound<'_, PyCapsule>, PyErr> ⓘwhere
T: 'static + Send + AssertNotZeroSized,
PyCapsule::new
Deprecated name for PyCapsule::new
.
pub fn new_with_destructor<T, F>(
py: Python<'_>,
value: T,
name: Option<CString>,
destructor: F,
) -> Result<Bound<'_, PyCapsule>, PyErr> ⓘ
pub fn new_with_destructor<T, F>( py: Python<'_>, value: T, name: Option<CString>, destructor: F, ) -> Result<Bound<'_, PyCapsule>, PyErr> ⓘ
Constructs a new capsule whose contents are value
, associated with name
.
Also provides a destructor: when the PyCapsule
is destroyed, it will be passed the original object,
as well as a *mut c_void
which will point to the capsule’s context, if any.
The destructor
must be Send
, because there is no guarantee which thread it will eventually
be called from.
pub fn new_bound_with_destructor<T, F>(
py: Python<'_>,
value: T,
name: Option<CString>,
destructor: F,
) -> Result<Bound<'_, PyCapsule>, PyErr> ⓘ
👎Deprecated since 0.23.0: renamed to PyCapsule::new_with_destructor
pub fn new_bound_with_destructor<T, F>( py: Python<'_>, value: T, name: Option<CString>, destructor: F, ) -> Result<Bound<'_, PyCapsule>, PyErr> ⓘ
PyCapsule::new_with_destructor
Deprecated name for PyCapsule::new_with_destructor
.
pub unsafe fn import<'py, T>(
py: Python<'py>,
name: &CStr,
) -> Result<&'py T, PyErr> ⓘ
pub unsafe fn import<'py, T>( py: Python<'py>, name: &CStr, ) -> Result<&'py T, PyErr> ⓘ
Imports an existing capsule.
The name
should match the path to the module attribute exactly in the form
of "module.attribute"
, which should be the same as the name within the capsule.
§Safety
It must be known that the capsule imported by name
contains an item of type T
.
Trait Implementations§
§impl PyTypeInfo for PyCapsule
impl PyTypeInfo for PyCapsule
§fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
fn type_object_raw(py: Python<'_>) -> *mut PyTypeObject
§fn is_type_of_bound(obj: &Bound<'_, PyAny>) -> bool
fn is_type_of_bound(obj: &Bound<'_, PyAny>) -> bool
PyTypeInfo::is_type_of
PyTypeInfo::is_type_of
.§fn type_object(py: Python<'_>) -> Bound<'_, PyType>
fn type_object(py: Python<'_>) -> Bound<'_, PyType>
§fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType>
fn type_object_bound(py: Python<'_>) -> Bound<'_, PyType>
PyTypeInfo::type_object
PyTypeInfo::type_object
.§fn is_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_type_of(object: &Bound<'_, PyAny>) -> bool
object
is an instance of this type or a subclass of this type.§fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of(object: &Bound<'_, PyAny>) -> bool
object
is an instance of this type.§fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool
fn is_exact_type_of_bound(object: &Bound<'_, PyAny>) -> bool
PyTypeInfo::is_exact_type_of
PyTypeInfo::is_exact_type_of
.impl DerefToPyAny for PyCapsule
Auto Trait Implementations§
impl !Freeze for PyCapsule
impl !RefUnwindSafe for PyCapsule
impl !Send for PyCapsule
impl !Sync for PyCapsule
impl Unpin for PyCapsule
impl UnwindSafe for PyCapsule
Blanket Implementations§
§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
§type ArchivedMetadata = ()
type ArchivedMetadata = ()
§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
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
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize ⓘ
fn byte_align(&self) -> usize ⓘ
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of_val(&self) -> usize ⓘ
fn mem_align_of_val(&self) -> usize ⓘ
Source§fn mem_size_of_val(&self) -> usize ⓘ
fn mem_size_of_val(&self) -> usize ⓘ
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
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> ⓘ
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> ⓘ
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§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError> ⓘ
§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out
indicating that a T
is niched.