use crate::ffi::module::{InternalHandle, LoaderHandle, ModuleHandle};
use crate::ownership::{AccessIdentifier, BorrowImmutable, BorrowMutable, Owned};
use std::marker::PhantomData;
use std::ops::{Deref, DerefMut};
mod api;
pub mod module_loader;
pub mod native_module;
pub use crate::ffi::module::InterfaceDescriptor;
pub use crate::ffi::module::InterfaceExtension;
pub use crate::ffi::module::InterfaceName;
pub use crate::ffi::module::ModuleInfo;
pub use crate::ffi::module::ModuleName;
pub use crate::ffi::module::ModuleStatus;
pub use crate::ffi::module::ModuleType;
pub use crate::ffi::module::ModuleVersion;
pub use crate::ffi::module::INTERFACE_EXTENSION_NAME_MAX_LENGTH;
pub use crate::ffi::module::INTERFACE_INFO_NAME_MAX_LENGTH;
pub use crate::ffi::module::MODULE_INFO_NAME_MAX_LENGTH;
pub use crate::ffi::module::MODULE_INFO_VERSION_MAX_LENGTH;
pub use crate::ffi::module::MODULE_LOADER_TYPE_MAX_LENGTH;
pub use crate::ffi::module::NATIVE_MODULE_INTERFACE_SYMBOL_NAME;
pub use crate::ffi::module::NATIVE_MODULE_TYPE_NAME;
pub use api::ModuleAPI;
pub const DEFAULT_HANDLE: Loader<'static, BorrowMutable<'static>> =
unsafe { Loader::new(crate::ffi::module::MODULE_LOADER_DEFAULT_HANDLE) };
#[non_exhaustive]
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum Error {
ParameterError(String),
FFIError(crate::ffi::module::Error),
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Module<'a, O> {
_handle: ModuleHandle,
_lifetime: PhantomData<&'a ModuleHandle>,
_ownership: PhantomData<*const O>,
}
impl<'a, O> Module<'a, O>
where
O: AccessIdentifier,
{
#[inline]
pub const unsafe fn new(handle: ModuleHandle) -> Self {
Self {
_handle: handle,
_lifetime: PhantomData,
_ownership: PhantomData,
}
}
#[inline]
pub const fn as_handle(&self) -> ModuleHandle {
self._handle
}
}
impl<'a> Module<'a, Owned> {
#[inline]
pub const fn as_borrowed(&self) -> Module<'a, BorrowImmutable<'_>> {
unsafe { Module::<BorrowImmutable<'_>>::new(self._handle) }
}
#[inline]
pub fn as_borrowed_mut(&mut self) -> Module<'a, BorrowMutable<'_>> {
unsafe { Module::<BorrowMutable<'_>>::new(self._handle) }
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Loader<'a, O> {
_handle: LoaderHandle,
_lifetime: PhantomData<&'a LoaderHandle>,
_ownership: PhantomData<*const O>,
}
impl<'a, O> Loader<'a, O>
where
O: AccessIdentifier,
{
#[inline]
pub const unsafe fn new(handle: LoaderHandle) -> Self {
Self {
_handle: handle,
_lifetime: PhantomData,
_ownership: PhantomData,
}
}
#[inline]
pub const fn as_handle(&self) -> LoaderHandle {
self._handle
}
}
impl<'a> Loader<'a, Owned> {
#[inline]
pub const fn as_borrowed(&self) -> Loader<'a, BorrowImmutable<'_>> {
unsafe { Loader::<BorrowImmutable<'_>>::new(self._handle) }
}
#[inline]
pub fn as_borrowed_mut(&mut self) -> Loader<'a, BorrowMutable<'_>> {
unsafe { Loader::<BorrowMutable<'_>>::new(self._handle) }
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct InternalModule<O> {
_handle: InternalHandle,
_ownership: PhantomData<*const O>,
}
impl<O> InternalModule<O>
where
O: AccessIdentifier,
{
#[inline]
pub const unsafe fn new(handle: InternalHandle) -> Self {
Self {
_handle: handle,
_ownership: PhantomData,
}
}
#[inline]
pub const fn as_handle(&self) -> InternalHandle {
self._handle
}
}
impl InternalModule<Owned> {
#[inline]
pub const fn as_borrowed(&self) -> InternalModule<BorrowImmutable<'_>> {
unsafe { InternalModule::<BorrowImmutable<'_>>::new(self._handle) }
}
#[inline]
pub fn as_borrowed_mut(&mut self) -> InternalModule<BorrowMutable<'_>> {
unsafe { InternalModule::<BorrowMutable<'_>>::new(self._handle) }
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Interface<'a, T> {
_interface: T,
_phantom: PhantomData<&'a ()>,
}
impl<T> Interface<'_, T> {
#[inline]
fn new(interface: T) -> Self {
Self {
_interface: interface,
_phantom: PhantomData,
}
}
}
impl<T> Deref for Interface<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
&self._interface
}
}
impl<T> DerefMut for Interface<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self._interface
}
}