@@ -13,6 +13,7 @@ use std::ops::{
13
13
Index , IndexMut , Range , RangeFrom , RangeFull , RangeInclusive , RangeTo , RangeToInclusive ,
14
14
} ;
15
15
use std:: os:: raw:: c_void;
16
+ use std:: slice;
16
17
use std:: ptr:: { slice_from_raw_parts, slice_from_raw_parts_mut} ;
17
18
18
19
/// Fixed-size device-side slice.
@@ -35,14 +36,6 @@ impl<T: DeviceCopy> Debug for DeviceSlice<T> {
35
36
}
36
37
}
37
38
38
- impl < T : DeviceCopy + Default + Clone > DeviceSlice < T > {
39
- pub fn as_host_vec ( & self ) -> CudaResult < Vec < T > > {
40
- let mut vec = vec ! [ T :: default ( ) ; self . len( ) ] ;
41
- self . copy_to ( & mut vec) ?;
42
- Ok ( vec)
43
- }
44
- }
45
-
46
39
// This works by faking a regular slice out of the device raw-pointer and the length and transmuting
47
40
// I have no idea if this is safe or not. Probably not, though I can't imagine how the compiler
48
41
// could possibly know that the pointer is not de-referenceable. I'm banking that we get proper
@@ -94,6 +87,17 @@ impl<T: DeviceCopy> DeviceSlice<T> {
94
87
DevicePointer :: from_raw ( self as * const _ as * const ( ) as usize as u64 )
95
88
}
96
89
90
+ pub fn as_host_vec ( & self ) -> CudaResult < Vec < T > > {
91
+ let mut vec = Vec :: with_capacity ( self . len ( ) ) ;
92
+ // SAFETY: The slice points to uninitialized memory, but we only write to it. Once it is
93
+ // written, all values are valid, so we can (and must) change the length of the vector.
94
+ unsafe {
95
+ self . copy_to ( slice:: from_raw_parts_mut ( vec. as_mut_ptr ( ) , self . len ( ) ) ) ?;
96
+ vec. set_len ( self . len ( ) )
97
+ }
98
+ Ok ( vec)
99
+ }
100
+
97
101
/* TODO (AL): keep these?
98
102
/// Divides one DeviceSlice into two at a given index.
99
103
///
0 commit comments