forget
unsafe fn forget(x: Tensor<T>) -> Result<(*mut u8, std::alloc::Layout), TensorError>
Transfers ownership of the tensor's memory to the caller, returning the raw pointer and layout information.
Note
This method "forgets" about the tensor's memory without deallocating it, allowing you to take manual control of the memory management. Once called, the tensor's destructor will not free the underlying memory - the caller becomes responsible for proper deallocation.
Parameters:
x
: The input tensor whose memory ownership will be transferred
Returns:
*mut u8
: A raw pointer to the tensor's data std::alloc::Layout
: Can be used to free the memory
Examples:
use hpt::{error::TensorError, Tensor};
fn main() -> Result<(), TensorError> {
// Create a tensor with f32 values
let a = Tensor::<f32>::new([1.5, 2.7, 3.2]);
// Transfer ownership of the memory to the caller
let (raw_ptr, layout) = unsafe { a.forget() }?;
// IMPORTANT: We are now responsible for properly deallocating the memory
// This is unsafe and should be used with caution
unsafe {
std::alloc::dealloc(raw_ptr as *mut u8, layout);
}
Ok(())
}