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: add SubscriptionSink::pipe_from_try_stream to support streams that returns Result #720

Merged
merged 8 commits into from
Apr 1, 2022

Conversation

niklasad1
Copy link
Member

@niklasad1 niklasad1 commented Mar 30, 2022

The rationale for this is that it is more flexible for use cases when Stream<Item = Result<T, Error>>.

Take for example tokio_stream::Broadcast then one would have to something like:

   let stream = BroadcastStream::new(rx).take_while(|r| future::ready(r.is_ok())).filter_map(|r| future::ready(r.ok()));

Note, I realized that pipe_from_stream will work if Error: Serialize bound is satisfied but that's unlikely for the error types but I added a comment about that

The rationale for this is that it is more flexible for use cases when `Stream<Item = Result<T, Error>>`.

Take for example `tokio_stream::Broadcast` then one would have to something like:

```rust
   let stream = BroadcastStream::new(rx).take_while(|r| future::ready(r.is_ok())).filter_map(|r| future::ready(r.ok()));
```

Of course it's a bit awkward to return `Result` when the underlying stream can't fail but I think that's fair trade-off
here.
@niklasad1 niklasad1 requested a review from a team as a code owner March 30, 2022 10:26
Copy link
Member

@TarikGul TarikGul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall, lgtm 👍 .

Only nit is the failing gitlab test:

tokio::spawn(sink.pipe_from_stream(stream));
   |                       ---------------- ^^^^^^ the trait `StdError` is not implemented for `anyhow::Error`

core/src/server/rpc_module.rs Outdated Show resolved Hide resolved
core/src/server/rpc_module.rs Outdated Show resolved Hide resolved
assert_eq!(sub.next().await.unwrap().unwrap(), 1);
let exp = SubscriptionClosed::new(SubscriptionClosedReason::Server(err.to_string()));
// The server closed down the subscription with the underlying error from the stream.
assert!(matches!(sub.next().await, Some(Err(Error::SubscriptionClosed(close_reason))) if close_reason == exp));
Copy link
Collaborator

@jsdw jsdw Mar 31, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth just calling sub.next().await once more after this to confirm that it's None and that the thing definitely won't send any more?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, just found a bug let's tackle it another PR.

Copy link
Collaborator

@jsdw jsdw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! A couple of thoughts but nothing blocking; do as you wish with them :)

@niklasad1 niklasad1 changed the title refactor: make pipe_from_stream take stream of result refactor: add pipe_from_try_stream to support streams that returns Result Apr 1, 2022
@niklasad1 niklasad1 changed the title refactor: add pipe_from_try_stream to support streams that returns Result feat: add SubscriptionSink::pipe_from_try_stream to support streams that returns Result Apr 1, 2022
proc-macros/src/lib.rs Outdated Show resolved Hide resolved
@niklasad1 niklasad1 merged commit 545ceaf into master Apr 1, 2022
@niklasad1 niklasad1 deleted the na-resultify-pipe-from-stream branch April 1, 2022 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants