zrx_stream/stream.rs
1// Copyright (c) Zensical LLC <https://zensical.org>
2
3// SPDX-License-Identifier: MIT
4// Third-party contributions licensed under CLA
5
6// Permission is hereby granted, free of charge, to any person obtaining a copy
7// of this software and associated documentation files (the "Software"), to
8// deal in the Software without restriction, including without limitation the
9// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10// sell copies of the Software, and to permit persons to whom the Software is
11// furnished to do so, subject to the following conditions:
12
13// The above copyright notice and this permission notice shall be included in
14// all copies or substantial portions of the Software.
15
16// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22// IN THE SOFTWARE.
23
24// ----------------------------------------------------------------------------
25
26//! Stream.
27
28use std::marker::PhantomData;
29use zrx_scheduler::{Id, Value};
30
31pub mod barrier;
32pub mod combinator;
33pub mod function;
34pub mod operator;
35pub mod value;
36pub mod workspace;
37
38use function::InspectFn as ForEachFn;
39use workspace::Workflow;
40
41// ----------------------------------------------------------------------------
42// Structs
43// ----------------------------------------------------------------------------
44
45/// Stream.
46#[derive(Debug)]
47pub struct Stream<I, T> {
48 /// Identifier.
49 id: usize,
50 /// Associated workflow.
51 workflow: Workflow<I>,
52 /// Type marker.
53 marker: PhantomData<T>,
54}
55
56// ----------------------------------------------------------------------------
57// Implementations
58// ----------------------------------------------------------------------------
59
60impl<I, T> Stream<I, T>
61where
62 I: Id,
63 T: Value + Clone,
64{
65 pub fn for_each<F>(&self, f: F)
66 where
67 F: ForEachFn<I, T> + Clone,
68 {
69 self.inspect(f);
70 }
71}
72
73// ----------------------------------------------------------------------------
74// Trait implementations
75// ----------------------------------------------------------------------------
76
77impl<I, T> Clone for Stream<I, T> {
78 /// Clones the stream.
79 #[inline]
80 fn clone(&self) -> Self {
81 let workflow = self.workflow.clone();
82 Self { workflow, ..*self }
83 }
84}
85
86// ----------------------------------------------------------------------------
87
88impl<I, T> PartialEq for Stream<I, T> {
89 /// Compares two streams for equality.
90 #[inline]
91 fn eq(&self, other: &Self) -> bool {
92 self.id == other.id
93 }
94}
95
96impl<I, T> Eq for Stream<I, T> {}