use std::future::Future;
use crate::bmt::DEFAULT_BODY_SIZE;
use crate::chunk::{AnyChunk, ChunkAddress};
pub trait SyncChunkPut<const BODY_SIZE: usize = DEFAULT_BODY_SIZE> {
type Error: std::error::Error + Send + Sync + 'static;
fn put(&self, chunk: AnyChunk<BODY_SIZE>) -> Result<(), Self::Error>;
}
pub trait SyncChunkGet<const BODY_SIZE: usize = DEFAULT_BODY_SIZE> {
type Error: std::error::Error + Send + Sync + 'static;
fn get(&self, address: &ChunkAddress) -> Result<AnyChunk<BODY_SIZE>, Self::Error>;
}
pub trait SyncChunkHas<const BODY_SIZE: usize = DEFAULT_BODY_SIZE> {
fn has(&self, address: &ChunkAddress) -> bool;
}
impl<T: SyncChunkPut<BS>, const BS: usize> SyncChunkPut<BS> for &T {
type Error = T::Error;
fn put(&self, chunk: AnyChunk<BS>) -> Result<(), Self::Error> {
(**self).put(chunk)
}
}
impl<T: SyncChunkPut<BS>, const BS: usize> SyncChunkPut<BS> for &mut T {
type Error = T::Error;
fn put(&self, chunk: AnyChunk<BS>) -> Result<(), Self::Error> {
(**self).put(chunk)
}
}
impl<T: SyncChunkGet<BS>, const BS: usize> SyncChunkGet<BS> for &T {
type Error = T::Error;
fn get(&self, address: &ChunkAddress) -> Result<AnyChunk<BS>, Self::Error> {
(**self).get(address)
}
}
impl<T: SyncChunkGet<BS>, const BS: usize> SyncChunkGet<BS> for &mut T {
type Error = T::Error;
fn get(&self, address: &ChunkAddress) -> Result<AnyChunk<BS>, Self::Error> {
(**self).get(address)
}
}
impl<T: SyncChunkHas<BS>, const BS: usize> SyncChunkHas<BS> for &T {
fn has(&self, address: &ChunkAddress) -> bool {
(**self).has(address)
}
}
impl<T: SyncChunkHas<BS>, const BS: usize> SyncChunkHas<BS> for &mut T {
fn has(&self, address: &ChunkAddress) -> bool {
(**self).has(address)
}
}
pub trait ChunkGet<const BODY_SIZE: usize = DEFAULT_BODY_SIZE>: Send + Sync {
type Error: std::error::Error + Send + Sync + 'static;
fn get(
&self,
address: &ChunkAddress,
) -> impl Future<Output = Result<AnyChunk<BODY_SIZE>, Self::Error>> + Send;
}
impl<T, const BS: usize> ChunkGet<BS> for T
where
T: SyncChunkGet<BS> + Send + Sync,
{
type Error = T::Error;
async fn get(&self, address: &ChunkAddress) -> Result<AnyChunk<BS>, Self::Error> {
SyncChunkGet::get(self, address)
}
}
pub trait ChunkHas<const BODY_SIZE: usize = DEFAULT_BODY_SIZE>: Send + Sync {
fn has(&self, address: &ChunkAddress) -> impl Future<Output = bool> + Send;
}
impl<T, const BS: usize> ChunkHas<BS> for T
where
T: SyncChunkHas<BS> + Send + Sync,
{
async fn has(&self, address: &ChunkAddress) -> bool {
SyncChunkHas::has(self, address)
}
}
pub trait ChunkPut<const BODY_SIZE: usize = DEFAULT_BODY_SIZE>: Send + Sync {
type Error: std::error::Error + Send + Sync + 'static;
fn put(
&self,
chunk: AnyChunk<BODY_SIZE>,
) -> impl Future<Output = Result<(), Self::Error>> + Send;
}
impl<T, const BS: usize> ChunkPut<BS> for T
where
T: SyncChunkPut<BS> + Send + Sync,
{
type Error = T::Error;
async fn put(&self, chunk: AnyChunk<BS>) -> Result<(), Self::Error> {
SyncChunkPut::put(self, chunk)
}
}