From 1c96f53da88b7d1f8576a2289a9f36fe62bdd93e Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sun, 26 Oct 2025 01:26:35 +0200 Subject: [PATCH 1/3] chore(api): add an unsafe helper --- src/lib.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 2a6b74a..4169609 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -190,6 +190,14 @@ impl DLPackTensor { pub fn byte_offset(&self) -> usize { self.as_ref().byte_offset() } + + /// SAFETY: Only call this if you know you are taking ownership for FFI. + pub fn into_raw_ptr(self) -> *mut sys::DLManagedTensorVersioned { + let ptr = self.raw.as_ptr(); + // Prevent Drop from running (ownership transferred) + std::mem::forget(self); + ptr + } } /// A reference to a DLPack tensor, with data borrowed from some owner, From 9c6fde2dabae3f523446262c009a3acf3e82e4c5 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sun, 26 Oct 2025 01:38:21 +0200 Subject: [PATCH 2/3] chore(api): publicize some more bits --- src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4169609..064b7df 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,9 +28,7 @@ use std::{ffi::c_void, ptr::NonNull}; pub mod sys; mod data_types; - -pub use self::data_types::CastError; -use self::data_types::DLPackPointerCast; +pub use self::data_types::{CastError, DLPackPointerCast, GetDLPackDataType}; /// A managed DLPack tensor, carrying ownership of the data. /// From f4dd38b8c3bc6dc6c38aa1516e8357ba3140793c Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Sun, 26 Oct 2025 02:15:37 +0200 Subject: [PATCH 3/3] chore(api): compiler driven --- src/lib.rs | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 064b7df..18cc60d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -142,6 +142,42 @@ impl DLPackTensor { } } + /// Gets a raw mutable pointer to the underlying [`sys::DLManagedTensorVersioned`] + /// struct. + /// + /// This function borrows the `DLPackTensor` and does **not** transfer + /// ownership. The `Drop` implementation of this `DLPackTensor` will + /// still run when it goes out of scope, calling the `deleter` function. + /// + /// # Safety + /// + /// The caller must ensure that the returned pointer does not outlive + /// this `DLPackTensor` wrapper. The caller must not call the `deleter` + /// function on this pointer, as the wrapper will do so on drop. + pub fn as_mut_ptr(&self) -> *mut sys::DLManagedTensorVersioned { + self.raw.as_ptr() + } + + /// Consumes the tensor and returns the raw pointer to the + /// [`sys::DLManagedTensorVersioned`] struct. + /// + /// # Safety + /// + /// Only call this if you know you are taking ownership for FFI. + /// This function **transfers ownership** of the tensor to the caller. + /// + /// The `Drop` implementation of `DLPackTensor` will **not** be called. + /// The caller is now responsible for freeing this tensor, + /// typically by passing it to a C API or another library that will + /// eventually call its `deleter` function. + /// + /// Failure to call the `deleter` will result in a memory leak. + pub fn into_raw(self) -> *mut sys::DLManagedTensorVersioned { + let ptr = self.raw.as_ptr(); + std::mem::forget(self); + ptr + } + /// Get a pointer to data in this tensor. This pointer can be a device /// pointer according to [`DLPackTensor::device`]. pub fn data_ptr(&self) -> Result<*const T, CastError> where T: DLPackPointerCast { @@ -188,14 +224,6 @@ impl DLPackTensor { pub fn byte_offset(&self) -> usize { self.as_ref().byte_offset() } - - /// SAFETY: Only call this if you know you are taking ownership for FFI. - pub fn into_raw_ptr(self) -> *mut sys::DLManagedTensorVersioned { - let ptr = self.raw.as_ptr(); - // Prevent Drop from running (ownership transferred) - std::mem::forget(self); - ptr - } } /// A reference to a DLPack tensor, with data borrowed from some owner,