use crate::errors::*;
use crate::objects::types::HelperResult;
use opencl_heads::types::*;
use std::ptr;
#[derive(PartialEq, Debug)]
pub struct WrappedPointer<T>(*const T);
impl<T> WrappedPointer<T> {
pub fn from<U>(x: &U) -> Self {
Self(ptr::NonNull::from(x).as_ptr() as *const T)
}
pub fn from_owned<U>(x: U) -> Self {
Self::from(&x)
}
pub unsafe fn from_raw(x: intptr_t) -> Self {
Self(x as *const T)
}
pub fn null() -> Self {
Self(ptr::null())
}
pub fn unwrap(&self) -> *const T {
self.0
}
}
#[derive(PartialEq, Debug)]
pub struct WrappedMutablePointer<T>(*mut T);
impl<T> WrappedMutablePointer<T> {
pub fn from<U>(x: &mut U) -> Self {
Self(ptr::NonNull::from(x).as_ptr() as *mut T)
}
pub fn from_owned<U>(mut x: U) -> Self {
Self::from(&mut x)
}
pub unsafe fn from_raw(x: intptr_t) -> Self {
Self(x as *mut T)
}
pub fn from_ptr(x: *mut T, fn_name: &'static str) -> HelperResult<Self> {
match ptr::NonNull::new(x) {
Some(dat) => Ok(Self(dat.as_ptr())),
None => Err(RuntimeError::NullPointer(fn_name).to_error()),
}
}
pub fn null() -> Self {
Self(ptr::null_mut())
}
pub fn unwrap(&self) -> *mut T {
self.0
}
}