viz_core/handler/
boxed.rs

1use std::fmt;
2
3use super::cloneable::BoxCloneable;
4use crate::{Handler, Request, Response, Result};
5
6/// A [`Clone`] + [`Send`] boxed [`Handler`].
7pub struct BoxHandler<I = Request, O = Result<Response>>(BoxCloneable<I, O>);
8
9impl<I, O> BoxHandler<I, O> {
10    /// Creates a new `BoxHandler`.
11    pub fn new<H>(h: H) -> Self
12    where
13        H: Handler<I, Output = O> + Clone,
14    {
15        Self(Box::new(h))
16    }
17}
18
19impl<I, O> Clone for BoxHandler<I, O>
20where
21    I: Send + 'static,
22    O: 'static,
23{
24    fn clone(&self) -> Self {
25        Self(self.0.clone_box())
26    }
27}
28
29#[crate::async_trait]
30impl<I, O> Handler<I> for BoxHandler<I, O>
31where
32    I: Send + 'static,
33    O: 'static,
34{
35    type Output = O;
36
37    async fn call(&self, i: I) -> Self::Output {
38        self.0.call(i).await
39    }
40}
41
42impl<I, O> From<BoxCloneable<I, O>> for BoxHandler<I, O> {
43    fn from(value: BoxCloneable<I, O>) -> Self {
44        Self(value)
45    }
46}
47
48impl<I, O> fmt::Debug for BoxHandler<I, O> {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        f.debug_struct("BoxHandler").finish()
51    }
52}