1extern crate alloc;
2use crate::ProgressEntry;
3use alloc::boxed::Box;
4use bytes::Bytes;
5use core::fmt::Debug;
6
7pub trait Pusher: Send + 'static {
8 type Error: Send + Unpin + 'static;
9 fn push(&mut self, range: &ProgressEntry, content: Bytes) -> Result<(), (Self::Error, Bytes)>;
10 fn flush(&mut self) -> Result<(), Self::Error> {
11 Ok(())
12 }
13}
14
15pub trait AnyError: Debug + Send + Unpin + 'static {}
16impl<T: Debug + Send + Unpin + 'static> AnyError for T {}
17
18pub struct BoxPusher {
19 pub pusher: Box<dyn Pusher<Error = Box<dyn AnyError>>>,
20}
21impl Pusher for BoxPusher {
22 type Error = Box<dyn AnyError>;
23 fn push(&mut self, range: &ProgressEntry, content: Bytes) -> Result<(), (Self::Error, Bytes)> {
24 self.pusher.push(range, content)
25 }
26 fn flush(&mut self) -> Result<(), Self::Error> {
27 self.pusher.flush()
28 }
29}
30
31struct PusherAdapter<P: Pusher> {
32 inner: P,
33}
34impl<P: Pusher> Pusher for PusherAdapter<P>
35where
36 P::Error: Debug,
37{
38 type Error = Box<dyn AnyError>;
39 fn push(&mut self, range: &ProgressEntry, content: Bytes) -> Result<(), (Self::Error, Bytes)> {
40 self.inner
41 .push(range, content)
42 .map_err(|(e, b)| (Box::new(e) as Box<dyn AnyError>, b))
43 }
44 fn flush(&mut self) -> Result<(), Self::Error> {
45 self.inner
46 .flush()
47 .map_err(|e| Box::new(e) as Box<dyn AnyError>)
48 }
49}
50
51impl BoxPusher {
52 pub fn new<P: Pusher>(pusher: P) -> Self
53 where
54 P::Error: Debug,
55 {
56 Self {
57 pusher: Box::new(PusherAdapter { inner: pusher }),
58 }
59 }
60}