Skip to content

Commit

Permalink
feat(hydro_lang): make Stream::cloned work with borrowed elements
Browse files Browse the repository at this point in the history
`Stream::cloned` is only really useful if the input has `&T` elements but we want `T`, so this updates the signature to do that.
  • Loading branch information
shadaj committed Jan 29, 2025
1 parent 316d700 commit 1056920
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions hydro_lang/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,28 +252,6 @@ 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!(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<T, L, B, Order>
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.
Expand Down Expand Up @@ -614,6 +592,30 @@ impl<'a, T, L: Location<'a>, B, Order> Stream<T, L, B, Order> {
}
}

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<T, L, B, Order>
where
T: Clone,
{
self.map(q!(|d| d.clone()))
}
}

impl<'a, T, L: Location<'a>, B, Order> Stream<T, L, B, Order>
where
Order: MinOrder<NoOrder, Min = NoOrder>,
Expand Down

0 comments on commit 1056920

Please sign in to comment.