1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![doc = include_str!("../README.md")]
3#![allow(renamed_and_removed_lints)] #![allow(unknown_lints)] #![warn(missing_docs)]
7#![warn(noop_method_call)]
8#![warn(unreachable_pub)]
9#![warn(clippy::all)]
10#![deny(clippy::await_holding_lock)]
11#![deny(clippy::cargo_common_metadata)]
12#![deny(clippy::cast_lossless)]
13#![deny(clippy::checked_conversions)]
14#![allow(clippy::cognitive_complexity)] #![deny(clippy::debug_assert_with_mut_call)]
16#![deny(clippy::exhaustive_enums)]
17#![deny(clippy::exhaustive_structs)]
18#![deny(clippy::expl_impl_clone_on_copy)]
19#![deny(clippy::fallible_impl_from)]
20#![deny(clippy::implicit_clone)]
21#![deny(clippy::large_stack_arrays)]
22#![warn(clippy::manual_ok_or)]
23#![deny(clippy::missing_docs_in_private_items)]
24#![warn(clippy::needless_borrow)]
25#![warn(clippy::needless_pass_by_value)]
26#![warn(clippy::option_option)]
27#![warn(clippy::or_fun_call)]
28#![deny(clippy::print_stderr)]
29#![deny(clippy::print_stdout)]
30#![warn(clippy::rc_buffer)]
31#![deny(clippy::ref_option_ref)]
32#![warn(clippy::semicolon_if_nothing_returned)]
33#![warn(clippy::trait_duplication_in_bounds)]
34#![deny(clippy::unchecked_time_subtraction)]
35#![deny(clippy::unnecessary_wraps)]
36#![warn(clippy::unseparated_literal_suffix)]
37#![deny(clippy::unwrap_used)]
38#![deny(clippy::mod_module_files)]
39#![allow(clippy::let_unit_value)] #![allow(clippy::uninlined_format_args)]
41#![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)] #![allow(clippy::needless_raw_string_hashes)] #![allow(clippy::needless_lifetimes)] #![allow(mismatched_lifetime_syntaxes)] #![allow(clippy::collapsible_if)] #![deny(clippy::unused_async)]
48#![deny(clippy::string_slice)] #![allow(recursion_depth_exceeding_limit)] #![cfg_attr(not(all(feature = "experimental", feature = "full")), allow(unused))]
54
55mod err;
56#[cfg(not(target_arch = "wasm32"))]
57mod fs;
58mod fs_mistrust_error_ext;
59mod handle;
60pub mod hsnickname;
61mod load_store;
62pub mod slug;
63#[cfg(feature = "testing")]
64mod testing;
65
66#[cfg(feature = "state-dir")]
67pub mod state_dir;
68
69use serde::{Deserialize, Serialize, de::DeserializeOwned};
70use std::sync::Arc;
71
72type Result<T> = std::result::Result<T, crate::Error>;
74
75pub use err::{Error, ErrorSource};
76#[cfg(not(target_arch = "wasm32"))]
77pub use fs::FsStateMgr;
78pub use fs_mistrust_error_ext::FsMistrustErrorExt;
79pub use handle::{DynStorageHandle, StorageHandle};
80pub use serde_json::Value as JsonValue;
81#[cfg(feature = "testing")]
82pub use testing::TestingStateMgr;
83
84pub trait StateMgr: Clone {
95 fn load<D>(&self, key: &str) -> Result<Option<D>>
99 where
100 D: DeserializeOwned;
101 fn store<S>(&self, key: &str, val: &S) -> Result<()>
105 where
106 S: Serialize;
107 fn can_store(&self) -> bool;
112
113 fn try_lock(&self) -> Result<LockStatus>;
121
122 fn unlock(&self) -> Result<()>;
125
126 fn create_handle<T>(self, key: impl Into<String>) -> DynStorageHandle<T>
129 where
130 Self: Send + Sync + Sized + 'static,
131 T: Serialize + DeserializeOwned + 'static,
132 {
133 Arc::new(handle::StorageHandleImpl::new(self, key.into()))
134 }
135}
136
137#[allow(clippy::exhaustive_enums)]
139#[derive(Debug, Copy, Clone, Eq, PartialEq)]
140#[must_use]
141pub enum LockStatus {
142 NoLock,
144 AlreadyHeld,
146 NewlyAcquired,
148}
149
150impl LockStatus {
151 pub fn held(&self) -> bool {
153 !matches!(self, LockStatus::NoLock)
154 }
155}
156
157#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
162#[serde(untagged)]
163#[allow(clippy::exhaustive_enums)]
164pub enum Futureproof<T> {
165 Understandable(T),
167 Unknown(JsonValue),
169}
170
171impl<T> Futureproof<T> {
172 pub fn into_option(self) -> Option<T> {
174 match self {
175 Futureproof::Understandable(x) => Some(x),
176 Futureproof::Unknown(_) => None,
177 }
178 }
179}
180
181impl<T> From<T> for Futureproof<T> {
182 fn from(inner: T) -> Self {
183 Self::Understandable(inner)
184 }
185}