embedded_sdmmc/filesystem/
search_id.rs

1use core::num::Wrapping;
2
3#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
5/// Unique ID used to search for files and directories in the open Volume/File/Directory lists
6pub struct SearchId(pub(crate) u32);
7
8/// A Search ID generator.
9///
10/// This object will always return a different ID.
11///
12/// Well, it will wrap after `2**32` IDs. But most systems won't open that many
13/// files, and if they do, they are unlikely to hold one file open and then
14/// open/close `2**32 - 1` others.
15#[derive(Debug)]
16pub struct SearchIdGenerator {
17    next_id: Wrapping<u32>,
18}
19
20impl SearchIdGenerator {
21    /// Create a new generator of Search IDs.
22    pub const fn new(offset: u32) -> Self {
23        Self {
24            next_id: Wrapping(offset),
25        }
26    }
27
28    /// Generate a new, unique [`SearchId`].
29    pub fn get(&mut self) -> SearchId {
30        let id = self.next_id;
31        self.next_id += 1;
32        SearchId(id.0)
33    }
34}
35
36// ****************************************************************************
37//
38// End Of File
39//
40// ****************************************************************************