Skip to content

Commit 5c819b3

Browse files
committed
backout VarArray non-copy changes
1 parent 840dbe8 commit 5c819b3

File tree

2 files changed

+24
-28
lines changed

2 files changed

+24
-28
lines changed

hdf5-types/src/array.rs

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,29 @@ use std::ptr;
66
use std::slice;
77

88
#[repr(C)]
9-
pub struct VarLenArray<T> {
9+
pub struct VarLenArray<T: Copy> {
1010
len: usize,
1111
ptr: *const T,
1212
tag: PhantomData<T>,
1313
}
1414

15-
impl<T> VarLenArray<T> {
15+
impl<T: Copy> VarLenArray<T> {
16+
pub unsafe fn from_parts(p: *const T, len: usize) -> Self {
17+
let (len, ptr) = if !p.is_null() && len != 0 {
18+
let dst = crate::malloc(len * mem::size_of::<T>());
19+
ptr::copy_nonoverlapping(p, dst.cast(), len);
20+
(len, dst)
21+
} else {
22+
(0, ptr::null_mut())
23+
};
24+
Self { len, ptr: ptr as *const _, tag: PhantomData }
25+
}
26+
27+
#[inline]
28+
pub fn from_slice(arr: &[T]) -> Self {
29+
unsafe { Self::from_parts(arr.as_ptr(), arr.len()) }
30+
}
31+
1632
#[inline]
1733
pub fn as_ptr(&self) -> *const T {
1834
self.ptr
@@ -30,31 +46,11 @@ impl<T> VarLenArray<T> {
3046

3147
#[inline]
3248
pub fn as_slice(&self) -> &[T] {
33-
unsafe { slice::from_raw_parts(self.as_ptr(), self.len()) }
34-
}
35-
}
36-
37-
impl<T: Copy> VarLenArray<T> {
38-
/// Creates a new `VarLenArray` from a slice by copying the source data.
39-
#[inline]
40-
pub fn from_slice(arr: &[T]) -> Self {
41-
unsafe { Self::from_parts(arr.as_ptr(), arr.len()) }
42-
}
43-
44-
/// Create a new `VarLenArray` from a ptr/len combo by copying the source data.
45-
pub unsafe fn from_parts(p: *const T, len: usize) -> Self {
46-
let (len, ptr) = if !p.is_null() && len != 0 {
47-
let dst = crate::malloc(len * mem::size_of::<T>());
48-
ptr::copy_nonoverlapping(p, dst.cast(), len);
49-
(len, dst)
50-
} else {
51-
(0, ptr::null_mut())
52-
};
53-
Self { len, ptr: ptr as *const _, tag: PhantomData }
49+
self
5450
}
5551
}
5652

57-
impl<T> Drop for VarLenArray<T> {
53+
impl<T: Copy> Drop for VarLenArray<T> {
5854
fn drop(&mut self) {
5955
if !self.ptr.is_null() {
6056
unsafe {
@@ -75,15 +71,15 @@ impl<T: Copy> Clone for VarLenArray<T> {
7571
}
7672
}
7773

78-
impl<T> Deref for VarLenArray<T> {
74+
impl<T: Copy> Deref for VarLenArray<T> {
7975
type Target = [T];
8076

8177
#[inline]
8278
fn deref(&self) -> &[T] {
8379
if self.len == 0 || self.ptr.is_null() {
8480
&[]
8581
} else {
86-
self.as_slice()
82+
unsafe { slice::from_raw_parts(self.as_ptr(), self.len()) }
8783
}
8884
}
8985
}
@@ -139,7 +135,7 @@ impl<T: Copy + PartialEq, const N: usize> PartialEq<[T; N]> for VarLenArray<T> {
139135
}
140136
}
141137

142-
impl<T: fmt::Debug> fmt::Debug for VarLenArray<T> {
138+
impl<T: Copy + fmt::Debug> fmt::Debug for VarLenArray<T> {
143139
#[inline]
144140
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
145141
self.as_slice().fmt(f)

hdf5-types/src/h5type.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ unsafe impl<T: H5Type, const N: usize> H5Type for [T; N] {
347347
}
348348
}
349349

350-
unsafe impl<T: H5Type> H5Type for VarLenArray<T> {
350+
unsafe impl<T: H5Type + Copy> H5Type for VarLenArray<T> {
351351
#[inline]
352352
fn type_descriptor() -> TypeDescriptor {
353353
TypeDescriptor::VarLenArray(Box::new(<T as H5Type>::type_descriptor()))

0 commit comments

Comments
 (0)