use alloc::rc::Rc; use core::{borrow::Borrow, fmt, ops::Bound}; #[derive(Eq, Hash, Ord, PartialEq, PartialOrd)] pub struct Ref(pub Rc); impl Clone for Ref { fn clone(&self) -> Self { Self(self.0.clone()) } } impl fmt::Debug for Ref where T: fmt::Debug, { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.0.fmt(f) } } #[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)] #[repr(transparent)] pub struct Wrapper(pub T); impl Wrapper { pub fn wrap(value: &T) -> &Self { // safe because Wrapper is #[repr(transparent)] unsafe { &*(value as *const T as *const Self) } } pub fn wrap_bound(bound: Bound<&T>) -> Bound<&Self> { match bound { Bound::Included(t) => Bound::Included(Self::wrap(t)), Bound::Excluded(t) => Bound::Excluded(Self::wrap(t)), Bound::Unbounded => Bound::Unbounded, } } } impl Borrow> for Ref where K: Borrow, Q: ?Sized, { fn borrow(&self) -> &Wrapper { // Rc: Borrow let k: &K = self.0.borrow(); // K: Borrow let q: &Q = k.borrow(); Wrapper::wrap(q) } }