#![no_std]
extern crate alloc;
#[cfg(feature = "std")]
extern crate std;
#[cfg(feature = "erasable")]
use erasable::{Erasable, ErasablePtr, ErasedPtr};
#[cfg(feature = "std")]
use std::{
io,
net::ToSocketAddrs,
panic::{RefUnwindSafe, UnwindSafe},
};
use {
alloc::{rc::Rc, sync::Arc},
core::{
borrow::Borrow,
cmp::Ordering,
fmt::{
self, Binary, Debug, Display, Formatter, LowerExp, LowerHex, Octal, Pointer, UpperExp,
UpperHex,
},
hash::{Hash, Hasher},
marker::PhantomData,
mem::ManuallyDrop,
ops::Deref,
ptr,
},
};
macro_rules! rc_borrow {
($($(#[$m:meta])* $RcBorrow:ident = $Rc:ident)*) => {$(
$(#[$m])*
pub struct $RcBorrow<'a, T: ?Sized> {
raw: ptr::NonNull<T>,
marker: PhantomData<&'a $Rc<T>>
}
unsafe impl<'a, T: ?Sized> Send for $RcBorrow<'a, T> where &'a $Rc<T>: Send {}
unsafe impl<'a, T: ?Sized> Sync for $RcBorrow<'a, T> where &'a $Rc<T>: Sync {}
impl<'a, T: ?Sized> From<&'a $Rc<T>> for $RcBorrow<'a, T> {
fn from(v: &'a $Rc<T>) -> $RcBorrow<'a, T> {
$RcBorrow {
raw: (&**v).into(),
marker: PhantomData,
}
}
}
impl<'a, T: ?Sized> $RcBorrow<'a, T> {
pub fn upgrade(this: Self) -> $Rc<T> {
unsafe { $Rc::clone(&ManuallyDrop::new($Rc::from_raw(this.raw.as_ptr()))) }
}
pub fn downgrade(this: Self) -> &'a T {
unsafe { &*this.raw.as_ptr() }
}
}
#[cfg(feature = "erasable")]
unsafe impl<T: ?Sized> ErasablePtr for $RcBorrow<'_, T>
where
T: Erasable
{
fn erase(this: Self) -> ErasedPtr {
T::erase(this.raw)
}
unsafe fn unerase(this: ErasedPtr) -> Self {
$RcBorrow {
raw: T::unerase(this),
marker: PhantomData,
}
}
}
impl<T: ?Sized, U: ?Sized> AsRef<U> for $RcBorrow<'_, T>
where
T: AsRef<U>,
{
fn as_ref(&self) -> &U {
(**self).as_ref()
}
}
impl<T: ?Sized> Binary for $RcBorrow<'_, T>
where
T: Binary,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T: ?Sized> Borrow<T> for $RcBorrow<'_, T> {
fn borrow(&self) -> &T {
&**self
}
}
impl<T: ?Sized> Clone for $RcBorrow<'_, T> {
fn clone(&self) -> Self { *self }
}
impl<T: ?Sized> Copy for $RcBorrow<'_, T> {}
impl<T: ?Sized> Debug for $RcBorrow<'_, T>
where
T: Debug
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T: ?Sized> Deref for $RcBorrow<'_, T> {
type Target = T;
fn deref(&self) -> &T {
Self::downgrade(*self)
}
}
impl<T: ?Sized> Display for $RcBorrow<'_, T>
where
T: Display,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T: ?Sized> Eq for $RcBorrow<'_, T> where T: Eq {}
impl<T: ?Sized> Hash for $RcBorrow<'_, T>
where
T: Hash,
{
fn hash<H: Hasher>(&self, state: &mut H) {
(**self).hash(state)
}
}
impl<T: ?Sized> LowerExp for $RcBorrow<'_, T>
where
T: LowerExp,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T: ?Sized> LowerHex for $RcBorrow<'_, T>
where
T: LowerHex,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T: ?Sized> Octal for $RcBorrow<'_, T>
where
T: Octal,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T: Ord> Ord for $RcBorrow<'_, T>
where
T: Ord,
{
fn cmp(&self, other: &Self) -> Ordering {
(**self).cmp(&**other)
}
}
impl<T: ?Sized, O: ?Sized> PartialEq<&'_ O> for $RcBorrow<'_, T>
where
T: PartialEq<O>,
{
fn eq(&self, other: &&O) -> bool {
(**self).eq(other)
}
}
impl<T: ?Sized, O: ?Sized> PartialEq<&'_ mut O> for $RcBorrow<'_, T>
where
T: PartialEq<O>,
{
fn eq(&self, other: &&mut O) -> bool {
(**self).eq(other)
}
}
impl<T: ?Sized> PartialEq for $RcBorrow<'_, T>
where
T: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
(**self).eq(other)
}
}
impl<T: ?Sized, O: ?Sized> PartialOrd<&'_ O> for $RcBorrow<'_, T>
where
T: PartialOrd<O>,
{
fn partial_cmp(&self, other: &&O) -> Option<Ordering> {
(**self).partial_cmp(other)
}
}
impl<T: ?Sized, O: ?Sized> PartialOrd<&'_ mut O> for $RcBorrow<'_, T>
where
T: PartialOrd<O>,
{
fn partial_cmp(&self, other: &&mut O) -> Option<Ordering> {
(**self).partial_cmp(other)
}
}
impl<T: ?Sized> PartialOrd for $RcBorrow<'_, T>
where
T: PartialOrd,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
(**self).partial_cmp(other)
}
}
impl<T: ?Sized> Pointer for $RcBorrow<'_, T>
where
T: Pointer,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
#[cfg(feature = "std")]
impl<T: ?Sized> ToSocketAddrs for $RcBorrow<'_, T>
where
T: ToSocketAddrs
{
type Iter = T::Iter;
fn to_socket_addrs(&self) -> io::Result<T::Iter> {
(**self).to_socket_addrs()
}
}
impl<T: ?Sized> Unpin for $RcBorrow<'_, T> {}
#[cfg(feature = "std")]
impl<T: ?Sized> UnwindSafe for $RcBorrow<'_, T> where T: RefUnwindSafe {}
impl<T: ?Sized> UpperExp for $RcBorrow<'_, T>
where
T: UpperExp,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
impl<T: ?Sized> UpperHex for $RcBorrow<'_, T>
where
T: UpperHex,
{
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
(**self).fmt(f)
}
}
)*}
}
rc_borrow! {
ArcBorrow = Arc
RcBorrow = Rc
}