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
use core::num::Wrapping;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
/// Unique ID used to search for files and directories in the open Volume/File/Directory lists
pub struct SearchId(pub(crate) u32);

/// A Search ID generator.
///
/// This object will always return a different ID.
///
/// Well, it will wrap after `2**32` IDs. But most systems won't open that many
/// files, and if they do, they are unlikely to hold one file open and then
/// open/close `2**32 - 1` others.
pub struct SearchIdGenerator {
    next_id: Wrapping<u32>,
}

impl SearchIdGenerator {
    /// Create a new generator of Search IDs.
    pub const fn new(offset: u32) -> Self {
        Self {
            next_id: Wrapping(offset),
        }
    }

    /// Generate a new, unique [`SearchId`].
    pub fn get(&mut self) -> SearchId {
        let id = self.next_id;
        self.next_id += 1;
        SearchId(id.0)
    }
}