-
Notifications
You must be signed in to change notification settings - Fork 67
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
Impl poll_readable and poll_writable. #37
Conversation
```rust pub struct ListenStream { listener: Async<TcpListener>, } impl Stream for ListenStream { type Item = Result<Async<TcpStream>, std::io::Error>; fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> { loop { match self.listener.accept().now_or_never() { Some(Ok((stream, _))) => { return Poll::Ready(Some(Ok(stream))); } Some(Err(err)) => { return Poll::Ready(Some(Err(err))); } None => { match self.listener.poll_readable(cx) { Poll::Ready(Ok(())) => continue, Poll::Pending => return Poll::Pending, Poll::Ready(Err(err)) => return Poll::Ready(Some(Err(err))), } } } } } } ```
Thanks for the PR! I have only glossed over it now but will come back to it later. Just leaving a thought so I don't forget... I think calls to Secondly, I'm not sure if |
Not sure if it makes a large difference, but essentially as long as the stream is readable it should return The reason I went with &mut is to avoid having to mess around with |
Not sure if that makes it any better, but you can also use |
readable and writable return |
|
let readable = self.source.readable(); | ||
pin!(readable); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is not good to create and destruct a new future each time poll is called. The state of the future will be reset each time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I understand, but I don't see a different solution without changing the api to be poll
based instead of async
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought we can do that by splitting Source::ready
into fn poll_ready
that returns Poll
and async fn ready
that calls poll_fn(|cx| self.poll_ready(cx, dir))
, but it seems difficult as ready
takes &self
.
So, I think the solution (at this time) is probably not to provide poll_{readable, writable}
on the async-io side, but to store the future returned by {readable, writable}
on the user side.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can help with this tomorrow :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fyi storing the future doesn't work, at least not without unsafe code as the future takes &self, you'd need to construct a self referential struct.
Closes #36