Skip to main content

commonware_storage/
lib.rs

1//! Persist and retrieve data from an abstract store.
2//!
3//! # Status
4//!
5//! Stability varies by primitive. See [README](https://github.com/commonwarexyz/monorepo#stability) for details.
6
7#![doc(
8    html_logo_url = "https://commonware.xyz/imgs/rustdoc_logo.svg",
9    html_favicon_url = "https://commonware.xyz/favicon.ico"
10)]
11#![cfg_attr(not(any(feature = "std", test)), no_std)]
12
13commonware_macros::stability_scope!(ALPHA {
14    extern crate alloc;
15
16    pub mod bmt;
17    pub mod merkle;
18    pub use merkle::{mmb, mmr};
19});
20commonware_macros::stability_scope!(ALPHA, cfg(feature = "std") {
21    mod bitmap;
22    pub mod qmdb;
23    pub use crate::bitmap::{BitMap as AuthenticatedBitMap, MerkleizedBitMap, UnmerkleizedBitMap};
24    pub mod cache;
25    pub mod queue;
26    #[cfg(any(test, feature = "test-utils"))]
27    pub mod utils;
28});
29commonware_macros::stability_scope!(BETA, cfg(feature = "std") {
30    pub mod archive;
31    pub mod freezer;
32    pub mod index;
33    pub mod journal;
34    pub mod metadata;
35    pub mod ordinal;
36    pub mod rmap;
37
38    /// Section selector for storage operations that act on one or more sections.
39    pub trait Sections {
40        /// Iterator over selected sections.
41        type Iter: Iterator<Item = u64>;
42
43        /// Convert into selected section indices.
44        ///
45        /// This trait does not impose ordering or uniqueness; each storage operation decides how
46        /// to handle duplicates and missing sections.
47        fn sections(self) -> Self::Iter;
48    }
49
50    impl Sections for u64 {
51        type Iter = core::iter::Once<Self>;
52
53        fn sections(self) -> Self::Iter {
54            core::iter::once(self)
55        }
56    }
57
58    impl<const N: usize> Sections for [u64; N] {
59        type Iter = core::array::IntoIter<u64, N>;
60
61        fn sections(self) -> Self::Iter {
62            self.into_iter()
63        }
64    }
65
66    impl<'a, const N: usize> Sections for &'a [u64; N] {
67        type Iter = core::iter::Copied<core::slice::Iter<'a, u64>>;
68
69        fn sections(self) -> Self::Iter {
70            self.iter().copied()
71        }
72    }
73
74    impl<'a> Sections for &'a [u64] {
75        type Iter = core::iter::Copied<core::slice::Iter<'a, u64>>;
76
77        fn sections(self) -> Self::Iter {
78            self.iter().copied()
79        }
80    }
81
82    impl Sections for Vec<u64> {
83        type Iter = std::vec::IntoIter<u64>;
84
85        fn sections(self) -> Self::Iter {
86            self.into_iter()
87        }
88    }
89
90    impl<'a> Sections for &'a Vec<u64> {
91        type Iter = core::iter::Copied<core::slice::Iter<'a, u64>>;
92
93        fn sections(self) -> Self::Iter {
94            self.iter().copied()
95        }
96    }
97
98    impl Sections for std::collections::BTreeSet<u64> {
99        type Iter = std::collections::btree_set::IntoIter<u64>;
100
101        fn sections(self) -> Self::Iter {
102            self.into_iter()
103        }
104    }
105
106    impl<'a> Sections for &'a std::collections::BTreeSet<u64> {
107        type Iter = core::iter::Copied<std::collections::btree_set::Iter<'a, u64>>;
108
109        fn sections(self) -> Self::Iter {
110            self.iter().copied()
111        }
112    }
113
114    /// A runtime context providing storage, timing, and metrics capabilities.
115    ///
116    /// This is a convenience alias for the trait bound `BufferPooler + Storage + Clock + Metrics`
117    /// that appears on nearly every type in this crate.
118    pub trait Context:
119        commonware_runtime::BufferPooler
120        + commonware_runtime::Storage
121        + commonware_runtime::Clock
122        + commonware_runtime::Metrics
123    {
124    }
125    impl<
126            T: commonware_runtime::BufferPooler
127                + commonware_runtime::Storage
128                + commonware_runtime::Clock
129                + commonware_runtime::Metrics,
130        > Context for T
131    {
132    }
133});
134commonware_macros::stability_scope!(BETA {
135    pub mod translator;
136});