use bytes::Bytes;
use futures_core::stream::BoxStream;
use std::{
fmt,
sync::{
atomic::{AtomicUsize, Ordering},
Arc,
},
};
use crate::error::LocalInfileError;
pub mod builtin;
type BoxFuture<'a, T> = futures_core::future::BoxFuture<'a, Result<T, LocalInfileError>>;
pub type InfileData = BoxStream<'static, std::io::Result<Bytes>>;
pub trait GlobalHandler: Send + Sync + 'static {
fn handle(&self, file_name: &[u8]) -> BoxFuture<'static, InfileData>;
}
impl<T> GlobalHandler for T
where
T: for<'a> Fn(&'a [u8]) -> BoxFuture<'static, InfileData>,
T: Send + Sync + 'static,
{
fn handle(&self, file_name: &[u8]) -> BoxFuture<'static, InfileData> {
(self)(file_name)
}
}
static HANDLER_ID: AtomicUsize = AtomicUsize::new(0);
#[derive(Clone)]
pub struct GlobalHandlerObject(usize, Arc<dyn GlobalHandler>);
impl GlobalHandlerObject {
pub(crate) fn new<T: GlobalHandler>(handler: T) -> Self {
Self(HANDLER_ID.fetch_add(1, Ordering::SeqCst), Arc::new(handler))
}
pub(crate) fn clone_inner(&self) -> Arc<dyn GlobalHandler> {
self.1.clone()
}
}
impl PartialEq for GlobalHandlerObject {
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}
impl Eq for GlobalHandlerObject {}
impl fmt::Debug for GlobalHandlerObject {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("GlobalHandlerObject").field(&"..").finish()
}
}