@@ -10,6 +10,7 @@ use bytemuck::{Pod, Zeroable};
10
10
use std:: mem:: { self , size_of} ;
11
11
use std:: ops:: { Range , RangeFrom , RangeFull , RangeInclusive , RangeTo , RangeToInclusive } ;
12
12
use std:: os:: raw:: c_void;
13
+ use std:: slice;
13
14
14
15
/// Fixed-size device-side slice.
15
16
#[ derive( Debug , Copy , Clone ) ]
@@ -22,14 +23,6 @@ pub struct DeviceSlice<T: DeviceCopy> {
22
23
unsafe impl < T : Send + DeviceCopy > Send for DeviceSlice < T > { }
23
24
unsafe impl < T : Sync + DeviceCopy > Sync for DeviceSlice < T > { }
24
25
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
-
33
26
// This works by faking a regular slice out of the device raw-pointer and the length and transmuting
34
27
// I have no idea if this is safe or not. Probably not, though I can't imagine how the compiler
35
28
// 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> {
81
74
self . ptr
82
75
}
83
76
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
+
84
88
/* TODO (AL): keep these?
85
89
/// Divides one DeviceSlice into two at a given index.
86
90
///
0 commit comments