1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use std::io::SeekFrom;
use tokio::io::{AsyncRead, AsyncSeek, AsyncSeekExt};
use crate::container::ContainerMap;
use crate::handle::StopHandler;
use crate::log::{stream, HandleFactory, Sender};
pub struct Handle<H, F> {
handle: H,
handle_factory: F,
}
impl<H, F> std::fmt::Debug for Handle<H, F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ContainerHandle").finish()
}
}
impl<H: StopHandler, F> Handle<H, F> {
pub fn new(handle: H, handle_factory: F) -> Self {
Self {
handle,
handle_factory,
}
}
pub async fn stop(&mut self) -> anyhow::Result<()> {
self.handle.stop().await
}
pub(crate) async fn output<R>(&mut self, sender: Sender) -> anyhow::Result<()>
where
R: AsyncRead + AsyncSeek + Unpin + Send + 'static,
F: HandleFactory<R>,
{
let mut handle = self.handle_factory.new_handle();
handle.seek(SeekFrom::Start(0)).await?;
tokio::spawn(stream(handle, sender));
Ok(())
}
pub async fn wait(&mut self) -> anyhow::Result<()> {
self.handle.wait().await
}
}
pub type HandleMap<H, F> = ContainerMap<Handle<H, F>>;