1use core::fmt;
4use core::marker::PhantomData;
5use core::ptr::Pointee;
6
7use crate::ErasedNonNull;
8
9pub struct ErasedRef<'a> {
16 ptr: ErasedNonNull,
17 _phantom: PhantomData<&'a ()>,
18}
19
20impl<'a> ErasedRef<'a> {
21 pub fn new<T: ?Sized>(val: &'a T) -> ErasedRef<'a> {
23 ErasedRef {
24 ptr: ErasedNonNull::from(val),
25 _phantom: PhantomData,
26 }
27 }
28
29 pub fn as_ptr(&self) -> &ErasedNonNull {
31 &self.ptr
32 }
33
34 pub unsafe fn reify_ref<T: ?Sized + Pointee>(&self) -> &T {
40 self.ptr.reify_ptr::<T>().as_ref()
41 }
42}
43
44impl fmt::Pointer for ErasedRef<'_> {
45 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
46 fmt::Pointer::fmt(&self.ptr, f)
47 }
48}
49
50impl fmt::Debug for ErasedRef<'_> {
51 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52 f.debug_struct("ErasedRef")
53 .field("ptr", &self.ptr)
54 .finish_non_exhaustive()
55 }
56}
57
58pub struct ErasedMut<'a> {
65 ptr: ErasedNonNull,
66 _phantom: PhantomData<&'a mut ()>,
67}
68
69impl<'a> ErasedMut<'a> {
70 pub fn new<T: ?Sized>(val: &'a mut T) -> ErasedMut<'a> {
72 ErasedMut {
73 ptr: ErasedNonNull::from(val),
74 _phantom: PhantomData,
75 }
76 }
77
78 pub fn as_ptr(&self) -> &ErasedNonNull {
80 &self.ptr
81 }
82
83 pub unsafe fn reify_ref<T: ?Sized + Pointee>(&mut self) -> &mut T {
89 self.ptr.reify_ptr::<T>().as_mut()
90 }
91}
92
93impl fmt::Pointer for ErasedMut<'_> {
94 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
95 fmt::Pointer::fmt(&self.ptr, f)
96 }
97}
98
99impl fmt::Debug for ErasedMut<'_> {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 f.debug_struct("ErasedRef")
102 .field("ptr", &self.ptr)
103 .finish_non_exhaustive()
104 }
105}