dlpack-rs
Portable Rust bindings for the DLPack tensor protocol.
dlpack_rs::ffi— hand-written#[repr(C)]ABI types (DLTensor,DLManagedTensor,DLManagedTensorVersioned,DLDevice,DLDataType,DLPackVersion). Nobindgen, no C submodule, no build script.dlpack_rs::safe— ownership-correctpack/pack_versionedbuilders that wrap a Rust keepalive + shape/strides Vecs in a heap-allocatedDLManagedTensor[Versioned]with a correctdeletercallback.dlpack_rs::pyo3_glue(featurepyo3) —IntoDLPackexport trait +PyTensorimport struct; implements the__dlpack__capsule lifecycle (rename-on-consume + deleter-on-drop).
Installation
Add to Cargo.toml:
# no pyo3 glue (pure Rust, no Python dependency):
= { = "https://github.com/kornia/dlpack-rs", = "v0.2.0" }
# with pyo3 glue:
= { = "https://github.com/kornia/dlpack-rs", = "v0.2.0", = ["pyo3"] }
Usage
Raw FFI
use ;
let device = DLDevice ;
let dtype = DLDataType ;
Safe builder (safe::pack)
use ;
let mut data = vec!;
let info = contiguous;
// `data` is the keepalive: it will be dropped when the DLPack deleter fires.
let mt: *mut DLManagedTensor = pack;
// Hand `mt` to a consumer (e.g. Python via a capsule).
// The consumer calls mt.deleter(mt) when done; that drops the keepalive.
pyo3 __dlpack__ (feature pyo3)
Export — give a Python caller a zero-copy view of Rust data:
use IntoDLPack;
use ;
use *;
use c_void;
;
attach;
Import — consume a capsule from any Python object that implements __dlpack__():
use PyTensor;
use *;
attach;
Feature flags
| Feature | Default | Description |
|---|---|---|
pyo3 |
no | Enables pyo3_glue (requires Python headers at build time) |
Without any features (--no-default-features), the crate is a pure no_std-compatible FFI +
safe-builder layer with zero non-std dependencies.
License
Apache-2.0 — see LICENSE.