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