Skip to content

Commit

Permalink
Implement pre-pooping for in type_erase (#514)
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed May 15, 2024
1 parent 8a4d673 commit 0f946da
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions minijinja/src/value/type_erase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,22 @@ macro_rules! type_erase {
let vtable = &VTable {
$(
$f: |ptr, $($p),*| unsafe {
std::sync::Arc::<T>::increment_strong_count(ptr as *const T);
let arc = std::sync::Arc::<T>::from_raw(ptr as *const T);
let v = <T as $t_name>::$f(&arc, $($p),*);
std::mem::forget(arc);
v
<T as $t_name>::$f(&arc, $($p),*)
},
)*
$($(
$f_impl: |ptr, $($p_impl),*| unsafe {
std::sync::Arc::<T>::increment_strong_count(ptr as *const T);
let arc = std::sync::Arc::<T>::from_raw(ptr as *const T);
let v = <T as $impl_name>::$f_impl(&*arc, $($p_impl),*);
std::mem::forget(arc);
v
<T as $impl_name>::$f_impl(&*arc, $($p_impl),*)
},
)*)*
__type_id: || std::any::TypeId::of::<T>(),
__type_name: || std::any::type_name::<T>(),
__drop: |ptr| unsafe {
drop(std::sync::Arc::from_raw(ptr as *const T));
std::sync::Arc::from_raw(ptr as *const T);
},
};

Expand Down Expand Up @@ -100,10 +98,8 @@ macro_rules! type_erase {
$v fn downcast<T: 'static>(&self) -> Option<Arc<T>> {
if (vt(self).__type_id)() == std::any::TypeId::of::<T>() {
unsafe {
let arc = std::sync::Arc::<T>::from_raw(self.ptr as *const T);
let v = arc.clone();
std::mem::forget(arc);
return Some(v);
std::sync::Arc::<T>::increment_strong_count(self.ptr as *const T);
return Some(std::sync::Arc::<T>::from_raw(self.ptr as *const T));
}
}

Expand Down

0 comments on commit 0f946da

Please sign in to comment.