From 53f0672397695adf8cf37f61c2be0835a405e16c Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Fri, 29 Apr 2022 20:37:43 +0100 Subject: [PATCH 1/2] Remove unnecessary `Default` constraint on `DeviceBox::as_host_value`. --- crates/cust/src/memory/device/device_box.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/crates/cust/src/memory/device/device_box.rs b/crates/cust/src/memory/device/device_box.rs index 3bead1ba..5635a9d8 100644 --- a/crates/cust/src/memory/device/device_box.rs +++ b/crates/cust/src/memory/device/device_box.rs @@ -7,7 +7,7 @@ use crate::memory::{cuda_free_async, cuda_malloc_async, DeviceCopy}; use crate::stream::Stream; use crate::sys as cuda; use std::fmt::{self, Pointer}; -use std::mem::{self, ManuallyDrop}; +use std::mem::{self, ManuallyDrop, MaybeUninit}; use std::os::raw::c_void; @@ -131,12 +131,15 @@ impl DeviceBox { } } -impl DeviceBox { +impl DeviceBox { /// Read the data back from the GPU into host memory. pub fn as_host_value(&self) -> CudaResult { - let mut val = T::default(); - self.copy_to(&mut val)?; - Ok(val) + let mut val = MaybeUninit::uninit(); + // SAFETY: We do not read from the uninitialized reference. + unsafe { + self.copy_to(val.assume_init_mut())?; + Ok(val.assume_init()) + } } } From 3cac529f1cc6960145859e981a91196d7d52d522 Mon Sep 17 00:00:00 2001 From: Chris Jones Date: Mon, 2 May 2022 08:41:49 +0100 Subject: [PATCH 2/2] Merge `impl` sections. --- crates/cust/src/memory/device/device_box.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/cust/src/memory/device/device_box.rs b/crates/cust/src/memory/device/device_box.rs index 5635a9d8..a08b441e 100644 --- a/crates/cust/src/memory/device/device_box.rs +++ b/crates/cust/src/memory/device/device_box.rs @@ -129,9 +129,7 @@ impl DeviceBox { // you keep around a pointer, but in that case, we cannot guarantee safety. unsafe { cuda_free_async(stream, me.ptr) } } -} -impl DeviceBox { /// Read the data back from the GPU into host memory. pub fn as_host_value(&self) -> CudaResult { let mut val = MaybeUninit::uninit();