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
use core::fmt::{Debug, Formatter};
use core::sync::atomic::Ordering;

use crate::protocol::{Behold, Sequence};

#[cfg(not(feature = "alloc"))]
use no_std::_Sequencer;
#[cfg(feature = "alloc")]
use standard::_Sequencer;

/// Converts value into a [`Sequencer`].
///
/// This trait is implemented for [`Sequencer`] and [`Sequence`]. For the latter it creates a new
/// sequencer initialized with sequence value.
///
/// This trait also implemented for [`Sequencer`] passed by reference but behavior is different for
/// `alloc` and `no_alloc` targets:
///
/// * `alloc`: clones a sequencer
/// * `no_alloc`: forks a sequencer using [`Sequencer::fork`]
pub trait IntoSequencer {
    /// Converts value into a [`Sequencer`].
    fn into_sequencer(self) -> Sequencer;
}

/// Incremental MAVLink frame sequence.
///
/// # Examples
///
/// ```rust
/// use mavio::protocol::Sequencer;
///
/// // Start a new sequence
/// let seq = Sequencer::new();
/// assert_eq!(seq.next(), 0, "initial value");
/// assert_eq!(seq.next(), 1, "should increment");
///
/// // For original sequence to an independent counter
/// let forked = seq.fork();
/// assert_eq!(forked.next(), 2, "should increment");
/// assert_eq!(forked.next(), 3, "should increment");
/// assert_eq!(seq.current(), 2, "should be independent");
///
/// // Synchronize original sequence with the forked one
/// seq.sync(&forked);
/// assert_eq!(seq.next(), 4, "should be updated");
/// assert_eq!(forked.next(), 4, "forked sequence is still independent");
/// ```
pub struct Sequencer(_Sequencer);

impl Sequencer {
    /// Default constructor, starts from `0`.
    #[inline(always)]
    pub fn new() -> Self {
        Self::init(0)
    }

    /// Instantiates sequencer initialized with a specific `value`.
    #[inline(always)]
    pub fn init(value: Sequence) -> Self {
        Self(_Sequencer::init_with(value))
    }

    /// Forks sequencer to a new independent sequence counter that starts from the next value.
    ///
    /// Forking is similar to cloning and available for all targets, while cloning is possible only
    /// for `alloc` targets.
    ///
    /// # Examples
    ///
    /// ```
    /// use mavio::protocol::Sequencer;
    ///
    /// let seq = Sequencer::new();
    /// let forked = seq.fork();
    ///
    /// assert_eq!(forked.next(), 0, "initial value");
    /// assert_eq!(forked.next(), 1, "should increment");
    /// assert_eq!(seq.current(), 0, "should stay the same");
    /// ```
    #[inline(always)]
    pub fn fork(&self) -> Self {
        Self::init(self.current())
    }

    /// <sup>`alloc`</sup>
    /// Joins another sequencer with current one.
    ///
    /// From this moment both sequencers will share the same counter. The current sequence will be
    /// synced with the one it joins.
    ///
    /// Available only when `alloc` feature is enabled.
    ///
    /// # Examples
    ///
    /// ```rust
    /// # #[cfg(feature = "alloc")]{
    /// use mavio::protocol::Sequencer;
    ///
    /// let main = Sequencer::new();
    /// let mut forked = main.fork();
    ///
    /// // Proceed with the forked sequence
    /// assert_eq!(forked.next(), 0, "initial value");
    /// assert_eq!(forked.next(), 1, "should increment");
    /// assert_eq!(forked.next(), 2, "should increment");
    ///
    /// main.join(&mut forked);
    /// assert_eq!(main.next(), 3, "should be synced with the joined sequence");
    /// assert_eq!(forked.next(), 4, "joined sequence now shares the same counter");
    /// }
    /// ```
    #[cfg(feature = "alloc")]
    pub fn join(&self, other: &mut Sequencer) {
        self.sync(other);
        other.0 = self.0.clone();
    }

    /// Synchronizes this sequencer with another one.
    #[inline(always)]
    pub fn sync(&self, other: &Sequencer) {
        self.rewind(other.current())
    }

    /// Obtains the next value of the sequence.
    #[inline(always)]
    pub fn next(&self) -> Sequence {
        self.next_value()
    }

