use crate::on_early_drop::service::OnEarlyDropService;
use tower_layer::Layer;
#[derive(Clone, Copy)]
pub struct OnEarlyDropLayer<OFD, OBD> {
on_future_drop: OFD,
on_body_drop: OBD,
}
impl<OFD, OBD> std::fmt::Debug for OnEarlyDropLayer<OFD, OBD> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("OnEarlyDropLayer")
.field("on_future_drop", &format_args!(".."))
.field("on_body_drop", &format_args!(".."))
.finish()
}
}
impl OnEarlyDropLayer<(), ()> {
pub fn builder() -> Self {
Self {
on_future_drop: (),
on_body_drop: (),
}
}
}
impl<H: Clone> OnEarlyDropLayer<H, H> {
pub fn new(hook: H) -> Self {
Self {
on_future_drop: hook.clone(),
on_body_drop: hook,
}
}
}
impl<OFD, OBD> OnEarlyDropLayer<OFD, OBD> {
pub fn on_future_drop<T>(self, hook: T) -> OnEarlyDropLayer<T, OBD> {
OnEarlyDropLayer {
on_future_drop: hook,
on_body_drop: self.on_body_drop,
}
}
pub fn on_body_drop<T>(self, hook: T) -> OnEarlyDropLayer<OFD, T> {
OnEarlyDropLayer {
on_future_drop: self.on_future_drop,
on_body_drop: hook,
}
}
}
impl<S, OFD, OBD> Layer<S> for OnEarlyDropLayer<OFD, OBD>
where
OFD: Clone,
OBD: Clone,
{
type Service = OnEarlyDropService<S, OFD, OBD>;
fn layer(&self, inner: S) -> Self::Service {
OnEarlyDropService::new(
inner,
self.on_future_drop.clone(),
self.on_body_drop.clone(),
)
}
}