maviola 0.3.0

High-level MAVLink communication library with support for essential micro-services.
Documentation
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::marker::PhantomData;
use std::sync::atomic::AtomicBool;
use std::sync::{atomic, Arc};

use crate::core::utils::closable::WillClose;
use crate::core::utils::{Closable, Closer, Sealed, SharedCloser};

/// <sup>🔒</sup>
/// A trait that represents a shared atomic guarded boolean value that can be finalized.
///
/// 🔒 This trait is sealed 🔒
pub trait Flipper: Sealed {}

/// A simple flag that can be either in "on" or "off" state.
///
/// Combined with a [`Guarded`], it will be flipped on drop of the guard.
#[derive(Debug, Default)]
pub struct Flag;

/// A switch that can be either in "on" or "off" state and has a predefined final state.
///
/// Combined with a [`Guarded`], it will be set to the final state, when guard is dropped.
#[derive(Debug, Default)]
pub struct Switch;

/// Guarded flipper.
pub struct Guarded<C: WillClose, F: Flipper> {
    flag: Arc<AtomicBool>,
    guard: C,
    state: Closable,
    _kind: PhantomData<F>,
}

impl Guarded<Closer, Switch> {
    /// Default constructor.
    ///
    /// Creates a guarded switch which is initialized with `false` and will be set to `false` once
    /// guard is dropped.
    ///
    /// # Usage
    ///
    /// ```rust
    /// use maviola::core::utils::Guarded;
    ///
    /// let switch = Guarded::new();
    /// assert!(!switch.is());
    /// ```
    pub fn new() -> Self {
        let guard = Closer::new();
        let state = guard.to_closable();

        Self {
            flag: Arc::new(AtomicBool::new(false)),
            guard,
            state,
            _kind: PhantomData,
        }
    }

    /// Creates default shared switch.
    ///
    /// Default switch is initialized with `false` and will be set to `false` once
    /// guard is dropped.
    ///
    /// # Usage
    ///
    /// ```rust
    /// use maviola::core::utils::Guarded;
    ///
    /// let switch_1 = Guarded::shared();
    /// let mut switch_2 = switch_1.clone();
    ///
    /// switch_2.set(true);
    /// assert!(switch_1.is());
    /// ```
    pub fn shared() -> Guarded<SharedCloser, Switch> {
        Self::new().into_shared()
    }

    /// Sets the value of the switch to `true` in-place, does not have effect if already closed.
    ///
    /// This method takes [`Guarded<Closer, Switch>`] by value and returns an updated version.
    ///
    /// # Usage
    ///
    /// ```rust
    /// use maviola::core::utils::Guarded;
    ///
    /// let switch = Guarded::new().up();
    /// assert!(switch.is());
    /// ```
    pub fn up(self) -> Self {
        if !self.state.is_closed() {
            self.flag.store(true, atomic::Ordering::Release);
        }

        Self {
            flag: self.flag,
            guard: self.guard,
            state: self.state,
            _kind: PhantomData,
        }
    }

    /// Create a shared flag from this switch.
    #[must_use]
    pub fn to_flag(&self) -> Guarded<SharedCloser, Flag> {
        self.to_shared().into_flag()
    }
}

impl<F: Flipper> Guarded<Closer, F> {
    /// Creates a new associated shared guarded flipper.
    ///
    /// the result of this method is marked as `#[must_use]` since if the obtained shared flipper
    /// will be dropped, then original flipper will also receive a closing event.
    #[must_use]
    pub fn to_shared(&self) -> Guarded<SharedCloser, F> {
        Guarded {
            flag: self.flag.clone(),
            guard: self.guard.to_shared(),
            state: self.state.clone(),
            _kind: PhantomData,
        }
    }

    /// Transforms itself into shared guarded flipper.
    pub fn into_shared(self) -> Guarded<SharedCloser, F> {
        Guarded {
            flag: self.flag.clone(),
            guard: self.guard.into_shared(),
            state: self.state.clone(),
            _kind: PhantomData,
        }
    }

    /// Closes the guarded flipper.
    pub fn close(&mut self) {
        self.guard.close()
    }
}

impl<F: Flipper> Guarded<SharedCloser, F> {
    /// Converts this shared closer into a shared flag.
    pub fn into_flag(self) -> Guarded<SharedCloser, Flag> {
        Guarded {
            flag: self.flag.clone(),
            guard: self.guard,
            state: self.state,
            _kind: PhantomData,
        }
    }

    /// Discards this shared guard without triggering a closing event.
    pub fn discard(self) {
        self.guard.discard()
    }

    /// Closes the guarded flipper.
    pub fn close(&mut self) {
        self.guard.close()
    }
}

impl<C: WillClose> Guarded<C, Switch> {
    /// Sets the value of the switch, does not have effect if already closed.
    ///
    /// This method takes [`Guarded<SharedCloser, Switch>`] by mutable reference.
    pub fn set(&mut self, value: bool) {
        if !self.state.is_closed() {
            self.flag.store(value, atomic::Ordering::Release);
        }
    }
}

impl<C: WillClose, F: Flipper> Guarded<C, F> {
    /// Creates a watcher for a flipper.
    ///
    /// A watcher is an instance of [`Guarded<Closable, _>`] which can't initiate closing.
    pub fn to_watcher(&self) -> Guarded<Closable, Flag> {
        Guarded {
            flag: self.flag.clone(),
            guard: self.state.clone(),
            state: self.state.clone(),
            _kind: PhantomData,
        }
    }

