atecc608b/slot.rs
1// Copyright (c) 2026 Tuloup Simon
2//
3// This program is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// any later version.
7//
8// This program is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16//! Slot identifiers.
17//!
18//! The ATECC608B exposes 16 slots, numbered 0 to 15. This module provides a
19//! type-safe wrapper to avoid passing arbitrary `u8` values around.
20
21/// Total number of slots on the chip.
22pub(crate) const SLOT_COUNT: u8 = 16;
23
24/// A validated slot identifier.
25#[derive(Debug, Clone, Copy, PartialEq, Eq)]
26#[cfg_attr(feature = "defmt", derive(defmt::Format))]
27pub struct Slot(u8);
28
29impl Slot
30{
31 /// Try to build a slot from a raw `u8`. Returns `None` if `index >= 16`.
32 #[must_use]
33 pub const fn new(index: u8) -> Option<Self>
34 {
35 if index < SLOT_COUNT
36 {
37 Some(Self(index))
38 }
39 else
40 {
41 None
42 }
43 }
44
45 /// Build a slot from a known-valid constant.
46 ///
47 /// # Panics
48 /// Panics at compile time if `index >= 16`.
49 #[must_use]
50 pub const fn const_new(index: u8) -> Self
51 {
52 assert!(index < SLOT_COUNT, "slot index out of range");
53 Self(index)
54 }
55
56 /// Return the raw slot index.
57 #[must_use]
58 pub const fn as_u8(self) -> u8
59 {
60 self.0
61 }
62}