Rfcs: io::Seek should implement .tell()

Created on 29 Jun 2015  ·  7Comments  ·  Source: rust-lang/rfcs

A file.tell() is much nicer than file.seek(std::io::SeekFrom::Current(0)). Additionally, I think .tell() doesn't need the file to be mutably borrowed (but I'm not sure).

T-libs

Most helpful comment

It's unsound to create a &mut T from a &T.

All 7 comments

Oh, no. An issue here doesn't help much, and may be ignored easily. I'd
like to suggest you creating an RFC, which maybe very short for adding
File::tell. They required to do this trivial thing, not me. So, good
luck. I prefer to the proposal.

:+1:; the syntax is so much nicer (and I agree that tell shouldn't need mutability).

Also, I'm wondering about compresseded stream, like gzip: seeking is hard, if not impossible. But there's certainly a _position_, which is essentially how many bytes have you decompressed thus far.

~One can extend the current set of Seek implementors with tell() by using this extension trait and blanket impl:~

pub trait Tell {
    fn tell(&self) -> std::io::Result<u64>;
}

impl Tell for Seek {
    fn tell(&self) -> std::io::Result<u64> {
        unsafe {
            let s = self as *const Self;
            let s = s as *mut Self;
            (*s).seek(SeekFrom::Current(0))
        }
    }
}

~I'm considering making an RFC with the body of the tell implementation above as a sample implementation. Is there a reason the above wouldn't work?~

EDIT: Don't do this.

It's unsound to create a &mut T from a &T.

~@sfackler: In general, yes -- in the code above, the idea is that SeekFrom::Current(0) SHOULDN'T change any state. Conceptually, tell really should just be an immutable access, but there IS no immutable API for getting the current file read/write offset. Is there an example you can provide why the sample implementation I provided above wouldn't work? I admit to not having done much research, but my intuition tells me that seek(SeekFrom::Current(0)) should really be a no-op.~

EDIT: Don't do this.

"should not" is not the same thing as "must not". It is totally possible and fine for code to implement seek right now in a way that mutates internal state.

Relevant link: the seek_convenience feature implements this as io::Seek::stream_position().

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clarfonthey picture clarfonthey  ·  3Comments

Diggsey picture Diggsey  ·  3Comments

torkleyy picture torkleyy  ·  3Comments

3442853561 picture 3442853561  ·  3Comments

mqudsi picture mqudsi  ·  3Comments