use std::ptr;
use singe_cuda_sys::driver;
use crate::view::{AnyBitPatternDeviceRepr, DeviceRepr, NoUninitDeviceRepr, ZeroableDeviceRepr};
#[derive(Debug, Clone, Copy)]
#[repr(transparent)]
pub struct HostFunction(driver::CUhostFn);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(transparent)]
pub struct DeviceFunction(driver::CUfunction);
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
#[repr(transparent)]
pub struct DevicePtr(*mut ());
unsafe impl DeviceRepr for DevicePtr {}
unsafe impl NoUninitDeviceRepr for DevicePtr {}
unsafe impl ZeroableDeviceRepr for DevicePtr {}
unsafe impl AnyBitPatternDeviceRepr for DevicePtr {}
impl HostFunction {
#[must_use]
pub const unsafe fn new(raw: driver::CUhostFn) -> Self {
Self(raw)
}
#[must_use]
pub const unsafe fn from_raw(raw: driver::CUhostFn) -> Self {
unsafe { Self::new(raw) }
}
#[must_use]
pub const fn as_raw(self) -> driver::CUhostFn {
self.0
}
}
impl DeviceFunction {
pub const unsafe fn new(raw: driver::CUfunction) -> Self {
Self(raw)
}
pub const unsafe fn from_raw(raw: driver::CUfunction) -> Self {
unsafe { Self::new(raw) }
}
#[must_use]
pub const fn as_raw(self) -> driver::CUfunction {
self.0
}
#[must_use]
pub const fn is_null(self) -> bool {
self.0.is_null()
}
}
impl DevicePtr {
#[must_use]
pub const fn null() -> Self {
Self(ptr::null_mut())
}
pub const unsafe fn new(raw: *mut ()) -> Self {
Self(raw)
}
pub const unsafe fn from_raw(raw: *mut ()) -> Self {
unsafe { Self::new(raw.cast()) }
}
#[must_use]
pub const fn as_raw(self) -> *mut () {
self.0.cast()
}
#[must_use]
pub const fn as_ptr(self) -> *mut () {
self.0
}
#[must_use]
pub const fn as_const_ptr(self) -> *const () {
self.0.cast_const()
}
#[must_use]
pub const fn is_null(self) -> bool {
self.0.is_null()
}
#[must_use]
pub fn addr(self) -> usize {
self.0.addr()
}
#[must_use]
pub fn is_aligned_to(self, alignment: usize) -> bool {
self.addr().is_multiple_of(alignment)
}
#[must_use]
pub fn checked_byte_add(self, bytes: usize) -> Option<Self> {
self.addr()
.checked_add(bytes)
.map(|_| Self(self.0.wrapping_byte_add(bytes)))
}
#[must_use]
pub const fn cast<T>(self) -> *mut T {
self.0.cast()
}
}
impl From<HostFunction> for driver::CUhostFn {
fn from(value: HostFunction) -> Self {
value.as_raw()
}
}
impl From<DeviceFunction> for driver::CUfunction {
fn from(value: DeviceFunction) -> Self {
value.as_raw()
}
}
impl From<DevicePtr> for *mut () {
fn from(value: DevicePtr) -> Self {
value.as_raw()
}
}