Skip to main content

zrx_stream/stream/combinator/tuple/
cons.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 tuple construction.
27
28use crate::stream::combinator::StreamTuple;
29use crate::stream::Stream;
30
31mod convert;
32
33pub use convert::IntoStreamTupleCons;
34
35// ----------------------------------------------------------------------------
36// Traits
37// ----------------------------------------------------------------------------
38
39/// Stream tuple construction.
40///
41/// This trait is used to prepend streams to existings stream tuples, which is
42/// necessary to implement stream methods that take a tuple of streams, as they
43/// need to be combined with the current stream (i.e. `self`) to construct a
44/// stream tuple for further consumption.
45pub trait StreamTupleCons<I, T> {
46    /// Output type of construction.
47    type Output: StreamTuple<I>;
48
49    /// Prepends a stream to the stream tuple.
50    ///
51    /// This method is used to prepend a stream to the tuple and consume it,
52    /// constructing a new stream tuple that includes the given stream.
53    fn cons(head: Stream<I, T>, tail: Self) -> Self::Output;
54}
55
56// ----------------------------------------------------------------------------
57// Macros
58// ----------------------------------------------------------------------------
59
60/// Implements stream tuple construction trait.
61macro_rules! impl_stream_tuple_cons {
62    ($T1:ident $(, $T:ident)* $(,)?) => {
63        impl<I, $T1, $($T,)*> StreamTupleCons<I, $T1> for ($(Stream<I, $T>,)*) {
64            type Output = (Stream<I, $T1>, $(Stream<I, $T>,)*);
65
66            #[inline]
67            fn cons(head: Stream<I, $T1>, tail: Self) -> Self::Output {
68                #[allow(non_snake_case)]
69                let ($($T,)*) = tail;
70                (head, $($T,)*)
71            }
72        }
73    };
74}
75
76// ----------------------------------------------------------------------------
77
78impl_stream_tuple_cons!(T1);
79impl_stream_tuple_cons!(T1, T2);
80impl_stream_tuple_cons!(T1, T2, T3);
81impl_stream_tuple_cons!(T1, T2, T3, T4);
82impl_stream_tuple_cons!(T1, T2, T3, T4, T5);
83impl_stream_tuple_cons!(T1, T2, T3, T4, T5, T6);
84impl_stream_tuple_cons!(T1, T2, T3, T4, T5, T6, T7);
85impl_stream_tuple_cons!(T1, T2, T3, T4, T5, T6, T7, T8);