1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
use alloc::rc::Rc;
use core::{borrow::Borrow, fmt, ops::Bound};
#[derive(Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Ref<T>(pub Rc<T>);
impl<T> Clone for Ref<T> {
fn clone(&self) -> Self {
Self(self.0.clone())
}
}
impl<T> fmt::Debug for Ref<T>
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<T: ?Sized>(pub T);
impl<T: ?Sized> Wrapper<T> {
pub fn wrap(value: &T) -> &Self {
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<K, Q> Borrow<Wrapper<Q>> for Ref<K>
where
K: Borrow<Q>,
Q: ?Sized,
{
fn borrow(&self) -> &Wrapper<Q> {
let k: &K = self.0.borrow();
let q: &Q = k.borrow();
Wrapper::wrap(q)
}
}