Skip to content

stdio handles (stdin, stdout, stderr) should be Seek at least on Unix #72802

@ijackson

Description

@ijackson

Currently, std::io::Stdin et al do not implement Seek.

It is true that a Unix program is not guaranteed that its stdin/stdout/stderr will be seekable. Often they won't be. But, they may be. If stdin is redirected from a file, for example, lseek (the system call) will work fine.

The Seek trait does not promise that seek actually works. seek returns io::Result (quite properly). Indeed a std::io::File object can easily refer to a non-seekable object, depending what path was passed.

Conversely, not implementing Seek makes it impossible to perform this operation in safe Rust. There are perhaps some workarounds like asking to open /dev/stdin, but they are non-portable and often quite horrible. Using unsafe { libc::lseek(...) } is quite straightforward (apart, perhaps, from a question about whether lseek64 should be used instead) but we shouldn't expect our users to do that when we could have provided a safe way.

There are file data integrity and synchronisation issues with Seek. But these are not significantly worse for stdin/out/err than for std::fs::File. They are not a memory safety issue and we have already chosen to address this in the documentation rather than through the type system.

I come from a Unix background. I don't know what the situation is on Windows. But, the fact that in Standard C, it is legal to call fseek on stdin (for example) suggests that the correct answer for Rust is to permit the user to try to call seek on all platforms.

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-libs-apiRelevant to the library API team, which will review and decide on the PR/issue.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions