zrx_stream/stream/operator/transpose.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//! Transpose operator.
27
28use std::marker::PhantomData;
29
30use zrx_scheduler::action::descriptor::Property;
31use zrx_scheduler::action::output::IntoOutputs;
32use zrx_scheduler::action::Descriptor;
33use zrx_scheduler::effect::{Item, Task};
34use zrx_scheduler::{Id, Value};
35
36use crate::stream::value::Delta;
37use crate::stream::Stream;
38
39use super::{Operator, OperatorExt};
40
41// ----------------------------------------------------------------------------
42// Structs
43// ----------------------------------------------------------------------------
44
45/// Transpose operator.
46struct Transpose<T> {
47 /// Capture types.
48 marker: PhantomData<T>,
49}
50
51// ----------------------------------------------------------------------------
52// Implementations
53// ----------------------------------------------------------------------------
54
55impl<I, T> Stream<I, Delta<I, T>>
56where
57 I: Id,
58 T: Value + Clone,
59{
60 pub fn transpose(&self) -> Stream<I, Delta<I, T>> {
61 self.with_operator(Transpose { marker: PhantomData })
62 }
63}
64
65// ----------------------------------------------------------------------------
66// Trait implementations
67// ----------------------------------------------------------------------------
68
69impl<I, T> Operator<I, Delta<I, T>> for Transpose<T>
70where
71 I: Id,
72 T: Value + Clone,
73{
74 type Item<'a> = Item<&'a I, &'a Delta<I, T>>;
75
76 /// Handles the given item.
77 ///
78 /// Transposition is implemented as a swap of the identifiers of a delta of
79 /// items, by hoisting the inner items to the outer level, and wrapping the
80 /// associated data of the inner items with the outer item. This allows to
81 /// effectively invert relationships, adhering to differential semantics.
82 #[cfg_attr(
83 feature = "tracing",
84 tracing::instrument(skip_all, fields(id = %item.id))
85 )]
86 fn handle(&mut self, item: Self::Item<'_>) -> impl IntoOutputs<I> {
87 let item = item.into_owned();
88 Task::new(move || {
89 let iter = item.data.into_iter().map(|part| {
90 let inner = Item::new(item.id.clone(), part.data);
91 Item {
92 id: part.id,
93 data: Some(Delta::from([inner])),
94 }
95 });
96
97 // Return delta of items
98 iter.collect::<Vec<_>>()
99 })
100 }
101
102 /// Returns the descriptor.
103 #[inline]
104 fn descriptor(&self) -> Descriptor {
105 Descriptor::builder() // fmt
106 .property(Property::Pure)
107 .build()
108 }
109}