#[async_trait::async_trait(?Send)]
pub trait AsyncStorage {
async fn get(&self, cid: &Cid) -> Result<Block<DefaultParams>, StorageError>;
async fn set(&mut self, block: Block<DefaultParams>) -> Result<(), StorageError>;
async fn remove(&mut self, cid: &Cid) -> Result<(), StorageError>;
}
struct AsyncStorageWrapper {
next: Box<dyn Storage>,
}
#[async_trait::async_trait(?Send)]
impl AsyncStorage for AsyncStorageWrapper {
async fn get(&self, cid: &Cid) -> Result<Block<DefaultParams>, StorageError> {
self.next.get(cid)
}
async fn set(&mut self, block: Block<DefaultParams>) -> Result<(), StorageError> {
self.next.set(block)
}
async fn remove(&mut self, cid: &Cid) -> Result<(), StorageError> {
self.next.remove(cid)
}
}
struct StorageWrapper {
runtime: tokio::runtime::Runtime,
next: Box<dyn AsyncStorage>,
}
impl Storage for StorageWrapper {
fn get(&self, cid: &Cid) -> Result<Block<DefaultParams>, StorageError> {
self.runtime.block_on(self.next.get(cid))?
}
fn set(&mut self, block: Block<DefaultParams>) -> Result<(), StorageError> {
self.runtime.block_on(self.next.set(block))?
}
fn remove(&mut self, cid: &Cid) -> Result<(), StorageError> {
self.runtime.block_on(self.next.remove(cid))?
}
}