1use crate::Error;
2use std::{future::Future, pin::Pin};
3
4pub const PREFIX_LEN: usize = 4;
6
7pub type Prefix = [u8; PREFIX_LEN];
12
13pub type KvPairs = Vec<(Vec<u8>, Vec<u8>)>;
15
16pub type BoxFuture<'a, T> = Pin<Box<dyn Future<Output = T> + Send + 'a>>;
18
19pub fn storage_key(prefix: &Prefix, suffix: &[u8]) -> Vec<u8> {
21 let mut key = Vec::with_capacity(PREFIX_LEN + suffix.len());
22 key.extend_from_slice(prefix);
23 key.extend_from_slice(suffix);
24 key
25}
26
27pub trait Storage: Send + Sync {
33 fn get(&self, key: &[u8]) -> BoxFuture<'_, Result<Option<Vec<u8>>, Error>>;
35
36 fn set(&self, key: &[u8], value: Vec<u8>) -> BoxFuture<'_, Result<(), Error>>;
38
39 fn increment(&self, key: &[u8], delta: i64) -> BoxFuture<'_, Result<i64, Error>>;
42
43 fn list(&self, prefix: &Prefix) -> BoxFuture<'_, Result<KvPairs, Error>>;
46
47 fn delete(&self, key: &[u8]) -> BoxFuture<'_, Result<(), Error>>;
49}
50
51impl Storage for () {
57 fn get(&self, _key: &[u8]) -> BoxFuture<'_, Result<Option<Vec<u8>>, Error>> {
58 Box::pin(async { Err(Error::Internal("storage not configured".into())) })
59 }
60
61 fn set(&self, _key: &[u8], _value: Vec<u8>) -> BoxFuture<'_, Result<(), Error>> {
62 Box::pin(async { Err(Error::Internal("storage not configured".into())) })
63 }
64
65 fn increment(&self, _key: &[u8], _delta: i64) -> BoxFuture<'_, Result<i64, Error>> {
66 Box::pin(async { Err(Error::Internal("storage not configured".into())) })
67 }
68
69 fn list(&self, _prefix: &Prefix) -> BoxFuture<'_, Result<KvPairs, Error>> {
70 Box::pin(async { Err(Error::Internal("storage not configured".into())) })
71 }
72
73 fn delete(&self, _key: &[u8]) -> BoxFuture<'_, Result<(), Error>> {
74 Box::pin(async { Err(Error::Internal("storage not configured".into())) })
75 }
76}