#![cfg_attr(docsrs, feature(doc_auto_cfg, doc_cfg))]
#![doc = include_str!("../README.md")]
#![cfg_attr(not(ci_arti_stable), allow(renamed_and_removed_lints))]
#![cfg_attr(not(ci_arti_nightly), allow(unknown_lints))]
#![deny(missing_docs)]
#![warn(noop_method_call)]
#![deny(unreachable_pub)]
#![warn(clippy::all)]
#![deny(clippy::await_holding_lock)]
#![deny(clippy::cargo_common_metadata)]
#![deny(clippy::cast_lossless)]
#![deny(clippy::checked_conversions)]
#![warn(clippy::cognitive_complexity)]
#![deny(clippy::debug_assert_with_mut_call)]
#![deny(clippy::exhaustive_enums)]
#![deny(clippy::exhaustive_structs)]
#![deny(clippy::expl_impl_clone_on_copy)]
#![deny(clippy::fallible_impl_from)]
#![deny(clippy::implicit_clone)]
#![deny(clippy::large_stack_arrays)]
#![warn(clippy::manual_ok_or)]
#![deny(clippy::missing_docs_in_private_items)]
#![deny(clippy::missing_panics_doc)]
#![warn(clippy::needless_borrow)]
#![warn(clippy::needless_pass_by_value)]
#![warn(clippy::option_option)]
#![warn(clippy::rc_buffer)]
#![deny(clippy::ref_option_ref)]
#![warn(clippy::semicolon_if_nothing_returned)]
#![warn(clippy::trait_duplication_in_bounds)]
#![deny(clippy::unnecessary_wraps)]
#![warn(clippy::unseparated_literal_suffix)]
#![deny(clippy::unwrap_used)]
#![allow(clippy::let_unit_value)] #![allow(clippy::significant_drop_in_scrutinee)] #![allow(clippy::result_large_err)]
mod err;
#[cfg(not(target_arch = "wasm32"))]
mod fs;
mod handle;
#[cfg(feature = "testing")]
mod testing;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::sync::Arc;
type Result<T> = std::result::Result<T, crate::Error>;
pub use err::Error;
#[cfg(not(target_arch = "wasm32"))]
pub use fs::FsStateMgr;
pub use handle::{DynStorageHandle, StorageHandle};
pub use serde_json::Value as JsonValue;
#[cfg(feature = "testing")]
pub use testing::TestingStateMgr;
pub trait StateMgr: Clone {
fn load<D>(&self, key: &str) -> Result<Option<D>>
where
D: DeserializeOwned;
fn store<S>(&self, key: &str, val: &S) -> Result<()>
where
S: Serialize;
fn can_store(&self) -> bool;
fn try_lock(&self) -> Result<LockStatus>;
fn unlock(&self) -> Result<()>;
fn create_handle<T>(self, key: impl Into<String>) -> DynStorageHandle<T>
where
Self: Send + Sync + Sized + 'static,
T: Serialize + DeserializeOwned + 'static,
{
Arc::new(handle::StorageHandleImpl::new(self, key.into()))
}
}
#[allow(clippy::exhaustive_enums)]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum LockStatus {
NoLock,
AlreadyHeld,
NewlyAcquired,
}
impl LockStatus {
pub fn held(&self) -> bool {
!matches!(self, LockStatus::NoLock)
}
}
#[derive(Clone, Serialize, Deserialize, Debug, PartialEq, Eq)]
#[serde(untagged)]
#[allow(clippy::exhaustive_enums)]
pub enum Futureproof<T> {
Understandable(T),
Unknown(JsonValue),
}
impl<T> Futureproof<T> {
pub fn into_option(self) -> Option<T> {
match self {
Futureproof::Understandable(x) => Some(x),
Futureproof::Unknown(_) => None,
}
}
}
impl<T> From<T> for Futureproof<T> {
fn from(inner: T) -> Self {
Self::Understandable(inner)
}
}