use crate::ffi::library::{InternalHandle, LibraryHandle, LoaderHandle};
use crate::ownership::{AccessIdentifier, BorrowImmutable, BorrowMutable, Owned};
use std::marker::PhantomData;
mod api;
pub mod library_loader;
pub use crate::ffi::library::LibraryType;
pub use crate::ffi::library::LOADER_TYPE_MAX_LENGTH;
pub use crate::ffi::library::NATIVE_LIBRARY_TYPE_NAME;
pub use api::LibraryAPI;
pub const DEFAULT_HANDLE: Loader<'static, BorrowMutable<'static>> =
unsafe { Loader::new(crate::ffi::library::DEFAULT_HANDLE) };
#[non_exhaustive]
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub enum Error {
InvalidLibraryType(String),
FFIError(crate::ffi::library::Error),
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Library<'a, O> {
_handle: LibraryHandle,
_lifetime: PhantomData<&'a LibraryHandle>,
_ownership: PhantomData<*const O>,
}
impl<'a, O> Library<'a, O>
where
O: AccessIdentifier,
{
#[inline]
pub const unsafe fn new(handle: LibraryHandle) -> Self {
Self {
_handle: handle,
_lifetime: PhantomData,
_ownership: PhantomData,
}
}
#[inline]
pub const fn as_handle(&self) -> LibraryHandle {
self._handle
}
}
impl<'a> Library<'a, Owned> {
#[inline]
pub const fn as_borrowed(&self) -> Library<'a, BorrowImmutable<'_>> {
unsafe { Library::<BorrowImmutable<'_>>::new(self._handle) }
}
#[inline]
pub fn as_borrowed_mut(&mut self) -> Library<'a, BorrowMutable<'_>> {
unsafe { Library::<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 InternalLibrary<O> {
_handle: InternalHandle,
_ownership: PhantomData<*const O>,
}
impl<O> InternalLibrary<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 InternalLibrary<Owned> {
#[inline]
pub const fn as_borrowed(&self) -> InternalLibrary<BorrowImmutable<'_>> {
unsafe { InternalLibrary::<BorrowImmutable<'_>>::new(self._handle) }
}
#[inline]
pub fn as_borrowed_mut(&mut self) -> InternalLibrary<BorrowMutable<'_>> {
unsafe { InternalLibrary::<BorrowMutable<'_>>::new(self._handle) }
}
}
#[derive(Debug, Copy, Clone, Ord, PartialOrd, Eq, PartialEq)]
pub struct Symbol<'a, T> {
_symbol: T,
_phantom: PhantomData<&'a ()>,
}
impl<T> Symbol<'_, T> {
#[inline]
fn new(symbol: T) -> Self {
Self {
_symbol: symbol,
_phantom: PhantomData,
}
}
}
impl<T> AsRef<T> for Symbol<'_, T> {
#[inline]
fn as_ref(&self) -> &T {
&self._symbol
}
}
impl<T> AsRef<T> for Symbol<'_, &T> {
#[inline]
fn as_ref(&self) -> &T {
self._symbol
}
}