Refactor main library, implement some more functionality

This commit is contained in:
Andrzej Janik 2020-09-01 01:43:09 +02:00
commit 2e4cadc2ab
18 changed files with 6024 additions and 856 deletions

View file

@ -1,7 +1,7 @@
use crate::sys;
use std::{
ffi::{c_void, CStr},
fmt::{Debug, Display},
fmt::Debug,
marker::PhantomData,
mem, ptr,
};
@ -12,7 +12,7 @@ macro_rules! check {
{
let err = unsafe { $expr };
if err != crate::sys::ze_result_t::ZE_RESULT_SUCCESS {
return Result::Err(Error(err));
return Result::Err(err);
}
}
};
@ -27,39 +27,24 @@ macro_rules! check_panic {
};
}
pub type Result<T> = std::result::Result<T, Error>;
pub type Result<T> = std::result::Result<T, sys::ze_result_t>;
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
pub struct Error(pub sys::ze_result_t);
impl Error {
fn new<T>(res: sys::ze_result_t, default: T) -> Result<T> {
if res == sys::ze_result_t::ZE_RESULT_SUCCESS {
Ok(default)
} else {
Err(Self(res))
}
}
}
impl Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Debug::fmt(self, f)
}
}
impl std::error::Error for Error {}
pub fn init() -> Result<()> {
Error::new(
unsafe { sys::zeInit(sys::ze_init_flags_t::ZE_INIT_FLAG_GPU_ONLY) },
(),
)
match unsafe { sys::zeInit(sys::ze_init_flags_t::ZE_INIT_FLAG_GPU_ONLY) } {
sys::ze_result_t::ZE_RESULT_SUCCESS => Ok(()),
e => Err(e),
}
}
#[repr(transparent)]
pub struct Driver(sys::ze_driver_handle_t);
unsafe impl Send for Driver {}
unsafe impl Sync for Driver {}
impl Driver {
pub unsafe fn as_ffi(&self) -> sys::ze_driver_handle_t {
self.0
@ -184,6 +169,13 @@ impl Context {
}
}
impl Drop for Context {
#[allow(unused_must_use)]
fn drop(&mut self) {
check_panic! { sys::zeContextDestroy(self.0) };
}
}
#[repr(transparent)]
pub struct CommandQueue(sys::ze_command_queue_handle_t);