Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hydro_lang): make Stream::cloned work with borrowed elements #1678

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading