summaryrefslogtreecommitdiff
path: root/engine/src/util.rs
blob: 8e939825f4d2ca20913ce5b3d78989c1e9fd9a15 (plain)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
macro_rules! try_option {
    ($expr: expr) => {
        match $expr {
            Ok(value) => value,
            Err(err) => {
                return Some(Err(err.into()));
            }
        }
    };
}

use std::mem::ManuallyDrop;
use std::ops::{Deref, DerefMut};

pub(crate) use try_option;

macro_rules! or {
    (($($tt: tt)+) else ($($else_tt: tt)*)) => {
        $($tt)+
    };

    (() else ($($else_tt: tt)*)) => {
        $($else_tt)*
    };
}

pub(crate) use or;

macro_rules! builder {
    (
        $(#[doc = $doc: literal])*
        #[builder(
            name = $builder_name: ident
            $(, derives = ($($builder_derive: ident),+))?
        )]
        $(#[$attr: meta])*
        $visibility: vis struct $name: ident
        {
            $(
                $(#[$field_attr: meta])*
                $field_visibility: vis $field: ident: $field_type: ty,
            )*
        }
    ) => {
        $(#[doc = $doc])*
        $(#[$attr])*
        $visibility struct $name
        {
            $(
                $(#[$field_attr])*
                $field_visibility $field: $field_type,
            )*
        }

        $(#[derive($($builder_derive),+)])?
        $visibility struct $builder_name
        {
            $(
                $field: $field_type,
            )*
        }

        impl $builder_name
        {
            $(
                #[must_use]
                $visibility fn $field(mut self, $field: $field_type) -> Self
                {
                    self.$field = $field;
                    self
                }
            )*

            #[must_use]
            $visibility fn build(self) -> $name {
                $name {
                    $(
                        $field: self.$field,
                    )*
                }
            }
        }

        impl From<$name> for $builder_name
        {
            fn from(built: $name) -> Self
            {
                Self {
                    $(
                        $field: built.$field,
                    )*
                }
            }
        }
    };
}

pub(crate) use builder;

/// Wrapper that ensures the contained value will never be dropped.
#[derive(Debug)]
pub struct NeverDrop<Value>
{
    value: ManuallyDrop<Value>,
}

impl<Value> NeverDrop<Value>
{
    #[must_use]
    pub fn new(value: Value) -> Self
    {
        Self { value: ManuallyDrop::new(value) }
    }
}

impl<Value> Deref for NeverDrop<Value>
{
    type Target = Value;

    fn deref(&self) -> &Self::Target
    {
        &*self.value
    }
}

impl<Value> DerefMut for NeverDrop<Value>
{
    fn deref_mut(&mut self) -> &mut Self::Target
    {
        &mut *self.value
    }
}