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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
use ::core::{mem::ManuallyDrop, ptr::read};
use crate::Destructure;
pub trait Destructuring {}
pub trait DestructuringFor<T>: Destructuring {
type Destructurer: Destructurer<Inner = T>;
}
pub trait Destructurer {
type Inner: Destructure;
fn new(inner: Self::Inner) -> Self;
fn inner(&self) -> &Self::Inner;
fn inner_mut(&mut self) -> &mut Self::Inner;
}
pub trait Test<'a> {
type Test;
unsafe fn test(&'a mut self) -> Self::Test;
}
pub struct Ref<T>(T);
impl<T: Destructure> Destructurer for Ref<T> {
type Inner = T;
fn new(inner: T) -> Self {
Self(inner)
}
fn inner(&self) -> &Self::Inner {
&self.0
}
fn inner_mut(&mut self) -> &mut Self::Inner {
&mut self.0
}
}
impl<'a, T: 'a + Destructure> Test<'a> for Ref<T> {
type Test = &'a T::Underlying;
unsafe fn test(&'a mut self) -> Self::Test {
unsafe { &*T::underlying(&mut self.0) }
}
}
pub struct Value<T>(ManuallyDrop<T>);
impl<T: Destructure> Destructurer for Value<T> {
type Inner = T;
fn new(inner: T) -> Self {
Self(ManuallyDrop::new(inner))
}
fn inner(&self) -> &Self::Inner {
&self.0
}
fn inner_mut(&mut self) -> &mut Self::Inner {
&mut self.0
}
}
impl<'a, T: 'a + Destructure> Test<'a> for Value<T>
where
T::Underlying: Sized,
{
type Test = T::Underlying;
unsafe fn test(&'a mut self) -> Self::Test {
unsafe { read(T::underlying(&mut self.0)) }
}
}