-
Notifications
You must be signed in to change notification settings - Fork 633
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
Should AsyncRead and AsyncWrite take self by Pin? #1454
Comments
I have briefly experimented with this in |
I'm strongly in favor of this change. Implementing |
I'm still heavily in favor of #1365 Although we can't yet have versions of those traits where the produced Future is able to borrow the IO Object, we can at least have traits that cover the case where the IO object is inside an |
@Matthias247 It's not clear what an implementation like that would look like or how it would be object-safe (since |
Object safety is certainly a problem, since it will be impossible to unify all possible returned Futures. But I'm not sure why we deem it important for these kinds of types, whereas there are enough other types around which are not object safe either. Types that I am currently using are the followings (error handling omitted):
However Read and Write might be more problematic, because we actually want to pass the read/write buffer by But still: Standardizing something that sounds like the preferred generic way to do these operations, but which actually is incompatible with the all other primitives, doesn't sound like the right approach. Maybe the current |
I have written an experimental implementation of this: master...taiki-e:io-pin ( |
I think we should do this and make the change timed to coincide with #1455. |
I don’t think so. The use case is extremely unclear. I also can’t see how generators would be useful for those interfaces. They don’t have any space to store state (eg a returned Future). So where would that Generator state be stored? If it’s inside the implementation of the Trait itself, it can’t interact with the Input parameters. I think this needs at least a very motivating example what we can’t do without it. |
Question: Should all polling methods take |
@stjepang I'd expect that type specifically to implement |
On reflection I'm actually against this change because it would significantly inconvenience calling the futures adapters of |
@withoutboats can you explain why you think this would be inconvenient? It's exactly how most of the rest of the API works. Having to manually reborrow is an annoying issue, but I don't see how it's different for these types than it is across the rest of the library. |
@cramertj I don't know what you mean, I've looked at the API docs and found these examples that require pinning:
The vast majority of the "convenience APIs" don't require intermediate pinning. All three of these have long term alternatives that can avoid pinning in the common case (a "race" macro and for await loops I mean). My preference is to avoid intermediate pinning by the end user as much as possible, limiting pinning to an implementation detail of the executor. |
for-await loops aren't an alternative to the There are also a number of methods on I don't really think the situation here is any different-- that is, it's perfectly fine to expose the convenience APIs on |
It seems to me this will make the norm for code that's generic over an IO object to add wouldn't the same code be permitted but without everyone tracking Unpin by keeping the traits how they are and only implement the bridge from a generator to an IO type on a pinned value? |
No-- that doesn't allow owned, non-allocating objects. It also hits a number of significant ergonomics and compiler-implementation issues, as we saw when we attempted to do the same thing for |
We didn't do this because there's not a lot of motivation, but on reflection there's also not a lot of downside. Implementations of these traits are a) usually concrete (and therefore
Unpin
) and b) usually don't need to worry too hard about moving their receiver around in poll_read/poll_write.The motivation for this change would be that it would enable users to write mock AsyncRead sources from async fns or possibly async generators without boxing them. Possibly someday the same will be true for AsyncWrite once generators with resume arguments are a thing (along the same line as Sink).
cc @cramertj
The text was updated successfully, but these errors were encountered: