-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
task.rs
67 lines (59 loc) · 1.42 KB
/
task.rs
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
63
64
65
66
67
use crate::buffers::{Acker, EventStream};
use futures::{future::BoxFuture, FutureExt};
use pin_project::pin_project;
use std::{
fmt,
future::Future,
pin::Pin,
task::{Context, Poll},
};
pub enum TaskOutput {
Source,
Transform,
/// Buffer of sink
Sink(Pin<EventStream>, Acker),
Healthcheck,
}
/// High level topology task.
#[pin_project]
pub struct Task {
#[pin]
inner: BoxFuture<'static, Result<TaskOutput, ()>>,
name: String,
typetag: String,
}
impl Task {
pub fn new<S1, S2, Fut>(name: S1, typetag: S2, inner: Fut) -> Self
where
S1: Into<String>,
S2: Into<String>,
Fut: Future<Output = Result<TaskOutput, ()>> + Send + 'static,
{
Self {
inner: inner.boxed(),
name: name.into(),
typetag: typetag.into(),
}
}
pub fn name(&self) -> &str {
&self.name
}
pub fn typetag(&self) -> &str {
&self.typetag
}
}
impl Future for Task {
type Output = Result<TaskOutput, ()>;
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
let this: &mut Task = self.get_mut();
this.inner.as_mut().poll(cx)
}
}
impl fmt::Debug for Task {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Task")
.field("name", &self.name)
.field("typetag", &self.typetag)
.finish()
}
}