Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unnecessary Default constraint on DeviceBox::as_host_value. #67

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions crates/cust/src/memory/device/device_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -129,14 +129,15 @@ impl<T: DeviceCopy> DeviceBox<T> {
// you keep around a pointer, but in that case, we cannot guarantee safety.
unsafe { cuda_free_async(stream, me.ptr) }
}
}

impl<T: DeviceCopy + Default> DeviceBox<T> {
/// Read the data back from the GPU into host memory.
pub fn as_host_value(&self) -> CudaResult<T> {
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())
}
}
}

Expand Down