#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![cfg_attr(not(feature = "std"), no_std)]
#[macro_use]
extern crate amplify;
mod providers;
mod types;
use core::fmt::Display;
use amplify::hex::ToHex;
#[allow(unused_imports)]
pub use crate::providers::*;
pub use crate::types::*;
pub trait AoraMap<K, V, const KEY_LEN: usize = 32>
where K: Into<[u8; KEY_LEN]> + From<[u8; KEY_LEN]>
{
fn len(&self) -> usize;
fn is_empty(&self) -> bool { self.len() == 0 }
fn contains_key(&self, key: K) -> bool;
fn get(&self, key: K) -> Option<V>;
fn get_expect(&self, key: K) -> V { self.get(key).expect("key not found") }
fn insert(&mut self, key: K, item: &V);
fn extend<'a>(&mut self, iter: impl IntoIterator<Item = (K, &'a V)>)
where V: 'a {
for (key, item) in iter {
self.insert(key, item);
}
}
fn iter(&self) -> impl Iterator<Item = (K, V)>;
}
pub trait AoraIndex<K, V, const KEY_LEN: usize = 32, const VAL_LEN: usize = 32>
where
K: Into<[u8; KEY_LEN]> + From<[u8; KEY_LEN]>,
V: Into<[u8; VAL_LEN]> + From<[u8; VAL_LEN]>,
{
fn len(&self) -> usize;
fn is_empty(&self) -> bool { self.len() == 0 }
fn keys(&self) -> impl Iterator<Item = K>;
fn contains_key(&self, key: K) -> bool { self.value_len(key) > 0 }
fn value_len(&self, key: K) -> usize;
fn get(&self, key: K) -> impl ExactSizeIterator<Item = V>;
fn push(&mut self, key: K, val: V);
}
pub trait AuraMap<K, V, const KEY_LEN: usize = 32, const VAL_LEN: usize = 32>
where
K: Into<[u8; KEY_LEN]> + From<[u8; KEY_LEN]>,
V: Into<[u8; VAL_LEN]> + From<[u8; VAL_LEN]>,
{
fn display(&self) -> impl Display;
fn keys(&self) -> impl Iterator<Item = K>;
fn contains_key(&self, key: K) -> bool;
fn get(&self, key: K) -> Option<V>;
fn get_expect(&self, key: K) -> V {
let bytes = key.into();
self.get(bytes.into()).unwrap_or_else(|| {
panic!("key {} is not found in the table '{}'", bytes.to_hex(), self.display(),)
})
}
fn insert_only(&mut self, key: K, val: V)
where K: Copy {
let bytes = key.into();
if let Some(v) = self.get(bytes.into()) {
let old = v.into();
let new = val.into();
if old != new {
panic!(
"failed to insert-only key {} which is already present in the table '{}' (old \
value={}, attempted new value={})",
bytes.to_hex(),
self.display(),
old.to_hex(),
new.to_hex()
)
}
return;
}
self.insert_or_update(key, val);
}
fn insert_or_update(&mut self, key: K, val: V);
fn update_only(&mut self, key: K, val: V)
where K: Copy {
let bytes = key.into();
if !self.contains_key(bytes.into()) {
panic!(
"failed to update non-existing key {} in the table '{}'",
self.display(),
bytes.to_hex()
);
}
self.insert_or_update(key, val);
}
}
pub trait TransactionalMap<K> {
fn commit_transaction(&mut self) -> Option<u64>;
fn abort_transaction(&mut self);
fn transaction_keys(&self, txno: u64) -> impl ExactSizeIterator<Item = K>;
fn transaction_count(&self) -> u64;
}