munge/
internal.rs

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
use core::mem::ManuallyDrop;

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;
}

pub struct Borrow<T>(T);

impl<T: Destructure> Destructurer for Borrow<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 Borrow<T> {
    type Test = &'a T::Underlying;
}

pub struct Move<T>(ManuallyDrop<T>);

impl<T: Destructure> Destructurer for Move<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 Move<T>
where
    T::Underlying: Sized,
{
    type Test = T::Underlying;
}