use std::mem::ManuallyDrop;
use std::ops::Deref;
use std::sync::Arc;
#[doc(hidden)]
#[derive(Debug)]
pub struct SharedAllocation<T>(ManuallyDrop<Arc<T>>);
impl<T> SharedAllocation<T> {
pub fn new(value: T) -> Self {
Self(ManuallyDrop::new(Arc::new(value)))
}
pub fn strong_count(&self) -> usize {
Arc::strong_count(&self.0)
}
}
impl<T> Clone for SharedAllocation<T> {
fn clone(&self) -> Self {
Self(ManuallyDrop::new(Arc::clone(&self.0)))
}
}
impl<T> Deref for SharedAllocation<T> {
type Target = T;
fn deref(&self) -> &T {
&self.0
}
}
impl<T> Drop for SharedAllocation<T> {
fn drop(&mut self) {
let arc = unsafe { ManuallyDrop::take(&mut self.0) };
drop(Arc::into_inner(arc));
}
}