Ergonomic API around capsules#5983
Conversation
- A trait `PyCapsuleType` to give the capsule name for the type and encode that the type is pycapsule-safe
- Two wrapper types `PyCapsuleValue` and `PyCapsuleValueRef` that implement `FromPyObject` and `IntoTypeObject` wrapping respectively T and &T
Example of API goal:
```rust
#[pyfunction]
fn add_one(foo: PyCapsuleValueRef<'_, Foo>) -> PyCapsuleValue<Foo> {
PyCapsuleValue(Foo { val: foo.val })
}
```
|
Just out of curiosity, why would someone want this? My understanding is that the PyCapsule API is there to serve use-cases like NumPy's C API where someone wants to expose a C API for a Python library that must be imported before it can be usable from C. Are you trying to enable a similar pattern with PyO3 or something else entirely? |
|
@ngoldbaum Yes! This is this kind of usecases. For example datafusion Python bindings relies a lot on capsule for extensibilities and method definition like: pub fn __datafusion_table_provider__<'py>(
&self,
py: Python<'py>,
session: Bound<PyAny>,
) -> PyResult<Bound<'py, PyCapsule>> {can become pub fn __datafusion_table_provider__(
&self,
session: PyCapsuleValueRef<'_, FFI_LogicalExtensionCodec>,
) -> PyResult<PyCapsuleValue<FFI_TableProvider>> { |
|
Ah, TIL there's an Arrow PyCapsule interface: https://arrow.apache.org/docs/format/CDataInterface/PyCapsuleInterface.html. |
|
A follow up idea: auto implement this trait on all |
PyCapsuleTypeto give the capsule name for the type and encode that the type is pycapsule-safePyCapsuleValueandPyCapsuleValueRefthat implementFromPyObjectandIntoTypeObjectwrapping respectively T and &TExample of API goal: