-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Use generic io trait implementations for Cursor when possible #23364
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @pcwalton (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. The way Github handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see CONTRIBUTING.md for more information. |
It would be easy to add an implementation of |
Why |
Good questions. I chose |
I personally do find it much nicer for these to be generic implementations rather than hard-coded, but I also fear that we don't have the right trait to operate generically with at this time. I feel that A concrete backwards-compatibility risk is that this would enable something like cc @aturon |
The alternative to The lack of generic implementation is also an issue I have with the stdlib in general at the moment. I’m fearing that it will get messy when using a lot of third party provided types. If the author forgets to implement a trait you need, you end up having a lot of wrapper types to implement these traits yourself. In the case of reference-like types, the most natural trait to implement is |
|
|
I tend to agree that we may want to take things a bit slow here. Note, however, that it's not exactly back-compat to add such an impl later, since it could introduce coherence conflicts with downstream crates. (It might be possible to do if we introduced a new trait at the same time.) More broadly, we've had proposals like this come up for using |
I agree! This is also somewhat covered by rust-lang/rfcs#279 |
☔ The latest upstream changes (presumably #23654) made this pull request unmergeable. Please resolve the merge conflicts. |
Should I pursue this PR but using |
@nwin out of curiosity, do you have a use case for wanting to create a |
My use case is a chat (IRC) server where the messages need to get distributed to many clients. For this I planned to use |
Hm currently |
Hm ok this seems to have fallen by the wayside a bit, so I'm going to close this out of inactivity for now. I don't think that the use case for this generalization is super compelling today, but it doesn't mean it won't be one day! I believe the modifications here should be fine backwards-compatible additions, so the door is still open for this. |
I updated my implementation. Github does not seem to recognize it if that (maybe that happens if the PR gets reopened). It now uses The only drawback is, that specialized implementation for fixed-sized arrays are now ruled out as long as RFC#1210 it not accepted & implemented.
|
Yes I think that submitting a separate PR is fine to do at this point. |
This is a revival of #23364. Github didn’t recognize my updated branch there. The cursor implementation now uses `AsRef` which means that fixed-sized array can now be used with `Cursor`. Besides that, the generic implementation simplifies the code as the macro can be avoided. The only drawback is, that specialized implementation for fixed-sized arrays are now ruled out unless [RFC#1210](rust-lang/rfcs#1210) is accepted & implemented. `Box<[u8]>` cannot be used yet, but that should be mitigated by [implementing `AsRef` for `Box` and friends](https://internals.rust-lang.org/t/forward-implement-traits-on-smart-pointers-make-smart-pointers-more-transparent/2380/3). I will submit a separate PR for that later as it is an orthogonal issue.
This PR provides generic implementations of most io traits for
Cursor
. The former behavior is unchanged while it is now possible to use boxed types likeBox<[u8]>
or futureRc<[T]>
.A generic writer implementation does not seem to be possible, because it would conflict with the desirable specialization for
Vec<u8>
.