use crate::progress::{Source, Target, Timestamp};
use crate::communication::Push;
use crate::dataflow::Scope;
use crate::dataflow::channels::pushers::tee::TeeHelper;
use crate::dataflow::channels::Message;
use std::fmt::{self, Debug};
pub struct Stream<'scope, T: Timestamp, C> {
name: Source,
scope: Scope<'scope, T>,
ports: TeeHelper<T, C>,
}
impl<'scope, T: Timestamp, C: Clone+'static> Clone for Stream<'scope, T, C> {
fn clone(&self) -> Self {
Self {
name: self.name,
scope: self.scope,
ports: self.ports.clone(),
}
}
fn clone_from(&mut self, source: &Self) {
self.name.clone_from(&source.name);
self.scope.clone_from(&source.scope);
self.ports.clone_from(&source.ports);
}
}
pub type StreamVec<'scope, T, D> = Stream<'scope, T, Vec<D>>;
impl<'scope, T: Timestamp, C> Stream<'scope, T, C> {
pub fn connect_to<P: Push<Message<T, C>>+'static>(self, target: Target, pusher: P, identifier: usize) where C: 'static {
let mut logging: Option<crate::logging::TimelyLogger> = self.scope().worker().logging();
logging.as_mut().map(|l| l.log(crate::logging::ChannelsEvent {
id: identifier,
scope_addr: self.scope.addr().to_vec(),
source: (self.name.node, self.name.port),
target: (target.node, target.port),
typ: std::any::type_name::<C>().to_string(),
}));
self.scope.add_edge(self.name, target);
self.ports.add_pusher(pusher);
}
pub fn new(source: Source, output: TeeHelper<T, C>, scope: Scope<'scope, T>) -> Self {
Self { name: source, ports: output, scope }
}
pub fn name(&self) -> &Source { &self.name }
pub fn scope(&self) -> Scope<'scope, T> { self.scope }
pub fn container<C2>(self) -> Stream<'scope, T, C2> where Self: AsStream<'scope, T, C2> { self.as_stream() }
}
pub trait AsStream<'scope, T: Timestamp, C> {
fn as_stream(self) -> Stream<'scope, T, C>;
}
impl<'scope, T: Timestamp, C> AsStream<'scope, T, C> for Stream<'scope, T, C> {
fn as_stream(self) -> Self { self }
}
impl<'scope, T: Timestamp, C> Debug for Stream<'scope, T, C> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Stream")
.field("source", &self.name)
.finish_non_exhaustive()
}
}
#[cfg(test)]
mod tests {
use crate::dataflow::channels::pact::Pipeline;
use crate::dataflow::operators::{Operator, ToStream};
#[derive(Debug, Eq, PartialEq)]
struct NotClone;
#[test]
fn test_non_clone_stream() {
crate::example(|scope| {
let _ = [NotClone]
.to_stream(scope)
.container::<Vec<_>>()
.sink(Pipeline, "check non-clone", |(input, _frontier)| {
input.for_each(|_cap, data| {
for datum in data.drain(..) {
assert_eq!(datum, NotClone);
}
});
});
});
}
}