use crate::ops::{Begin, BeginMut, End, EndMut, Increment, Indirection};
use crate::vector_ops::{Data, DataMut, Size};
use crate::{cpp_iter, CppIterator, DynamicCast, Ptr, Ref, StaticDowncast, StaticUpcast};
use std::ops::Deref;
use std::{fmt, mem, ptr, slice};
pub trait CppDeletable: Sized {
unsafe fn delete(&self);
}
pub struct CppBox<T: CppDeletable>(ptr::NonNull<T>);
impl<T: CppDeletable> CppBox<T> {
pub unsafe fn new(ptr: Ptr<T>) -> Option<Self> {
Self::from_raw(ptr.as_raw_ptr())
}
pub unsafe fn from_raw(ptr: *const T) -> Option<Self> {
ptr::NonNull::new(ptr as *mut T).map(CppBox)
}
pub unsafe fn as_ptr(&self) -> Ptr<T> {
Ptr::from_raw(self.0.as_ptr())
}
pub fn as_mut_raw_ptr(&self) -> *mut T {
self.0.as_ptr()
}
pub fn as_raw_ptr(&self) -> *const T {
self.0.as_ptr() as *const T
}
pub fn into_raw_ptr(self) -> *mut T {
let ptr = self.0.as_ptr();
mem::forget(self);
ptr
}
pub unsafe fn into_ptr(self) -> Ptr<T> {
let ptr = Ptr::from_raw(self.0.as_ptr());
mem::forget(self);
ptr
}
#[allow(clippy::should_implement_trait)]
pub unsafe fn as_ref(&self) -> Ref<T> {
Ref::from_raw_non_null(self.0)
}
pub unsafe fn as_raw_ref<'a>(&self) -> &'a T {
&*self.0.as_ptr()
}
pub unsafe fn as_mut_raw_ref<'a>(&self) -> &'a mut T {
&mut *self.0.as_ptr()
}
pub unsafe fn static_upcast<U>(&self) -> Ref<U>
where
T: StaticUpcast<U>,
{
StaticUpcast::static_upcast(self.as_ptr())
.as_ref()
.expect("StaticUpcast returned null on CppBox input")
}
pub unsafe fn static_downcast<U>(&self) -> Ref<U>
where
T: StaticDowncast<U>,
{
StaticDowncast::static_downcast(self.as_ptr())
.as_ref()
.expect("StaticDowncast returned null on CppBox input")
}
pub unsafe fn dynamic_cast<U>(&self) -> Option<Ref<U>>
where
T: DynamicCast<U>,
{
DynamicCast::dynamic_cast(self.as_ptr()).as_ref()
}
}
impl<V, T> CppBox<V>
where
V: Data<Output = *const T> + Size + CppDeletable,
{
pub unsafe fn as_slice<'a>(&self) -> &'a [T] {
let ptr = self.data();
let size = self.size();
slice::from_raw_parts(ptr, size)
}
}
impl<V, T> CppBox<V>
where
V: DataMut<Output = *mut T> + Size + CppDeletable,
{
pub unsafe fn as_mut_slice<'a>(&self) -> &'a mut [T] {
let ptr = self.data_mut();
let size = self.size();
slice::from_raw_parts_mut(ptr, size)
}
}
impl<T, T1, T2> CppBox<T>
where
T: Begin<Output = CppBox<T1>> + End<Output = CppBox<T2>> + CppDeletable,
T1: CppDeletable + PartialEq<Ref<T2>> + Increment + Indirection,
T2: CppDeletable,
{
pub unsafe fn iter(&self) -> CppIterator<T1, T2> {
cpp_iter(self.begin(), self.end())
}
}
impl<T, T1, T2> CppBox<T>
where
T: BeginMut<Output = CppBox<T1>> + EndMut<Output = CppBox<T2>> + CppDeletable,
T1: CppDeletable + PartialEq<Ref<T2>> + Increment + Indirection,
T2: CppDeletable,
{
pub unsafe fn iter_mut(&self) -> CppIterator<T1, T2> {
cpp_iter(self.begin_mut(), self.end_mut())
}
}
impl<T: CppDeletable> Deref for CppBox<T> {
type Target = T;
fn deref(&self) -> &T {
unsafe { self.0.as_ref() }
}
}
impl<T: CppDeletable> Drop for CppBox<T> {
fn drop(&mut self) {
unsafe {
T::delete(self.0.as_ref());
}
}
}
impl<T: CppDeletable> fmt::Debug for CppBox<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "CppBox({:?})", self.0)
}
}
#[cfg(test)]
mod tests {
use crate::{CppBox, CppDeletable, Ptr};
use std::cell::RefCell;
use std::rc::Rc;
struct Struct1 {
value: Rc<RefCell<i32>>,
}
unsafe extern "C" fn struct1_delete(this_ptr: *const Struct1) {
(*this_ptr).value.borrow_mut().clone_from(&42);
}
impl CppDeletable for Struct1 {
unsafe fn delete(&self) {
struct1_delete(self);
}
}
#[test]
fn test_drop_calls_deleter() {
let value1 = Rc::new(RefCell::new(10));
let object1 = Struct1 {
value: value1.clone(),
};
assert!(*value1.borrow() == 10);
unsafe {
CppBox::new(Ptr::from_raw(&object1));
}
assert!(*value1.borrow() == 42);
}
}