summaryrefslogtreecommitdiff
path: root/engine/src/util.rs
blob: f18a9c7b93307c1a8e53c1a0035fc1dfa103d591 (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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use ecs::util::VecExt;

#[derive(Debug)]
pub struct MapVec<Key: Ord, Value>
{
    inner: Vec<(Key, Value)>,
}

impl<Key: Ord, Value> MapVec<Key, Value>
{
    pub fn insert(&mut self, key: Key, value: Value)
    {
        self.inner
            .insert_at_part_pt_by_key((key, value), |(a_key, _)| a_key);
    }

    pub fn remove(&mut self, key: Key) -> Option<Value>
    {
        let index = self
            .inner
            .binary_search_by_key(&&key, |(a_key, _)| a_key)
            .ok()?;

        let (_, value) = self.inner.remove(index);

        Some(value)
    }

    pub fn get(&self, key: &Key) -> Option<&Value>
    {
        let index = self
            .inner
            .binary_search_by_key(&key, |(a_key, _)| a_key)
            .ok()?;

        let Some((_, value)) = self.inner.get(index) else {
            unreachable!(); // Reason: Index from binary search cannot be OOB
        };

        Some(value)
    }

    pub fn get_mut(&mut self, key: &Key) -> Option<&mut Value>
    {
        let index = self
            .inner
            .binary_search_by_key(&key, |(a_key, _)| a_key)
            .ok()?;

        let Some((_, value)) = self.inner.get_mut(index) else {
            unreachable!(); // Reason: Index from binary search cannot be OOB
        };

        Some(value)
    }

    pub fn values(&self) -> impl Iterator<Item = &Value>
    {
        self.inner.iter().map(|(_, value)| value)
    }
}

impl<Key: Ord, Value> Default for MapVec<Key, Value>
{
    fn default() -> Self
    {
        Self { inner: Vec::new() }
    }
}

pub trait OptionExt<T>
{
    /// Substitute for the currently experimental function
    /// [`Option::get_or_try_insert_with`].
    /// See https://github.com/rust-lang/rust/issues/143648
    fn get_or_try_insert_with_fn<Err>(
        &mut self,
        func: impl Fn() -> Result<T, Err>,
    ) -> Result<&mut T, Err>;
}

impl<T> OptionExt<T> for Option<T>
{
    fn get_or_try_insert_with_fn<Err>(
        &mut self,
        func: impl FnOnce() -> Result<T, Err>,
    ) -> Result<&mut T, Err>
    {
        if let None = self {
            *self = Some(func()?);
        }

        Ok(unsafe { self.as_mut().unwrap_unchecked() })
    }
}

macro_rules! try_option {
    ($expr: expr) => {
        match $expr {
            Ok(value) => value,
            Err(err) => {
                return Some(Err(err.into()));
            }
        }
    };
}

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_export]
macro_rules! expand_map_opt {
    ($in: tt, no_occurance=($($no_occurance: tt)*), occurance=($($occurance: tt)*)) => {
        $($occurance)*
    };

    (, no_occurance=($($no_occurance: tt)*), occurance=($($occurance: tt)*)) => {
        $($no_occurance)*
    };
}

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

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

        impl $builder_name
        {
            $(
                $crate::expand_map_opt!(
                    $(true $($field_skip_generate_fn)?)?,
                    no_occurance=(
                        #[must_use]
                        $visibility fn $field(mut self, $field: $field_type) -> Self
                        {
                            self.$field = $field;
                            self
                        }
                    ),
                    occurance=()
                );
            )*

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

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