use topcoat_core::context::Cx;
use crate::{Body, Layer, LayerFuture, Next, Path, PathBuf};
#[doc(hidden)]
pub trait ModuleLayer: Send + Sync + 'static {
fn module_path(&self) -> &'static str;
fn relative_path(&self) -> Option<&Path> {
None
}
fn handle<'a>(&'a self, cx: &'a Cx, body: Body, next: Next<'a>) -> LayerFuture<'a>;
}
impl<L: ModuleLayer + ?Sized> ModuleLayer for &'static L {
fn module_path(&self) -> &'static str {
(**self).module_path()
}
fn relative_path(&self) -> Option<&Path> {
(**self).relative_path()
}
fn handle<'a>(&'a self, cx: &'a Cx, body: Body, next: Next<'a>) -> LayerFuture<'a> {
(**self).handle(cx, body, next)
}
}
#[cfg(feature = "discover")]
inventory::collect!(&'static dyn ModuleLayer);
pub(super) struct ResolvedLayer<L> {
layer: L,
path: PathBuf,
}
impl<L: ModuleLayer> ResolvedLayer<L> {
pub(super) fn new(layer: L, path: PathBuf) -> Self {
Self { layer, path }
}
}
impl<L: ModuleLayer> Layer for ResolvedLayer<L> {
fn path(&self) -> Option<&Path> {
Some(&self.path)
}
fn handle<'a>(&'a self, cx: &'a Cx, body: Body, next: Next<'a>) -> LayerFuture<'a> {
self.layer.handle(cx, body, next)
}
}