    /// Returns the current value without incrementing internal counter.
    #[inline(always)]
    pub fn current(&self) -> Sequence {
        self.0 .0.load(Ordering::Acquire)
    }

    /// Rewinds sequence to a specific value.
    #[inline(always)]
    pub fn rewind(&self, value: Sequence) {
        self.0 .0.store(value, Ordering::Release)
    }

    /// Skips `increment` items in sequence and return the updated current value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use mavio::protocol::Sequencer;
    ///
    /// let seq = Sequencer::new();
    /// seq.advance(3).discard();
    /// assert_eq!(seq.next(), 3, "should skip 0, 1, and 2");
    /// ```
    ///
    /// The return value is wrapped with [`Behold`] since it is not guaranteed in multithreaded
    /// environments that the [`Sequencer::next`] will return the same value in this thread. Use
    /// [`Behold::unwrap`] to explicitly acknowledge that you understand what you are doing and
    /// retrieve the value or discard it by calling [`Behold::discard`].
    #[inline(always)]
    pub fn advance(&self, increment: Sequence) -> Behold<Sequence> {
        Behold::new(self.0 .0.fetch_add(increment, Ordering::Release) + increment)
    }

    #[inline]
    fn next_value(&self) -> Sequence {
        self.0 .0.fetch_add(1, Ordering::Release)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for Sequencer {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_u8(self.current())
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Sequencer {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let value = u8::deserialize(d)?;
        Ok(Sequencer::init(value))
    }
}

impl IntoSequencer for Sequencer {
    /// Passes [`Sequencer`] unchanged.
    #[inline(always)]
    fn into_sequencer(self) -> Sequencer {
        self
    }
}

#[cfg(not(feature = "alloc"))]
impl IntoSequencer for &Sequencer {
    /// Forks a reference to a [`Sequencer`] into a new forked sequencer for `no_alloc` targets.
    #[inline(always)]
    fn into_sequencer(self) -> Sequencer {
        self.fork()
    }
}

#[cfg(feature = "alloc")]
impl IntoSequencer for &Sequencer {
    /// Clones [`Sequencer`] passed by reference for `alloc` targets.
    #[inline(always)]
    fn into_sequencer(self) -> Sequencer {
        self.clone()
    }
}

impl IntoSequencer for Sequence {
    /// Creates a [`Sequencer`] initialized with [`Sequence`] value.
    #[inline]
    fn into_sequencer(self) -> Sequencer {
        Sequencer::init(self)
    }
}

impl From<&Sequencer> for Sequencer {
    fn from(value: &Sequencer) -> Self {
        value.into_sequencer()
    }
}

impl From<Sequence> for Sequencer {
    fn from(value: Sequence) -> Self {
        value.into_sequencer()
    }
}

#[cfg(feature = "alloc")]
impl Clone for Sequencer {
    /// <sup>`alloc`</sup>
    /// Creates a new sequencer which shares the same counter with the current one.
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

impl Default for Sequencer {
    /// Creates a default [`Sequencer`] starting from `0`.
    fn default() -> Self {
        Self::new()
    }
}

impl Iterator for Sequencer {
    type Item = Sequence;

    /// Gets the next element of a sequence. Always returns [`Some`].
    fn next(&mut self) -> Option<Self::Item> {
        Some(self.next_value())
    }
}

impl Debug for Sequencer {
    /// Formats [`Sequencer`] showing its current value.
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        f.debug_tuple("Sequencer").field(&self.current()).finish()
    }
}

#[cfg(feature = "alloc")]
mod standard {
    use alloc::sync::Arc;
    use core::sync::atomic::AtomicU8;

    use crate::protocol::Sequence;

    #[derive(Clone)]
    pub struct _Sequencer(pub(super) Arc<AtomicU8>);

    impl _Sequencer {
        #[inline]
        pub(super) fn init_with(value: Sequence) -> Self {
            Self(Arc::new(AtomicU8::new(value)))
        }
    }
}

#[cfg(not(feature = "alloc"))]
mod no_std {
    use core::sync::atomic::AtomicU8;

    use crate::protocol::Sequence;

    pub struct _Sequencer(pub(super) AtomicU8);

    impl _Sequencer {
        #[inline]
        pub(super) fn init_with(value: Sequence) -> Self {
            Self(AtomicU8::new(value))
        }
    }
}