Edit: Update proposal here. PR here.
Hello everyone!
Recently I tried to obtain the length of a stream (Read + Seek) and was surprised that there isn’t a nice method for it. I had to manually write this:
let length = stream.seek(SeekFrom::End(0))?;
stream.seek(SeekFrom::Start(0))?;
Sure, it’s not terrible, but stream.len() or something similar would be a lot nicer and easier to read.
Therefore I propose to add the following provided methods to std::io::Seek:
(Alternative names for the last two: seek_to_{start,end}.)
It’s mostly convenience, in particular it avoids the need to import std::io::SeekFrom. Additionally, stream_len() could prevent a common bug: to forget to seek to the beginning after getting the stream length from seeking to the end.
Obviously, this is nothing groundbreaking. But I think it would be nice little addition (in particular stream_len() – I don’t care about the other two methods as much).
What do you think about this? Any related ideas? And: if most of you agree that this is a good idea: official RFC or just open a PR on the main repo?