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
55
56
57
// Not public API. Use #[derive(RefCastCustom)] and #[ref_cast_custom].
#[doc(hidden)]
pub unsafe trait RefCastCustom<From: ?Sized> {
    type CurrentCrate;
    fn __static_assert() {}
}

#[doc(hidden)]
pub unsafe trait RefCastOkay<From>: Sealed<From> {
    type CurrentCrate;
    type Target: ?Sized;
}

unsafe impl<'a, From, To> RefCastOkay<&'a From> for &'a To
where
    From: ?Sized,
    To: ?Sized + RefCastCustom<From>,
{
    type CurrentCrate = To::CurrentCrate;
    type Target = To;
}

unsafe impl<'a, From, To> RefCastOkay<&'a mut From> for &'a mut To
where
    From: ?Sized,
    To: ?Sized + RefCastCustom<From>,
{
    type CurrentCrate = To::CurrentCrate;
    type Target = To;
}

#[doc(hidden)]
pub trait Sealed<From> {}

impl<'a, From, To> Sealed<&'a From> for &'a To
where
    From: ?Sized,
    To: ?Sized + RefCastCustom<From>,
{
}

impl<'a, From, To> Sealed<&'a mut From> for &'a mut To
where
    From: ?Sized,
    To: ?Sized + RefCastCustom<From>,
{
}

#[doc(hidden)]
pub type CurrentCrate<From, To> = <To as RefCastOkay<From>>::CurrentCrate;

#[doc(hidden)]
pub fn ref_cast_custom<From, To>(_arg: From)
where
    To: RefCastOkay<From>,
{
}