Skip to content

Commit d64e36a

Browse files
committed
Make DLPackTensorRef Clone and Copy, like &T
1 parent 6a2d6e2 commit d64e36a

4 files changed

Lines changed: 11 additions & 10 deletions

File tree

src/lib.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ struct DLTensorDebug<'a>(&'a sys::DLTensor);
195195
impl std::fmt::Debug for DLTensorDebug<'_> {
196196
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
197197
let tensor_ref = unsafe {
198-
DLPackTensorRef::from_raw(self.0.clone())
198+
DLPackTensorRef::from_raw(*self.0)
199199
};
200200
debug_tensor(&tensor_ref, f, "DLTensor")
201201
}
@@ -300,7 +300,7 @@ impl DLPackTensor {
300300
pub fn as_ref(&self) -> DLPackTensorRef<'_> {
301301
unsafe {
302302
// SAFETY: we are constaining the returned reference lifetime
303-
DLPackTensorRef::from_raw(self.raw.as_ref().dl_tensor.clone())
303+
DLPackTensorRef::from_raw(self.raw.as_ref().dl_tensor)
304304
}
305305
}
306306

@@ -320,7 +320,7 @@ impl DLPackTensor {
320320
// SAFETY: we are constraining the returned reference lifetime
321321
// the caller must ensure that the uniqueness check doesn't apply
322322
// i.e. they're fine mutating an ArcArray with refcount > 1
323-
DLPackTensorRefMut::from_raw(self.raw.as_ref().dl_tensor.clone())
323+
DLPackTensorRefMut::from_raw(self.raw.as_ref().dl_tensor)
324324
}
325325
}
326326

@@ -434,6 +434,7 @@ impl DLPackTensor {
434434

435435
/// A reference to a DLPack tensor, with data borrowed from some owner,
436436
/// potentially in another language.
437+
#[derive(Clone, Copy)]
437438
pub struct DLPackTensorRef<'a> {
438439
pub raw: sys::DLTensor,
439440
phantom: std::marker::PhantomData<&'a [u8]>,
@@ -519,7 +520,7 @@ impl<'a> DLPackTensorRef<'a> {
519520
/// potentially in another language.
520521
pub struct DLPackTensorRefMut<'a> {
521522
raw: sys::DLTensor,
522-
phantom: std::marker::PhantomData<&'a [u8]>,
523+
phantom: std::marker::PhantomData<&'a mut [u8]>,
523524
}
524525

525526
impl std::fmt::Debug for DLPackTensorRefMut<'_> {
@@ -548,7 +549,7 @@ impl<'a> DLPackTensorRefMut<'a> {
548549
pub fn as_ref(&self) -> DLPackTensorRef<'_> {
549550
unsafe {
550551
// SAFETY: we are constaining the returned reference lifetime
551-
DLPackTensorRef::from_raw(self.raw.clone())
552+
DLPackTensorRef::from_raw(self.raw)
552553
}
553554
}
554555

@@ -624,10 +625,12 @@ pub mod sync;
624625

625626
/// Small wrapper type to mark a tensor as read-only when there is a choice
626627
/// between a read-only and a read-write implementation.
628+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
627629
pub struct ReadOnly<T>(pub T);
628630

629631
/// Small wrapper type to mark a tensor as read-write when there is a choice
630632
/// between a read-only and a read-write implementation.
633+
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
631634
pub struct ReadWrite<T>(pub T);
632635

633636

src/ndarray/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ mod tests {
677677

678678
// To check correctness, we can create a view from the managed tensor's data.
679679
let view = unsafe {
680-
let tensor_ref = DLPackTensorRef::from_raw(raw.clone());
680+
let tensor_ref = DLPackTensorRef::from_raw(*raw);
681681
ndarray::ArrayView2::<i64>::try_from(tensor_ref).unwrap()
682682
};
683683
assert_eq!(view, arr2(&[[1, 2, 3], [4, 5, 6]]));

src/pyo3.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -267,9 +267,7 @@ impl<'py> TryFrom<Bound<'py, PyCapsule>> for DLPackTensorRef<'py> {
267267

268268
// SAFETY: The lifetime of the returned reference is tied to the
269269
// lifetime GIL lifetime.
270-
let tensor = unsafe {
271-
DLPackTensorRef::from_raw(dltensor.clone())
272-
};
270+
let tensor = unsafe { DLPackTensorRef::from_raw(*dltensor) };
273271

274272
Ok(tensor)
275273
})

src/sys.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ impl std::fmt::Display for DLDataType {
246246

247247
/// Plain C Tensor object, does not manage memory.
248248
#[repr(C)]
249-
#[derive(Clone, Debug)]
249+
#[derive(Clone, Copy, Debug)]
250250
pub struct DLTensor {
251251
/// The data pointer points to the allocated data. This will be CUDA device
252252
/// pointer or cl_mem handle in OpenCL. It may be opaque on some device

0 commit comments

Comments
 (0)