use std::{ops::{Deref, DerefMut}};
use libloading::Library;
use crate::FProxyFrom;
pub enum FAllocated<'a, T: ?Sized> {
Box(Box<T>),
Ref(&'a T),
RefMut(&'a mut T),
}
impl<'a, T: ?Sized> Deref for FAllocated<'a, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
match self {
FAllocated::Box(b) => &**b,
FAllocated::Ref(r) => *r,
FAllocated::RefMut(r) => *r,
}
}
}
impl<'a, T: ?Sized> DerefMut for FAllocated<'a, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
FAllocated::Box(b) => &mut **b,
FAllocated::Ref(_) => panic!(),
FAllocated::RefMut(r) => *r,
}
}
}
impl<T> Clone for FAllocated<'_, T> {
fn clone(&self) -> Self {
match self {
FAllocated::Ref(r) => FAllocated::Ref(*r),
_ => panic!(),
}
}
}
pub trait FProxy {
unsafe fn free(&mut self) {
}
}
#[derive(Clone)]
pub struct FOwned<F: FProxy> {
pub proxy: F,
}
impl<F> Deref for FOwned<F>
where
F: FProxy
{
type Target = F;
fn deref(&self) -> &Self::Target {
&self.proxy
}
}
impl<F> DerefMut for FOwned<F>
where
F: FProxy
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.proxy
}
}
impl<F> From<F> for FOwned<F>
where
F: FProxy
{
fn from(proxy: F) -> Self {
FOwned { proxy }
}
}
impl<'l, F, U> FProxyFrom<'l, U> for FOwned<F>
where
F: FProxyFrom<'l, U> + FProxy
{
fn proxy_from(value: U, lib: &'l Library) -> Self {
F::proxy_from(value, lib)
.into()
}
}
#[derive(Clone, Copy)]
pub struct FRef<F: FProxy> {
proxy: F,
}
impl<F> Deref for FRef<F>
where
F: FProxy
{
type Target = F;
fn deref(&self) -> &Self::Target {
&self.proxy
}
}
impl<F> From<F> for FRef<F>
where
F: FProxy
{
fn from(proxy: F) -> Self {
FRef { proxy }
}
}
impl<'l, F, U> FProxyFrom<'l, U> for FRef<F>
where
F: FProxyFrom<'l, U> + FProxy
{
fn proxy_from(value: U, lib: &'l Library) -> Self {
F::proxy_from(value, lib)
.into()
}
}
pub struct FRefMut<F: FProxy> {
proxy: F,
}
impl<F> Deref for FRefMut<F>
where
F: FProxy
{
type Target = F;
fn deref(&self) -> &Self::Target {
&self.proxy
}
}
impl<F> DerefMut for FRefMut<F>
where
F: FProxy
{
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.proxy
}
}
impl<F> From<F> for FRefMut<F>
where
F: FProxy
{
fn from(proxy: F) -> Self {
FRefMut { proxy }
}
}
impl<'l, F, U> FProxyFrom<'l, U> for FRefMut<F>
where
F: FProxyFrom<'l, U> + FProxy
{
fn proxy_from(value: U, lib: &'l Library) -> Self {
F::proxy_from(value, lib)
.into()
}
}