hermes_async_runtime_components/stream/traits/
boxed.rs

1use alloc::boxed::Box;
2use core::pin::Pin;
3
4use cgp::prelude::*;
5use futures_core::stream::Stream;
6use hermes_runtime_components::traits::stream::{
7    HasStreamType, ProvideStreamType, StreamTypeComponent,
8};
9
10pub trait HasBoxedStreamType: HasStreamType {
11    fn to_boxed_stream<Item>(
12        stream: Self::Stream<Item>,
13    ) -> Pin<Box<dyn Stream<Item = Item> + Send + Sync + 'static>>
14    where
15        Item: Async;
16
17    fn from_boxed_stream<Item>(
18        stream: Pin<Box<dyn Stream<Item = Item> + Send + Sync + 'static>>,
19    ) -> Self::Stream<Item>
20    where
21        Item: Async;
22}
23
24impl<Runtime, Components> HasBoxedStreamType for Runtime
25where
26    Runtime: Async + HasComponents<Components = Components>,
27    Components: BoxedStreamTypeProvider<Runtime>,
28{
29    fn to_boxed_stream<Item>(
30        stream: Self::Stream<Item>,
31    ) -> Pin<Box<dyn Stream<Item = Item> + Send + Sync + 'static>>
32    where
33        Item: Async,
34    {
35        Components::to_boxed_stream(stream)
36    }
37
38    fn from_boxed_stream<Item>(
39        stream: Pin<Box<dyn Stream<Item = Item> + Send + Sync + 'static>>,
40    ) -> Self::Stream<Item>
41    where
42        Item: Async,
43    {
44        Components::from_boxed_stream(stream)
45    }
46}
47
48pub trait BoxedStreamTypeProvider<Runtime>: ProvideStreamType<Runtime>
49where
50    Runtime: Async,
51{
52    fn to_boxed_stream<Item>(
53        stream: Self::Stream<Item>,
54    ) -> Pin<Box<dyn Stream<Item = Item> + Send + Sync + 'static>>
55    where
56        Item: Async;
57
58    fn from_boxed_stream<Item>(
59        stream: Pin<Box<dyn Stream<Item = Item> + Send + Sync + 'static>>,
60    ) -> Self::Stream<Item>
61    where
62        Item: Async;
63}
64
65impl<Runtime, Component, Delegate> BoxedStreamTypeProvider<Runtime> for Component
66where
67    Runtime: Async,
68    Component: DelegateComponent<StreamTypeComponent, Delegate = Delegate>,
69    Delegate: BoxedStreamTypeProvider<Runtime>,
70{
71    fn to_boxed_stream<Item>(
72        stream: Self::Stream<Item>,
73    ) -> Pin<Box<dyn Stream<Item = Item> + Send + Sync + 'static>>
74    where
75        Item: Async,
76    {
77        Delegate::to_boxed_stream(stream)
78    }
79
80    fn from_boxed_stream<Item>(
81        stream: Pin<Box<dyn Stream<Item = Item> + Send + Sync + 'static>>,
82    ) -> Self::Stream<Item>
83    where
84        Item: Async,
85    {
86        Delegate::from_boxed_stream(stream)
87    }
88}