-
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
add Stdin::lines, Stdin::split forwarder methods #86412
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
Add forwarder methods `Stdin::lines` and `Stdin::split`, which consume and lock a `Stdin` handle, and forward on to the corresponding `BufRead` methods. This should make it easier for beginners to use those iterator constructors without explicitly dealing with locks or lifetimes.
@rustbot label -T-libs +T-libs-api |
I'm pretty good with Rust and I had quite some trouble the other day trying to get stdin working with simple "line at a time" input, so this sounds great. |
I love the idea of making this easier. I wonder, though, if we should just provide a method like let stdin = std::io::stdin();
let stdin = stdin.lock(); Writing those two lines as one line generates lifetime errors, because I don't strongly object to the idea of adding But I personally think the priority should be to fix this common cause of lifetime errors that prevents people from writing various one-liners, and in the process, make it easier to call any method that would be available on the locked versions. |
Thanks for the feedback!
I think
I do think having something like On the other hand, there is some discussion here https://internals.rust-lang.org/t/add-owned-guard-types-to-standard-library/14912 about owned guard types, so maybe there is some interest in doing similarly for the stdio handles? The naming for those methods also seems reasonable: We could even go a step further and make free functions like
I think there's substantial benefit for adding those forwarders. We already have I think this is quite important: adding interactivity can feel like a substantial achievement beyond "hello world", and lowering the barriers to achieving this can make learning Rust a little friendlier. Adding
I'm happy to work on a pull request that consists only of adding I guess I'd like to ask: what benefits there are to having a public |
Locking for performance. If you're going to consume or produce a large amount of data in a loop, you can get a substantial performance improvement by locking once rather than once per iteration.
Those helpers seem entirely reasonable to me as well. I would be happy to see them added. And those helpers also provide more brevity, making it more reasonable to use the verbose name
I would love to see a PR that adds an That doesn't preclude adding helpers for the methods of BufRead as well. I just feel like this is a good incremental step to make Rust more learnable, and to give users a gentler introduction to one concept at a time. (Users would still have to understand that they need to lock stdin, but they wouldn't also simultaneously have to deal with a lifetime issue.) |
Thanks! I'll work on that, then. I know this is unlikely to be accepted because it's a breaking change, but what if the default stdio handles |
Much as that might be a good idea, breaking changes to the standard library are just not going to happen at this time. |
I don't think we could change |
Deferring until #86799 gets some review. |
add owned locked stdio handles Add stderr_locked, stdin_locked, and stdout_locked free functions to obtain owned locked stdio handles in a single step. Also add into_lock methods to consume a stdio handle and return an owned lock. These methods will make it easier to use locked stdio handles without having to deal with lifetime problems or keeping bindings to the unlocked handles around. Fixes rust-lang#85383; enables rust-lang#86412. r? `@joshtriplett` `@rustbot` label +A-io +C-enhancement +D-newcomer-roadblock +T-libs-api
☔ The latest upstream changes (presumably #86799) made this pull request unmergeable. Please resolve the merge conflicts. |
I'll resolve the merge conflicts (and try to rename the branch, because that name is no longer quite accurate). @rustbot label -S-blocked +S-waiting-on-author |
add `Stdin::lines`, `Stdin::split` forwarder methods Add forwarder methods `Stdin::lines` and `Stdin::split`, which consume and lock a `Stdin` handle, and forward on to the corresponding `BufRead` methods. This should make it easier for beginners to use those iterator constructors without explicitly dealing with locks or lifetimes. Replaces rust-lang#86412. ~~Based on rust-lang#86846 to get the tracking issue number for the `stdio_locked` feature.~~ Rebased after merge, so it's only one commit now. r? `@joshtriplett` `@rustbot` label +A-io +C-enhancement +D-newcomer-roadblock +T-libs-api
Add forwarder methods
Stdin::lines
andStdin::split
, which consumeand lock a
Stdin
handle, and forward on to the correspondingBufRead
methods. This should make it easier for beginners to use those iterator
constructors without explicitly dealing with locks or lifetimes.
Fixes #85383
@rustbot label +A-io +C-enhancement +D-newcomer-roadblock +T-libs