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

Add poll_recv method to Receiver #56

Merged
merged 6 commits into from
Mar 4, 2024
Merged

Add poll_recv method to Receiver #56

merged 6 commits into from
Mar 4, 2024

Conversation

jsdw
Copy link
Contributor

@jsdw jsdw commented Feb 29, 2024

This PR Adds a poll_recv() method to the Receiver type. It returns the same Result<T,RecvError> type that the receiver.recv() future returns (hence the name).

This method can be used when defining custom streams that internally make use of async_broadcast and want to know about whether the async_broadcast stream has overflowed or not.

src/lib.rs Show resolved Hide resolved
Copy link
Member

@zeenix zeenix left a comment

Choose a reason for hiding this comment

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

LGTM otherwise.

src/lib.rs Outdated Show resolved Hide resolved
src/lib.rs Show resolved Hide resolved
@jsdw jsdw marked this pull request as ready for review February 29, 2024 15:02
@jsdw jsdw requested a review from zeenix February 29, 2024 15:02
@jsdw
Copy link
Contributor Author

jsdw commented Feb 29, 2024

I added an example to poll_recv(), and added an OverflowError so that poll_recv() could be more precise rather than returning a RecvError.

@zeenix what do you reckon? :)

This reminds me that it would be good to document possible errors from this method in a Errors section (as per the conventions).

I wasn't sure what you meant here offhand (I can't see any such sections in the repo)

@zeenix
Copy link
Member

zeenix commented Feb 29, 2024

I added an example to poll_recv()

Awesome!

and added an OverflowError so that poll_recv() could be more precise rather than returning a RecvError.

Is it worth it? Do we envision any other information going in it other than the count? If not, I'd rather keep things simple and just return the integer. If we need an error type, then we just go with option#1.

This reminds me that it would be good to document possible errors from this method in a Errors section (as per the conventions).

I wasn't sure what you meant here offhand (I can't see any such sections in the repo)

I meant adding a # Error heading in the docs of the method and listing all possible errors it can throw.

@jsdw
Copy link
Contributor Author

jsdw commented Feb 29, 2024

Is it worth it? Do we envision any other information going in it other than the count? If not, I'd rather keep things simple and just return the integer. If we need an error type, then we just go with option#1.

My arguments for keeping OverflowError would be:

  • The name of the type makes it clear what it is about (ie an error caused because of overflow)
  • It acts like the other errors returned in Results throughout this library, ie it implements Error and Display and can be handled accordingly by libs like anyhow and such.

But happy to go either way if you'd prefer otherwise

I meant adding a # Error heading in the docs of the method and listing all possible errors it can throw.

There are no other Error sections in the library for other functions that return Results, but I did what some of those did and added a bit of text about the error condition. Happy to put that into an # Error section if you like though?

@zeenix
Copy link
Member

zeenix commented Feb 29, 2024

The name of the type makes it clear what it is about (ie an error caused because of overflow)

It's clear enough since it's the Err variant of a Result. :) The docs would remove any confusion (if there is still some). :)

It acts like the other errors returned in Results throughout this library, ie it implements Error and Display and can be handled accordingly by libs like anyhow and such.

usize also implements Display so that isn't a good argument but Error impl is. I'd be fine with RecvError here based on this argument.

There are no other Error sections in the library for other functions that return Results,

I didn't realize that but just because we've been lazy so far, doesn't mean we should continue to be so. :) Also in other fallible methods, all variants of the returned result type are possible so it's not as important as when returning an Error and not all variants could be possible (which would be the case for RecvError here).

By "convention", I meant the general conversion in the Rust world (I think it's even recommended in the book).

Happy to put that into an # Error section if you like though?

Yes please.

src/lib.rs Show resolved Hide resolved
@jsdw
Copy link
Contributor Author

jsdw commented Feb 29, 2024

Done and done!

@zeenix
Copy link
Member

zeenix commented Feb 29, 2024

Done and done!

You're too efficient. Do it again! 😆

src/lib.rs Outdated Show resolved Hide resolved
Copy link
Member

@zeenix zeenix left a comment

Choose a reason for hiding this comment

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

Almost done. :)

Copy link
Member

@zeenix zeenix left a comment

Choose a reason for hiding this comment

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

👍

@zeenix
Copy link
Member

zeenix commented Mar 1, 2024

@jsdw now if you could just update the PR description, I can squash merge with that as the commit message. 🙏

@jsdw jsdw changed the title Add example poll_recv impl Add poll_recv method to Receiver Mar 1, 2024
@niklasad1
Copy link

now if you could just update the PR description, I can squash merge with that as the commit message.

@zeenix good to go? :)

@zeenix zeenix merged commit 74d5889 into smol-rs:master Mar 4, 2024
5 checks passed
@jsdw jsdw deleted the poll_recv branch March 4, 2024 11:56
@notgull notgull mentioned this pull request Jun 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants