-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Implement Write for Cursor<[u8; N]>
, plus A: Allocator
cursor support
#92663
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
r? @dtolnay |
@rust-lang/libs-api: Previously there were only the following set of 4 concrete
This PR generalizes it to:
Example of code that this makes work: use std::io::{Cursor, Write};
fn main() {
let mut writer = Cursor::new([0u8; 128]);
write!(writer, "...").unwrap();
let n = writer.position();
println!("{:?}", &writer.into_inner()[..n as usize]);
} The previous set of impls required you to either heap-allocate your buffer (T= |
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: Concerns:
Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
@rfcbot concern specialization I think we'll likely just want |
It could also be confusing for users because |
If we back off to |
I agree that we should use a more specific impl. As an example:
Do we need to, given that a |
Note that this is only about
Ideally we might be able to specialize that deref, but right now it's "error: cannot specialize on trait
Sure, they can implement that, and it's not affected by whatever we do for Currently
The point would be an owned but indirect cursor, perhaps for a large array. Its use would be the same as I'm okay with backing off without specialization, just doing arrays. I especially accept the point that we can't fake the API semantics for Should I update that in this PR, or open a replacement? |
@cuviper Thanks for the clarifications; I had forgotten that I do still think that this should just handle I would suggest updating this PR. |
9d24325
to
38cef65
Compare
Write for Cursor<T>
Write for Cursor<[u8; N]>
Write for Cursor<[u8; N]>
Write for Cursor<[u8; N]>
, plus A: Allocator
cursor support
Ok, I've rewritten this to just the I considered |
@rfcbot fcp cancel |
@dtolnay proposal cancelled. |
@rfcbot fcp merge This PR generalizes the following existing stable impls: - impl Write for Cursor<&mut Vec<u8>>
+ impl<A: Allocator> Write for Cursor<&mut Vec<u8, A>>
- impl Write for Cursor<Vec<u8>>
+ impl<A: Allocator> Write for Cursor<Vec<u8, A>>
- impl Write for Cursor<Box<[u8]>>
+ impl<A: Allocator> Write for Cursor<Box<[u8], A>> and adds this new stable impl: impl<const N: usize> Write for Cursor<[u8; N]> Example of code that this makes work: use std::io::{Cursor, Write};
fn main() {
let mut writer = Cursor::new([0u8; 128]);
write!(writer, "...").unwrap();
let n = writer.position();
println!("{:?}", &writer.into_inner()[..n as usize]);
} The previous set of impls required you to either heap-allocate your buffer (T= |
Team member @dtolnay has proposed to merge this. The next step is review by the rest of the tagged team members: No concerns currently listed. Once a majority of reviewers approve (and at most 2 approvals are outstanding), this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
The final comment period, with a disposition to merge, as per the review above, is now complete. As the automated representative of the governance process, I would like to thank the author for their work and everyone else who contributed. This will be merged soon. |
@bors r+ |
📌 Commit 7d44316 has been approved by |
…lnay Implement `Write for Cursor<[u8; N]>`, plus `A: Allocator` cursor support This implements `Write for Cursor<[u8; N]>`, and also adds support for generic `A: Allocator` in `Box` and `Vec` cursors. This was inspired by a user questioning why they couldn't write a `Cursor<[u8; N]>`: https://users.rust-lang.org/t/why-vec-and-not-u8-makes-cursor-have-write/68210 Related history: - rust-lang#27197 switched `AsRef<[u8]>` for reading and seeking - rust-lang#67415 tried to use `AsMut<[u8]>` for writing, but did not specialize `Vec`.
Rollup of 6 pull requests Successful merges: - rust-lang#92519 (Use verbatim paths for `process::Command` if necessary) - rust-lang#92612 (Update stdlib for the l4re target) - rust-lang#92663 (Implement `Write for Cursor<[u8; N]>`, plus `A: Allocator` cursor support) - rust-lang#93263 (Consistently present absent stdio handles on Windows as NULL handles.) - rust-lang#93692 (keyword_docs: document use of `in` with `pub` keyword) - rust-lang#94984 (add `CStr` method that accepts any slice containing a nul-terminated string) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
This implements
Write for Cursor<[u8; N]>
, and also adds support for genericA: Allocator
inBox
andVec
cursors.This was inspired by a user questioning why they couldn't write a
Cursor<[u8; N]>
:https://users.rust-lang.org/t/why-vec-and-not-u8-makes-cursor-have-write/68210
Related history:
AsRef<[u8]>
for reading and seekingAsMut<[u8]>
for writing, but did not specializeVec
.