    /// Returns the value of a switch.
    ///
    /// Always returns `false` if guard is closed.
    pub fn is(&self) -> bool {
        if self.state.is_closed() {
            false
        } else {
            self.flag.load(atomic::Ordering::Acquire)
        }
    }

    /// Returns `true` if guard is dropped or closed.
    pub fn is_closed(&self) -> bool {
        self.guard.is_closed()
    }
}

impl Default for Guarded<Closer, Switch> {
    fn default() -> Self {
        Self::new()
    }
}

impl<F: Flipper> Clone for Guarded<SharedCloser, F> {
    fn clone(&self) -> Self {
        Self {
            flag: self.flag.clone(),
            guard: self.guard.clone(),
            state: self.state.clone(),
            _kind: PhantomData,
        }
    }
}

impl<F: Flipper> Clone for Guarded<Closable, F> {
    fn clone(&self) -> Self {
        Self {
            flag: self.flag.clone(),
            guard: self.guard.clone(),
            state: self.state.clone(),
            _kind: PhantomData,
        }
    }
}

impl<C: WillClose, F: Flipper> Sealed for Guarded<C, F> {}
impl<C: WillClose, F: Flipper> WillClose for Guarded<C, F> {
    fn is_closed(&self) -> bool {
        self.is_closed()
    }

    fn to_closable(&self) -> Closable {
        self.state.to_closable()
    }
}

impl Sealed for Flag {}
impl Flipper for Flag {}

impl Sealed for Switch {}
impl Flipper for Switch {}

impl From<Closer> for Guarded<Closer, Switch> {
    fn from(value: Closer) -> Self {
        let state = value.to_closable();
        Self {
            flag: Arc::new(AtomicBool::new(false)),
            guard: value,
            state,
            _kind: PhantomData,
        }
    }
}

impl From<&Closer> for Guarded<SharedCloser, Switch> {
    fn from(value: &Closer) -> Self {
        Self {
            flag: Arc::new(AtomicBool::new(false)),
            guard: value.to_shared(),
            state: value.to_closable(),
            _kind: PhantomData,
        }
    }
}

impl From<SharedCloser> for Guarded<SharedCloser, Switch> {
    fn from(value: SharedCloser) -> Self {
        let state = value.to_closable();
        Self {
            flag: Arc::new(AtomicBool::new(false)),
            guard: value,
            state,
            _kind: PhantomData,
        }
    }
}

impl From<&SharedCloser> for Guarded<SharedCloser, Switch> {
    fn from(value: &SharedCloser) -> Self {
        Self {
            flag: Arc::new(AtomicBool::new(false)),
            guard: value.clone(),
            state: value.to_closable(),
            _kind: PhantomData,
        }
    }
}

#[cfg(test)]
mod test_flipper {
    use super::*;

    #[test]
    fn basic_switch_workflow() {
        let switch = Guarded::new();
        assert!(!switch.is());

        let switch = Guarded::new().up();
        assert!(switch.is());

        let mut switch = Guarded::new();
        switch.set(true);
        assert!(switch.is());
        switch.set(false);
        assert!(!switch.is());

        let switch = Guarded::new().up();
        let watcher = switch.to_watcher();

        assert!(watcher.is());
        drop(switch);
        assert!(!watcher.is());
    }

    #[test]
    fn shared_switch_workflow() {
        let mut switch = Guarded::new();
        let mut shared_switch = switch.to_shared();

        assert!(!shared_switch.is());

        switch.set(true);
        assert!(switch.is());
        assert!(shared_switch.is());

        shared_switch.set(false);
        assert!(!switch.is());
        assert!(!shared_switch.is());

        let switch = Guarded::new().up();
        let shared_switch = switch.to_shared();
        drop(switch);
        assert!(!shared_switch.is());

        let switch = Guarded::new().up();
        let shared_switch = switch.to_shared();
        drop(shared_switch);
        assert!(!switch.is());
    }

    #[test]
    fn state_between_shared_switches_is_shared() {
        let shared_switch_1 = Guarded::shared();
        let mut shared_switch_2 = shared_switch_1.clone();

        assert!(!shared_switch_1.is_closed());
        assert!(!shared_switch_2.is_closed());

        shared_switch_2.set(true);
        assert!(shared_switch_2.is());
        assert!(shared_switch_1.is());
    }

    #[test]
    fn to_shared_switch_discard() {
        let switch = Guarded::new().up();
        switch.to_shared().discard();
        assert!(switch.is());

        let switch = Guarded::new().up();
        _ = switch.to_shared();
        assert!(!switch.is());
    }

    #[test]
    fn test_shared_flags() {
        let mut switch = Guarded::new();
        let flag = switch.to_flag();

        switch.set(true);
        assert!(flag.is());

        drop(flag);
        assert!(switch.is_closed());

        let switch = Guarded::new();
        let flag = switch.to_flag();

        drop(switch);
        assert!(flag.is_closed());
    }

    #[test]
    fn test_watcher_flags() {
        let mut switch = Guarded::new();
        let flag = switch.to_watcher();

        switch.set(true);
        assert!(flag.is());

        drop(switch);
        assert!(flag.is_closed());
        assert!(!flag.is());
    }
}