use cid::Cid;
use fvm_ipld_blockstore::Blockstore;
use crate::v14::{ActorError, Config, Map2, MapKey};
pub struct Set<BS, K>(Map2<BS, K, ()>)
where
BS: Blockstore,
K: MapKey;
impl<BS, K> Set<BS, K>
where
BS: Blockstore,
K: MapKey,
{
pub fn empty(bs: BS, config: Config, name: &'static str) -> Self {
Self(Map2::empty(bs, config, name))
}
pub fn load(
bs: BS,
root: &Cid,
config: Config,
name: &'static str,
) -> Result<Self, ActorError> {
Ok(Self(Map2::load(bs, root, config, name)?))
}
#[inline]
pub fn flush(&mut self) -> Result<Cid, ActorError> {
self.0.flush()
}
#[inline]
pub fn put(&mut self, key: &K) -> Result<Option<()>, ActorError> {
self.0.set(key, ())
}
#[inline]
pub fn has(&self, key: &K) -> Result<bool, ActorError> {
self.0.contains_key(key)
}
#[inline]
pub fn delete(&mut self, key: &K) -> Result<Option<()>, ActorError> {
self.0.delete(key)
}
pub fn for_each<F>(&self, mut f: F) -> Result<(), ActorError>
where
F: FnMut(K) -> Result<(), ActorError>,
{
self.0.for_each(|s, _| f(s))
}
pub fn collect_keys(&self) -> Result<Vec<K>, ActorError> {
let mut ret_keys = Vec::new();
self.for_each(|k| {
ret_keys.push(k);
Ok(())
})?;
Ok(ret_keys)
}
}