zrx_stream/stream/combinator/set/convert.rs
1// Copyright (c) 2025-2026 Zensical and contributors
2
3// SPDX-License-Identifier: MIT
4// All contributions are certified under the DCO
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 set conversions.
27
28use super::{Stream, StreamSet};
29
30// ----------------------------------------------------------------------------
31// Traits
32// ----------------------------------------------------------------------------
33
34/// Conversion into [`StreamSet`].
35pub trait IntoStreamSet<I, T> {
36 /// Converts an iterator of stream references into a stream set.
37 ///
38 /// # Examples
39 ///
40 /// ```
41 /// use zrx_stream::combinator::IntoStreamSet;
42 /// use zrx_stream::workspace::Workspace;
43 ///
44 /// // Create workspace and workflow
45 /// let workspace = Workspace::<&str>::new();
46 /// let workflow = workspace.add_workflow();
47 ///
48 /// // Create streams (homogeneous)
49 /// let a = workflow.add_source::<i32>();
50 /// let b = workflow.add_source::<i32>();
51 ///
52 /// // Create stream set
53 /// let set = [&a, &b].into_stream_set();
54 /// ```
55 fn into_stream_set(self) -> StreamSet<I, T>;
56}
57
58// ----------------------------------------------------------------------------
59// Trait implementations
60// ----------------------------------------------------------------------------
61
62impl<I, T> IntoStreamSet<I, T> for &Stream<I, T> {
63 /// Converts a stream reference into a stream set.
64 ///
65 /// Albeit this conversion is trivial, it allows to pass stream references
66 /// to functions that expect sets, which can be quite convenient.
67 ///
68 /// # Examples
69 ///
70 /// ```
71 /// use zrx_stream::combinator::IntoStreamSet;
72 /// use zrx_stream::workspace::Workspace;
73 ///
74 /// // Create workspace and workflow
75 /// let workspace = Workspace::<&str>::new();
76 /// let workflow = workspace.add_workflow();
77 ///
78 /// // Create stream
79 /// let stream = workflow.add_source::<i32>();
80 ///
81 /// // Create stream set
82 /// let set = stream.into_stream_set();
83 /// ```
84 #[inline]
85 fn into_stream_set(self) -> StreamSet<I, T> {
86 StreamSet::from_iter([self.clone()])
87 }
88}
89
90// ----------------------------------------------------------------------------
91// Blanket implementations
92// ----------------------------------------------------------------------------
93
94impl<'a, I, T, S> IntoStreamSet<I, T> for S
95where
96 S: IntoIterator<Item = &'a Stream<I, T>>,
97 Stream<I, T>: 'a,
98{
99 #[inline]
100 fn into_stream_set(self) -> StreamSet<I, T> {
101 self.into_iter().cloned().collect()
102 }
103}