use std::path;
use std::pin::Pin;
use std::sync::Arc;
use std::{fmt, ptr::NonNull};
mod guts;
pub use guts::{IsPtr, Own, Ref};
pub use crossbeam_epoch::Guard;
pub use crossbeam_epoch::pin;
#[cfg(all(test, loom))]
mod loom_tests;
#[cfg(test)]
mod recycler_tests;
#[cfg(test)]
mod ui_tests;
impl<T: Send + 'static> Own<Box<T>> {
pub fn new_box(value: T) -> Self {
Self::new(Box::new(value))
}
}
impl<P: IsPtr + Send> fmt::Debug for Own<P>
where
P::T: fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Own({:?})", &**self)
}
}
impl<T: fmt::Debug + ?Sized> fmt::Debug for Ref<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.get(&pin()) {
Some(live) => {
write!(f, "Ref::Live({live:?})")
}
None => f.debug_tuple("Ref::Dead").finish_non_exhaustive(),
}
}
}
impl<P: IsPtr + core::ops::Deref> IsPtr for Pin<P> {
type T = P::T;
fn into_raw_ptr(this: Self) -> NonNull<P::T> {
let b = unsafe { Pin::into_inner_unchecked(this) };
IsPtr::into_raw_ptr(b)
}
unsafe fn from_raw_ptr(ptr: NonNull<P::T>) -> Self {
let b: P = unsafe { IsPtr::from_raw_ptr(ptr) };
unsafe { Pin::new_unchecked(b) }
}
}
impl<T: ?Sized> IsPtr for Box<T> {
type T = T;
fn into_raw_ptr(this: Self) -> NonNull<T> {
NonNull::new(Box::into_raw(this)).unwrap()
}
unsafe fn from_raw_ptr(ptr: NonNull<T>) -> Self {
unsafe { Box::from_raw(ptr.as_ptr()) }
}
}
impl<T: ?Sized> IsPtr for Arc<T> {
type T = T;
fn into_raw_ptr(this: Self) -> NonNull<T> {
NonNull::new(Arc::into_raw(this).cast_mut()).unwrap()
}
unsafe fn from_raw_ptr(ptr: NonNull<T>) -> Self {
unsafe { Arc::from_raw(ptr.as_ptr()) }
}
}
impl IsPtr for String {
type T = str;
fn into_raw_ptr(this: Self) -> NonNull<str> {
let b: Box<str> = this.into();
IsPtr::into_raw_ptr(b)
}
unsafe fn from_raw_ptr(ptr: NonNull<str>) -> Self {
let b: Box<str> = unsafe { IsPtr::from_raw_ptr(ptr) };
b.into()
}
}
impl IsPtr for path::PathBuf {
type T = path::Path;
fn into_raw_ptr(this: Self) -> NonNull<path::Path> {
let b: Box<path::Path> = this.into();
IsPtr::into_raw_ptr(b)
}
unsafe fn from_raw_ptr(ptr: NonNull<path::Path>) -> Self {
let b: Box<path::Path> = unsafe { IsPtr::from_raw_ptr(ptr) };
b.into()
}
}
impl<T> IsPtr for Vec<T> {
type T = [T];
fn into_raw_ptr(this: Self) -> NonNull<[T]> {
let b: Box<[T]> = this.into();
IsPtr::into_raw_ptr(b)
}
unsafe fn from_raw_ptr(ptr: NonNull<[T]>) -> Self {
let b: Box<[T]> = unsafe { IsPtr::from_raw_ptr(ptr) };
b.into()
}
}
impl IsPtr for () {
type T = ();
fn into_raw_ptr(_: Self) -> NonNull<Self::T> {
NonNull::dangling()
}
unsafe fn from_raw_ptr(_: NonNull<Self::T>) -> Self {}
}
#[macro_export]
macro_rules! refer {
($owner:expr) => {{
let r: $crate::Ref<_> = $owner._weak;
r
}};
}