Skip to content

Safe conversions of OS streams to Stdio #1519

@SimonSapin

Description

@SimonSapin

Writing this issue was triggered by the question How to properly pipe commands in Rust. Currently, the answer is:

.stdin(unsafe { Stdio::from_raw_fd(cmd.stdout.unwrap().as_raw_fd()) })

This is fairly verbose, but more importantly requires unsafe code.

Similar (also unsafe) code can be used to create Stdio from other things like a file or a socket.

For those conversions that are safe⁽¹⁾, the standard library should provide a way to do them without using unsafe. For example, Stdio could implement From<ChildStdout>, From<File>, etc. Or maybe From<T> where T: IntoRawFd / T: IntoRawHandle.

⁽¹⁾ Some combinations seem incorrect, e.g. taking the reading end of a pipe (ChildStdout) and using it for writing (Command::stdout), but can they cause undefined behavior?

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiRelevant to the library API team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions