From 10569208aa5d2380d493c3ec2da7e810f287990e Mon Sep 17 00:00:00 2001 From: Shadaj Laddad Date: Tue, 28 Jan 2025 17:43:52 -0800 Subject: [PATCH] feat(hydro_lang): make `Stream::cloned` work with borrowed elements `Stream::cloned` is only really useful if the input has `&T` elements but we want `T`, so this updates the signature to do that. --- hydro_lang/src/stream.rs | 46 +++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/hydro_lang/src/stream.rs b/hydro_lang/src/stream.rs index db14cfe40c5..6ea29059329 100644 --- a/hydro_lang/src/stream.rs +++ b/hydro_lang/src/stream.rs @@ -252,28 +252,6 @@ impl<'a, T, L: Location<'a>, B, Order> Stream { ) } - /// Clone each element of the stream; akin to `map(q!(|d| d.clone()))`. - /// - /// # Example - /// ```rust - /// # use hydro_lang::*; - /// # use dfir_rs::futures::StreamExt; - /// # tokio_test::block_on(test_util::stream_transform_test(|process| { - /// process.source_iter(q!(vec![1..3])).cloned() - /// # }, |mut stream| async move { - /// // 1, 2, 3 - /// # for w in vec![1..3] { - /// # assert_eq!(stream.next().await.unwrap(), w); - /// # } - /// # })); - /// ``` - pub fn cloned(self) -> Stream - where - T: Clone, - { - self.map(q!(|d| d.clone())) - } - /// For each item `i` in the input stream, transform `i` using `f` and then treat the /// result as an [`Iterator`] to produce items one by one. The implementation for [`Iterator`] /// for the output type `U` must produce items in a **deterministic** order. @@ -614,6 +592,30 @@ impl<'a, T, L: Location<'a>, B, Order> Stream { } } +impl<'a, T, L: Location<'a>, B, Order> Stream<&T, L, B, Order> { + /// Clone each element of the stream; akin to `map(q!(|d| d.clone()))`. + /// + /// # Example + /// ```rust + /// # use hydro_lang::*; + /// # use dfir_rs::futures::StreamExt; + /// # tokio_test::block_on(test_util::stream_transform_test(|process| { + /// process.source_iter(q!(&[1, 2, 3])).cloned() + /// # }, |mut stream| async move { + /// // 1, 2, 3 + /// # for w in vec![1, 2, 3] { + /// # assert_eq!(stream.next().await.unwrap(), w); + /// # } + /// # })); + /// ``` + pub fn cloned(self) -> Stream + where + T: Clone, + { + self.map(q!(|d| d.clone())) + } +} + impl<'a, T, L: Location<'a>, B, Order> Stream where Order: MinOrder,