use std::future::Future;
use super::maybe_send::{MaybeSend, MaybeSync};
use crate::bmt::DEFAULT_BODY_SIZE;
use crate::chunk::{AnyChunk, ChunkAddress};
pub trait ChunkGet<const BODY_SIZE: usize = DEFAULT_BODY_SIZE>: MaybeSend + MaybeSync {
type Error: std::error::Error + Send + Sync + 'static;
fn get(
&self,
address: &ChunkAddress,
) -> impl Future<Output = Result<AnyChunk<BODY_SIZE>, Self::Error>> + MaybeSend;
}
impl<T: ChunkGet<BS> + ?Sized, const BS: usize> ChunkGet<BS> for &T {
type Error = T::Error;
fn get(
&self,
address: &ChunkAddress,
) -> impl Future<Output = Result<AnyChunk<BS>, Self::Error>> + MaybeSend {
(**self).get(address)
}
}
pub trait ChunkHas<const BODY_SIZE: usize = DEFAULT_BODY_SIZE>: MaybeSend + MaybeSync {
fn has(&self, address: &ChunkAddress) -> impl Future<Output = bool> + MaybeSend;
}
impl<T: ChunkHas<BS> + ?Sized, const BS: usize> ChunkHas<BS> for &T {
fn has(&self, address: &ChunkAddress) -> impl Future<Output = bool> + MaybeSend {
(**self).has(address)
}
}
pub trait ChunkPut<const BODY_SIZE: usize = DEFAULT_BODY_SIZE>: MaybeSend + MaybeSync {
type Error: std::error::Error + Send + Sync + 'static;
fn put(
&self,
chunk: AnyChunk<BODY_SIZE>,
) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend;
}
impl<T: ChunkPut<BS> + ?Sized, const BS: usize> ChunkPut<BS> for &T {
type Error = T::Error;
fn put(
&self,
chunk: AnyChunk<BS>,
) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend {
(**self).put(chunk)
}
}
#[cfg(target_arch = "wasm32")]
mod wasm_send_sync_proof {
use super::*;
struct NotSendSync(core::marker::PhantomData<*const ()>);
impl<const BS: usize> ChunkGet<BS> for NotSendSync {
type Error = std::io::Error;
async fn get(&self, _addr: &ChunkAddress) -> Result<AnyChunk<BS>, Self::Error> {
unreachable!()
}
}
fn _assert<const BS: usize, S: ChunkGet<BS>>() {}
#[allow(dead_code)]
fn _proof() {
_assert::<{ DEFAULT_BODY_SIZE }, NotSendSync>()
}
}