tuwunel_core/utils/stream/cloned.rs
1use futures::{Stream, StreamExt, stream::Map};
2
3/// Clones items yielded by a stream of shared references.
4///
5/// Each referenced value is cloned only when its stream item is polled. Item
6/// order and readiness follow the source stream unchanged.
7pub trait Cloned<'a, T, S>
8where
9 S: Stream<Item = &'a T>,
10 T: Clone + 'a,
11{
12 /// Returns a stream of owned clones from the borrowed items.
13 ///
14 /// The adapter uses [`Clone::clone`] for each yielded reference. It
15 /// performs no eager collection or buffering.
16 fn cloned(self) -> Map<S, fn(&T) -> T>;
17}
18
19impl<'a, T, S> Cloned<'a, T, S> for S
20where
21 S: Stream<Item = &'a T>,
22 T: Clone + 'a,
23{
24 #[inline]
25 fn cloned(self) -> Map<S, fn(&T) -> T> { self.map(Clone::clone) }
26}