use std::cell::{Ref, RefCell, RefMut};
use std::ffi::c_void;
use std::fmt::{Debug, Formatter};
use std::ops::{Deref, DerefMut};
use std::rc::{Rc, Weak};
pub(crate) trait DropInnerExclusively {
fn drop_exclusively(self);
}
pub(crate) struct CoapFfiRcCell<D>(Rc<RefCell<D>>);
impl<D> CoapFfiRcCell<D> {
pub fn new(value: D) -> CoapFfiRcCell<D> {
CoapFfiRcCell(Rc::new(RefCell::new(value)))
}
pub unsafe fn clone_raw_rc(ptr: *mut c_void) -> CoapFfiRcCell<D> {
let orig_ref = Rc::from_raw(ptr as *const RefCell<D>);
let new_ref = Rc::clone(&orig_ref);
Rc::into_raw(orig_ref);
CoapFfiRcCell(new_ref)
}
pub unsafe fn clone_raw_weak(ptr: *mut c_void) -> CoapFfiRcCell<D> {
let orig_ref = Weak::from_raw(ptr as *const RefCell<D>);
let new_ref = Weak::upgrade(&orig_ref).expect("attempted to upgrade a weak reference that was orphaned");
let _weakref = Weak::into_raw(orig_ref);
CoapFfiRcCell(new_ref)
}
pub unsafe fn raw_ptr_to_weak(ptr: *mut c_void) -> Weak<RefCell<D>> {
Weak::from_raw(ptr as *const RefCell<D>)
}
#[allow(unused)]
pub unsafe fn raw_ptr_to_rc(ptr: *mut c_void) -> Rc<RefCell<D>> {
Rc::from_raw(ptr as *const RefCell<D>)
}
#[allow(unused)]
pub fn create_raw_rc(&self) -> *mut c_void {
Rc::into_raw(Rc::clone(&self.0)) as *mut c_void
}
pub fn create_raw_weak(&self) -> *mut c_void {
Weak::into_raw(Rc::downgrade(&self.0)) as *mut c_void
}
pub fn borrow(&self) -> Ref<D> {
RefCell::borrow(&self.0)
}
pub fn borrow_mut(&self) -> RefMut<D> {
RefCell::borrow_mut(&self.0)
}
}
impl<D: PartialEq> PartialEq for CoapFfiRcCell<D> {
fn eq(&self, other: &Self) -> bool {
Rc::eq(&self.0, &other.0)
}
}
impl<D: Eq + PartialEq> Eq for CoapFfiRcCell<D> {}
impl<D> Clone for CoapFfiRcCell<D> {
fn clone(&self) -> Self {
CoapFfiRcCell(self.0.clone())
}
}
impl<D: Debug> Debug for CoapFfiRcCell<D> {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CoapFfiRcCell").field("0", &self.0).finish()
}
}
impl<T: Debug> DropInnerExclusively for CoapFfiRcCell<T> {
fn drop_exclusively(self) {
std::mem::drop(
Rc::try_unwrap(self.0).expect("unable to unwrap instance of CoapFfiRcCell as it is still in use"),
)
}
}
#[derive(Debug)]
pub(crate) struct CoapLendableFfiRcCell<T: Debug>(Rc<RefCell<T>>, Rc<RefCell<Option<*mut T>>>);
impl<T: Debug> CoapLendableFfiRcCell<T> {
pub fn new(value: T) -> CoapLendableFfiRcCell<T> {
CoapLendableFfiRcCell(Rc::new(RefCell::new(value)), Rc::new(RefCell::new(None)))
}
pub fn borrow_mut(&self) -> CoapLendableFfiRefMut<'_, T> {
if let Some(borrowed) = RefCell::borrow_mut(&self.1).take() {
CoapLendableFfiRefMut::Borrowed(unsafe { borrowed.as_mut() }.unwrap(), Rc::clone(&self.1))
} else {
CoapLendableFfiRefMut::Owned(RefCell::borrow_mut(&self.0))
}
}
pub fn borrow(&self) -> CoapLendableFfiRef<'_, T> {
if let Some(borrowed) = RefCell::borrow_mut(&self.1).take() {
CoapLendableFfiRef::Borrowed(unsafe { borrowed.as_mut() }.unwrap(), Rc::clone(&self.1))
} else {
CoapLendableFfiRef::Owned(RefCell::borrow(&self.0))
}
}
#[allow(unused)]
pub fn create_raw_rc_box(&self) -> *mut CoapLendableFfiRcCell<T> {
Box::into_raw(Box::new(CoapLendableFfiRcCell::<T>::clone(self)))
}
pub fn create_raw_weak_box(&self) -> *mut CoapLendableFfiWeakCell<T> {
Box::into_raw(Box::new(self.downgrade()))
}
#[allow(unused)]
pub unsafe fn clone_raw_rc_box(ptr: *mut CoapLendableFfiRcCell<T>) -> CoapLendableFfiRcCell<T> {
let ref_box: Box<CoapLendableFfiRcCell<T>> = Box::from_raw(ptr);
let ret_val = CoapLendableFfiRcCell::<T>::clone(ref_box.as_ref());
Box::into_raw(ref_box);
ret_val
}
pub unsafe fn clone_raw_weak_box(ptr: *mut CoapLendableFfiWeakCell<T>) -> CoapLendableFfiRcCell<T> {
let ref_box: Box<CoapLendableFfiWeakCell<T>> = Box::from_raw(ptr);
let ret_val = ref_box
.upgrade()
.expect("unable to restore CoapLendableFfiRcCell as the underlying value was already dropped");
Box::into_raw(ref_box);
ret_val
}
#[allow(unused)]
pub unsafe fn from_raw_rc_box(ptr: *mut CoapLendableFfiRcCell<T>) -> Box<CoapLendableFfiRcCell<T>> {
Box::from_raw(ptr)
}
pub fn lend_ref_mut<'a>(&self, refer: &'a mut T) -> CoapLendableFfiRefLender<'a, T> {
assert_eq!(
RefCell::as_ptr(&self.0),
refer as *mut T,
"attempted to lend different object over CoapLendableFfiRcCell"
);
CoapLendableFfiRefLender::new(refer, &self.1)
}
pub fn downgrade(&self) -> CoapLendableFfiWeakCell<T> {
CoapLendableFfiWeakCell(Rc::downgrade(&self.0), Rc::downgrade(&self.1))
}
}
impl<T: Debug> Clone for CoapLendableFfiRcCell<T> {
fn clone(&self) -> Self {
CoapLendableFfiRcCell(Rc::clone(&self.0), Rc::clone(&self.1))
}
}
#[derive(Debug)]
pub(crate) struct CoapLendableFfiWeakCell<T: Debug>(Weak<RefCell<T>>, Weak<RefCell<Option<*mut T>>>);
impl<T: Debug> Clone for CoapLendableFfiWeakCell<T> {
fn clone(&self) -> Self {
CoapLendableFfiWeakCell(Weak::clone(&self.0), Weak::clone(&self.1))
}
}
impl<T: Debug> CoapLendableFfiWeakCell<T> {
pub fn upgrade(&self) -> Option<CoapLendableFfiRcCell<T>> {
self.0
.upgrade()
.zip(self.1.upgrade())
.map(|(v0, v1)| CoapLendableFfiRcCell(v0, v1))
}
pub unsafe fn from_raw_box(ptr: *mut CoapLendableFfiWeakCell<T>) -> Box<CoapLendableFfiWeakCell<T>> {
Box::from_raw(ptr)
}
}
pub(crate) struct CoapLendableFfiRefLender<'a, T> {
lent_ref: &'a mut T,
lent_ptr_ctr: Weak<RefCell<Option<*mut T>>>,
}
impl<'a, T> CoapLendableFfiRefLender<'a, T> {
fn new(refer: &'a mut T, container: &Rc<RefCell<Option<*mut T>>>) -> CoapLendableFfiRefLender<'a, T> {
RefCell::borrow_mut(container).replace(refer as *mut T);
CoapLendableFfiRefLender {
lent_ref: refer,
lent_ptr_ctr: Rc::downgrade(container),
}
}
pub(crate) fn unlend(self) {
std::mem::drop(self);
}
}
impl<T> Drop for CoapLendableFfiRefLender<'_, T> {
fn drop(&mut self) {
if let Some(lent_ptr_ctr) = self.lent_ptr_ctr.upgrade() {
assert_eq!(
self.lent_ref as *mut T,
lent_ptr_ctr
.take()
.expect("unable to retrieve lent reference, implying that it may be in use somewhere"),
"somehow, multiple references are stored in the same CoapLendableFfiRcCell"
)
}
}
}
pub(crate) enum CoapLendableFfiRef<'a, T> {
Owned(Ref<'a, T>),
Borrowed(&'a mut T, Rc<RefCell<Option<*mut T>>>),
}
impl<T> Deref for CoapLendableFfiRef<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
match self {
CoapLendableFfiRef::Owned(v) => v.deref(),
CoapLendableFfiRef::Borrowed(v, _lend_container) => v,
}
}
}
impl<T> Drop for CoapLendableFfiRef<'_, T> {
fn drop(&mut self) {
if let CoapLendableFfiRef::Borrowed(refer, lend_container) = self {
let mut lend_container = RefCell::borrow_mut(lend_container);
assert!(
lend_container.is_none(),
"somehow, multiple references are stored in the same CoapLendableFfiRcCell"
);
assert!(
lend_container.replace(*refer).is_none(),
"somehow, multiple references are stored in the same CoapLendableFfiRcCell"
);
}
}
}
pub(crate) enum CoapLendableFfiRefMut<'a, T> {
Owned(RefMut<'a, T>),
Borrowed(&'a mut T, Rc<RefCell<Option<*mut T>>>),
}
impl<T> Deref for CoapLendableFfiRefMut<'_, T> {
type Target = T;
fn deref(&self) -> &Self::Target {
match self {
CoapLendableFfiRefMut::Owned(v) => v.deref(),
CoapLendableFfiRefMut::Borrowed(v, _lend_container) => v,
}
}
}
impl<T> DerefMut for CoapLendableFfiRefMut<'_, T> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
CoapLendableFfiRefMut::Owned(v) => v.deref_mut(),
CoapLendableFfiRefMut::Borrowed(v, _lend_container) => v,
}
}
}
impl<T> Drop for CoapLendableFfiRefMut<'_, T> {
fn drop(&mut self) {
if let CoapLendableFfiRefMut::Borrowed(refer, lend_container) = self {
let mut lend_container = RefCell::borrow_mut(lend_container);
assert!(
lend_container.is_none(),
"somehow, multiple references are stored in the same CoapLendableFfiRcCell"
);
assert!(
lend_container.replace(*refer).is_none(),
"somehow, multiple references are stored in the same CoapLendableFfiRcCell"
);
}
}
}