iceoryx2_cal/static_storage/
mod.rs1pub mod file;
17pub mod process_local;
18pub mod recommended;
19
20use core::{fmt::Debug, time::Duration};
21
22use iceoryx2_bb_elementary_traits::testing::abandonable::Abandonable;
23use iceoryx2_bb_system_types::file_name::*;
24use iceoryx2_log::fail;
25
26use crate::named_concept::{
27 NamedConcept, NamedConceptBuilder, NamedConceptConfiguration, NamedConceptMgmt,
28};
29
30#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
31pub enum StaticStorageCreateError {
32 AlreadyExists,
33 Creation,
34 Write,
35 InsufficientPermissions,
36 InternalError,
37}
38
39impl core::fmt::Display for StaticStorageCreateError {
40 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
41 write!(f, "StaticStorageCreateError::{self:?}")
42 }
43}
44
45impl core::error::Error for StaticStorageCreateError {}
46
47#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
48pub enum StaticStorageOpenError {
49 DoesNotExist,
50 Read,
51 InitializationNotYetFinalized,
52 InternalError,
53}
54
55impl core::fmt::Display for StaticStorageOpenError {
56 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
57 write!(f, "StaticStorageOpenError::{self:?}")
58 }
59}
60
61impl core::error::Error for StaticStorageOpenError {}
62
63#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
64pub enum StaticStorageReadError {
65 BufferTooSmall,
66 ReadError,
67 StaticStorageWasModified,
68 CreationNotComplete,
69}
70
71impl core::fmt::Display for StaticStorageReadError {
72 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
73 write!(f, "StaticStorageReadError::{self:?}")
74 }
75}
76
77impl core::error::Error for StaticStorageReadError {}
78
79#[derive(Debug, Clone, Copy, Eq, Hash, PartialEq)]
80pub enum StaticStorageUnlockError {
81 InsufficientPermissions,
82 NoSpaceLeft,
83 InternalError,
84}
85
86impl core::fmt::Display for StaticStorageUnlockError {
87 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
88 write!(f, "StaticStorageUnlockError::{self:?}")
89 }
90}
91
92impl core::error::Error for StaticStorageUnlockError {}
93
94pub trait StaticStorageConfiguration: Clone + Default + NamedConceptConfiguration {}
97
98pub trait StaticStorageBuilder<T: StaticStorage>: Sized + NamedConceptBuilder<T> {
102 fn has_ownership(self, value: bool) -> Self;
104
105 fn create(self, contents: &[u8]) -> Result<T, StaticStorageCreateError> {
108 let locked_storage = self.create_locked()?;
109
110 Ok(
111 fail!(from "StaticStorageBuilder::create", when locked_storage.unlock(contents),
112 with StaticStorageCreateError::Write,
113 "Unable to unlock static storage with content"),
114 )
115 }
116
117 fn create_locked(self) -> Result<T::Locked, StaticStorageCreateError>;
121
122 fn open(self, timeout: Duration) -> Result<T, StaticStorageOpenError>;
128}
129
130pub trait StaticStorageLocked<T: StaticStorage>: Sized + NamedConcept + Abandonable {
132 fn unlock(self, contents: &[u8]) -> Result<T, StaticStorageUnlockError>;
134}
135
136pub trait StaticStorage:
139 Debug + Sized + NamedConceptMgmt + NamedConcept + Send + Sync + Abandonable
140{
141 type Builder: StaticStorageBuilder<Self> + NamedConceptBuilder<Self>;
142 type Locked: StaticStorageLocked<Self>;
143
144 fn len(&self) -> u64;
147
148 fn is_empty(&self) -> bool;
150
151 fn read(&self, content: &mut [u8]) -> Result<(), StaticStorageReadError>;
154
155 fn release_ownership(&self);
158
159 fn acquire_ownership(&self);
162
163 fn default_suffix() -> FileName {
165 unsafe { FileName::new_unchecked(b".static_storage") }
166 }
167}