-
-
Notifications
You must be signed in to change notification settings - Fork 14.8k
Tracking Issue for owned locked stdio handles #86845
Copy link
Copy link
Closed
Labels
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`Area: `std::io`, `std::fs`, `std::net` and `std::path`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Metadata
Metadata
Assignees
Labels
A-ioArea: `std::io`, `std::fs`, `std::net` and `std::path`Area: `std::io`, `std::fs`, `std::net` and `std::path`C-tracking-issueCategory: An issue tracking the progress of sth. like the implementation of an RFCCategory: An issue tracking the progress of sth. like the implementation of an RFCD-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.Diagnostics: Confusing error or lint; hard to understand for new users.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.Relevant to the library API team, which will review and decide on the PR/issue.
Type
Fields
Give feedbackNo fields configured for issues without a type.
Feature gate:
#![feature(stdio_locked)]This is a tracking issue for adding owned locked handles to stdio.
Especially for beginners, using stdio handles can involve intimidating problems with locking and lifetimes. First, the user has to call a free function (
stderr(),stdin(),stdout()) to get a handle on the stream; then, the user might have to call thelock()method (for example, to gain access to thelines()iterator constructor). At this point, lifetime issues rapidly arise: the following code (playground) will produce a compile error.The need to create a
letbinding to the handle seems confusing and frustrating, especially if the program does not need to use the handle again. The explanation is that the lock behaves as if it borrows the original handle fromstdin(), and the temporary value created for the call to thelock()method is dropped at the end of the statement, invalidating the borrow. That explanation might be beyond the current level of understanding of a beginner who simply wants to write an interactive terminal program.Even experienced Rust programmers sometimes have trouble with the lifetime management required for using locked stdio handles: (comment), (comment).
Fortunately, the stdio handles don't contain any non-static references, so it's possible to create owned locks such as
StdinLock<'static>. This proposal creates methods and free functions for creating owned locked stdio handles. The methods consumeself, eliminating any lifetime problems. The free functions directly return an owned locked handle, for programs where there is no need to use an unlocked handle. The implementation currently depends on the mutex references in the stdio handles being static, but this does not preclude future changes to the locking internals (for example, usingArcto wrap the mutex).Public API
Steps / History
Unresolved Questions
stdin_locked(), etc.) over the ones that obtain unlocked handles (stdin(), etc.)?@rustbot label +A-io +D-newcomer-roadblock