embedded_fat/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.
15pub struct SearchIdGenerator {
16 next_id: Wrapping<u32>,
17}
18
19impl SearchIdGenerator {
20 /// Create a new generator of Search IDs.
21 pub const fn new(offset: u32) -> Self {
22 Self {
23 next_id: Wrapping(offset),
24 }
25 }
26
27 /// Generate a new, unique [`SearchId`].
28 pub fn get(&mut self) -> SearchId {
29 let id = self.next_id;
30 self.next_id += 1;
31 SearchId(id.0)
32 }
33}