-
-
Notifications
You must be signed in to change notification settings - Fork 14.7k
Description
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.