Skip to content

Commit 5f72c5d

Browse files
committed
Remove unnecessary Default bound from DeviceSlice::as_host_vec.
1 parent 28d1269 commit 5f72c5d

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

crates/cust/src/memory/device/device_slice.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use bytemuck::{Pod, Zeroable};
1010
use std::mem::{self, size_of};
1111
use std::ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
1212
use std::os::raw::c_void;
13+
use std::slice;
1314

1415
/// Fixed-size device-side slice.
1516
#[derive(Debug, Copy, Clone)]
@@ -22,14 +23,6 @@ pub struct DeviceSlice<T: DeviceCopy> {
2223
unsafe impl<T: Send + DeviceCopy> Send for DeviceSlice<T> {}
2324
unsafe impl<T: Sync + DeviceCopy> Sync for DeviceSlice<T> {}
2425

25-
impl<T: DeviceCopy + Default + Clone> DeviceSlice<T> {
26-
pub fn as_host_vec(&self) -> CudaResult<Vec<T>> {
27-
let mut vec = vec![T::default(); self.len()];
28-
self.copy_to(&mut vec)?;
29-
Ok(vec)
30-
}
31-
}
32-
3326
// This works by faking a regular slice out of the device raw-pointer and the length and transmuting
3427
// I have no idea if this is safe or not. Probably not, though I can't imagine how the compiler
3528
// could possibly know that the pointer is not de-referenceable. I'm banking that we get proper
@@ -81,6 +74,17 @@ impl<T: DeviceCopy> DeviceSlice<T> {
8174
self.ptr
8275
}
8376

77+
pub fn as_host_vec(&self) -> CudaResult<Vec<T>> {
78+
let mut vec = Vec::with_capacity(self.len());
79+
// SAFETY: The slice points to uninitialized memory, but we only write to it. Once it is
80+
// written, all values are valid, so we can (and must) change the length of the vector.
81+
unsafe {
82+
self.copy_to(slice::from_raw_parts_mut(vec.as_mut_ptr(), self.len()))?;
83+
vec.set_len(self.len())
84+
}
85+
Ok(vec)
86+
}
87+
8488
/* TODO (AL): keep these?
8589
/// Divides one DeviceSlice into two at a given index.
8690
///

0 commit comments

Comments
 (0)